8 #include "fwData/registry/macros.hpp" 9 #include "fwData/Exception.hpp" 10 #include "fwData/Array.hpp" 22 inline size_t computeSize(
24 const ::fwData::Array::SizeType &size,
25 size_t nbOfComponents )
32 std::accumulate (size.begin(),
33 size.end(), nbOfComponents, std::multiplies< ::fwData::Array::SizeType::value_type >() );
41 strides.reserve(size.size());
43 size_t currentStride = sizeOfType*nbOfComponents;
44 for(
const SizeType::value_type& s : size)
46 strides.push_back(currentStride);
57 m_bufferObject(::
fwMemory::BufferObject::New()),
76 m_strides.swap(_source->m_strides);
77 m_size.swap(_source->m_size);
78 m_bufferObject->swap(_source->m_bufferObject);
80 std::swap(m_type, _source->m_type);
81 std::swap(m_nbOfComponents, _source->m_nbOfComponents);
82 std::swap(m_isBufferOwner, _source->m_isBufferOwner);
89 Array::csptr other = Array::dynamicConstCast(_source);
91 "Unable to copy" + (_source ? _source->getClassname() : std::string(
"<NULL>"))
97 if( !other->m_bufferObject->isEmpty() )
100 this->
resize(other->m_type, other->m_size, other->m_nbOfComponents,
true);
101 char * buffDest =
static_cast< char *
>( lockerDest.
getBuffer() );
103 char * buffSrc =
static_cast< char *
>( lockerSource.
getBuffer() );
104 std::copy(buffSrc, buffSrc+other->getSizeInBytes(), buffDest );
108 m_strides = other->m_strides;
109 m_type = other->m_type;
110 m_size = other->m_size;
111 m_nbOfComponents = other->m_nbOfComponents;
118 const ::fwTools::Type &type,
120 size_t nbOfComponents,
124 nbOfComponents = (nbOfComponents == 0) ? 1 : nbOfComponents;
125 size_t bufSize = computeSize(type.sizeOf(), size, nbOfComponents);
127 if(reallocate && (m_isBufferOwner || m_bufferObject->isEmpty()))
129 if(m_bufferObject->isEmpty())
131 m_bufferObject->allocate(bufSize);
135 m_bufferObject->reallocate(bufSize);
137 m_isBufferOwner =
true;
139 else if(reallocate && !m_isBufferOwner)
142 "Tried to reallocate a not-owned Buffer.");
148 m_nbOfComponents = nbOfComponents;
157 return this->
resize(m_type, size, nbOfComponents, reallocate);
164 return this->
resize(m_type, size, m_nbOfComponents, reallocate);
172 return this->
resize( fwType, size, nbOfComponents, reallocate);
179 if ( !this->m_bufferObject->isEmpty() )
183 this->m_bufferObject->destroy();
188 m_nbOfComponents = 0;
196 return m_size.empty();
203 return m_type.
sizeOf() * m_nbOfComponents;
210 return computeSize(1, m_size, m_nbOfComponents);
217 return computeSize(m_type.
sizeOf(), m_size, m_nbOfComponents);
239 m_nbOfComponents = (nb == 0) ? 1 : nb;
244 (m_isBufferOwner && !m_bufferObject->isEmpty())
252 return m_nbOfComponents;
259 return m_size.size();
266 m_isBufferOwner = own;
273 return m_isBufferOwner;
291 (m_isBufferOwner && !m_bufferObject->isEmpty())
308 "Given index has " <<
id.size() <<
" dimensions, but Array has " << m_size.size() <<
"dimensions.",
309 id.size() == m_size.size()
314 std::transform(
id.begin(),
id.end(), m_strides.begin(), offsets.begin(),
315 std::multiplies<OffsetType::value_type>() );
318 offset = std::accumulate(offsets.begin(), offsets.end(), size_t(0));
320 offset += component*sizeOfType;
static FWDATA_API OffsetType computeStrides(SizeType size, size_t nbOfComponents, size_t sizeOfType)
Compute strides for given parameters.
virtual FWDATA_API size_t resize(const ::fwTools::Type &type, const SizeType &size, size_t nbOfComponents, bool reallocate=false)
Resizes and allocate (if needed) the array.
virtual FWDATA_API bool getIsBufferOwner() const
Getter for array's buffer ownership.
std::vector< size_t > SizeType
Array size type.
virtual FWDATA_API void setType(const std::string &type)
Setter for array's type.
#define OSLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
virtual FWDATA_API size_t getNumberOfElements() const
Get the number of elements of type <getType()> in the array.
The namespace fwMemory contains tools to manage memory. Use for dump.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
virtual FWDATA_API size_t getSizeInBytes() const
Getter for the array view size.
virtual FWDATA_API size_t getElementSizeInBytes() const
Get the size of one element of the array, ie. sizeof type * nbOfComponents.
FWDATA_API size_t getBufferOffset(const ::fwData::Array::IndexType &id, size_t component, size_t sizeOfType) const
Compute offset in buffer for given parameters of type.
virtual FWDATA_API const OffsetType & getStrides() const
Getter for the array strides.
base class for BufferObject Lock
FieldMapType m_fields
Fields.
virtual FWDATA_API::fwTools::Type getType() const
Getter for array's type.
std::vector< size_t > OffsetType
Offset type.
FWDATA_API void swap(Array::sptr _source)
Exchanges the content of the Array with the content of _source.
virtual FWDATA_API void clear()
Clear this array. Size, type, nbOfComponents are reset, buffer is released.
virtual FWDATA_API size_t getNumberOfComponents() const
Getter for number of components.
Provides a way to manage a view on a multidimentionnal array.
virtual FWDATA_API bool empty() const
Test whether array is empty.
virtual FWDATA_API size_t getNumberOfDimensions() const
Getter for number of dimensions, ie. getSize().size()
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
LockBase< T >::BufferType getBuffer() const
Returns BufferObject's buffer pointer.
FWDATA_API Array(::fwData::Object::Key key)
Constructor.
virtual FWDATA_API const SizeType & getSize() const
Getter for the array size.
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
Contains the representation of the data objects used in the framework.
virtual const std::string & getClassname() const override
return full object's classname with its namespace, i.e. fwCore::BaseObject
virtual FWDATA_API void setIsBufferOwner(const bool own)
Set array's buffer ownership.
virtual FWDATA_API void setNumberOfComponents(size_t nb)
Setter for array's number of components If the array has a buffer and owns it, the buffer will be rea...