fw4spl
ools/include/fwDataTools/helper/Array.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "fwDataTools/config.hpp"
10 
11 #include <fwData/Array.hpp>
12 
13 #include <fwMemory/BufferObject.hpp>
14 
15 namespace fwDataTools
16 {
17 
18 namespace helper
19 {
20 
24 class FWDATATOOLS_CLASS_API Array
25 {
26 
27 public:
28  fwCoreClassFactoryMacro((Array), (((::fwData::Array::sptr))), new Array )
29 
30  FWDATATOOLS_API Array( ::fwData::Array::sptr array );
31  FWDATATOOLS_API virtual ~Array();
32 
40  FWDATATOOLS_API virtual void setItem(const ::fwData::Array::IndexType& id, const void* value);
41 
51  FWDATATOOLS_API virtual void setItem(const ::fwData::Array::IndexType& id, const size_t component,
52  const void* value);
53 
55 
63  FWDATATOOLS_API virtual void* getItem(const ::fwData::Array::IndexType& id, const size_t component = 0);
64 
74  template< typename T > T* getItem(const ::fwData::Array::IndexType& id, const size_t component = 0);
75 
82  FWDATATOOLS_API virtual void getItem(const ::fwData::Array::IndexType& id, void* value) const;
83  FWDATATOOLS_API virtual void getItem(const ::fwData::Array::IndexType& id, const size_t component,
84  void* value) const;
85 
92  FWDATATOOLS_API virtual void* getBuffer();
93  FWDATATOOLS_API virtual const void* getBuffer() const;
95 
108  FWDATATOOLS_API void setBuffer(
109  void* buf,
110  bool takeOwnership,
111  const ::fwTools::Type& type,
112  const ::fwData::Array::SizeType& size,
113  size_t nbOfComponents,
114  ::fwMemory::BufferAllocationPolicy::sptr policy = ::fwMemory::BufferMallocPolicy::New()
115  );
116 
118  FWDATATOOLS_API virtual char* begin();
119  FWDATATOOLS_API virtual char* end();
120  FWDATATOOLS_API virtual const char* begin() const;
121  FWDATATOOLS_API virtual const char* end() const;
122 
124  template< typename T > T* begin();
125  template< typename T > T* end();
126 
137  FWDATATOOLS_API char* getBufferPtr( const ::fwData::Array::IndexType& id, size_t component, size_t sizeOfType );
138  FWDATATOOLS_API const char* getBufferPtr( const ::fwData::Array::IndexType& id, size_t component,
139  size_t sizeOfType ) const;
141 
143  FWDATATOOLS_API ::fwMemory::BufferObject::Lock getLock() const;
144 
145 protected:
146 
155  FWDATATOOLS_API virtual void setBuffer(
156  void* buf,
157  bool takeOwnership = false,
158  ::fwMemory::BufferAllocationPolicy::sptr policy = ::fwMemory::BufferMallocPolicy::New()
159  );
160 
161  ::fwData::Array::sptr m_array;
163 
164 };
165 
166 //------------------------------------------------------------------------------
167 
168 template< typename T >
170 {
171  return static_cast<T*>(this->getBuffer());
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 template< typename T >
177 T* Array::end()
178 {
179  return reinterpret_cast<T*> (static_cast<char*>(this->getBuffer()) + m_array->getSizeInBytes());
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 template< typename T >
185 T* Array::getItem(const ::fwData::Array::IndexType& id, const size_t component)
186 {
187  return static_cast<T*> (this->getItem(id, component));
188 }
189 
190 } // namespace helper
191 
192 } // namespace fwData
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
virtual FWDATATOOLS_API void * getItem(const ::fwData::Array::IndexType &id, const size_t component=0)
Getter for a buffer item. pointer to the requested item in the buffer.
base class for BufferObject Lock
virtual FWDATATOOLS_API char * begin()
Returns the begining/end of the buffer interpreted as a char buffer.
Helper to manage array buffer. Lock the buffer before to modify it.
#define fwCoreClassFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate methods for classes with one factory.