fw4spl
ools/src/fwDataTools/helper/Array.cpp
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 #include "fwDataTools/helper/Array.hpp"
8 
9 namespace fwDataTools
10 {
11 
12 namespace helper
13 {
14 
15 Array::Array( ::fwData::Array::sptr array ) :
16  m_array(array)
17 {
18  SLM_ASSERT("Array ptr is null.", array);
19  m_lock = array->getBufferObject()->lock();
20 }
21 
22 //-----------------------------------------------------------------------------
23 
24 Array::~Array()
25 {
26 
27 }
28 
29 //-----------------------------------------------------------------------------
30 
31 void* Array::getBuffer()
32 {
33  return m_lock.getBuffer();
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 const void* Array::getBuffer() const
39 {
40  return m_lock.getBuffer();
41 }
42 
43 //------------------------------------------------------------------------------
44 
45 void Array::setBuffer(void* buf, bool takeOwnership, ::fwMemory::BufferAllocationPolicy::sptr policy)
46 {
47  if(m_array->getIsBufferOwner())
48  {
49  if(!m_array->getBufferObject()->isEmpty())
50  {
51  m_array->getBufferObject()->destroy();
52  }
53  }
54  else
55  {
56  ::fwMemory::BufferObject::sptr newBufferObject = ::fwMemory::BufferObject::New();
57  ::fwMemory::BufferObject::sptr oldBufferObject = m_array->getBufferObject();
58  oldBufferObject->swap(newBufferObject);
59  }
60  m_array->getBufferObject()->setBuffer(buf, (buf == NULL) ? 0 : m_array->getSizeInBytes(), policy);
61  m_array->setIsBufferOwner(takeOwnership);
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 void Array::setBuffer(
67  void* buf,
68  bool takeOwnership,
69  const ::fwTools::Type& type,
70  const ::fwData::Array::SizeType& size,
71  size_t nbOfComponents,
72  ::fwMemory::BufferAllocationPolicy::sptr policy)
73 {
74  m_array->resize( type, size, nbOfComponents, false);
75  this->setBuffer(buf, takeOwnership, policy);
76 }
77 
78 //-----------------------------------------------------------------------------
79 
80 char* Array::begin()
81 {
82  void* v = this->getBuffer();
83  return static_cast<char*>(v);
84 }
85 
86 //------------------------------------------------------------------------------
87 
88 char* Array::end()
89 {
90  return reinterpret_cast<char*> (static_cast<char*>(this->getBuffer()) + m_array->getSizeInBytes());
91 }
92 //------------------------------------------------------------------------------
93 
94 const char* Array::begin() const
95 {
96  return static_cast<const char*>(this->getBuffer());
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 const char* Array::end() const
102 {
103  return reinterpret_cast<const char*> (static_cast<const char*>(this->getBuffer()) + m_array->getSizeInBytes());
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 char* Array::getBufferPtr( const ::fwData::Array::IndexType& id, size_t component, size_t sizeOfType )
109 {
110  size_t sizeOf = m_array->getType().sizeOf();
111  size_t offset = m_array->getBufferOffset(id, component, sizeOf);
112  char* item = static_cast<char*>(this->getBuffer()) + offset;
113  return item;
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 const char* Array::getBufferPtr( const ::fwData::Array::IndexType& id, size_t component, size_t sizeOfType ) const
119 {
120  size_t sizeOf = m_array->getType().sizeOf();
121  size_t offset = m_array->getBufferOffset(id, component, sizeOf);
122  const char* item = static_cast<const char*>(this->getBuffer()) + offset;
123  return item;
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 void Array::setItem(const ::fwData::Array::IndexType& id, const void* value)
129 {
130  size_t sizeOf = m_array->getType().sizeOf();
131  const char* val = static_cast<const char*>(value);
132  char* item = this->getBufferPtr(id, 0, sizeOf);
133  std::copy(val, val + m_array->getNumberOfComponents()*sizeOf, item);
134 }
135 //------------------------------------------------------------------------------
136 
137 void Array::setItem(const ::fwData::Array::IndexType& id, const size_t component, const void* value)
138 {
139  size_t sizeOf = m_array->getType().sizeOf();
140  const char* val = static_cast<const char*>(value);
141  char* item = this->getBufferPtr(id, component, sizeOf);
142  std::copy(val, val + sizeOf, item);
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 void* Array::getItem(const ::fwData::Array::IndexType& id, const size_t component)
148 {
149  size_t sizeOf = m_array->getType().sizeOf();
150  char* item = this->getBufferPtr(id, component, sizeOf);
151  return item;
152 }
153 
154 //------------------------------------------------------------------------------
155 
156 void Array::getItem(const ::fwData::Array::IndexType& id, void* value) const
157 {
158  size_t sizeOf = m_array->getType().sizeOf();
159  const char* item = this->getBufferPtr(id, 0, sizeOf);
160  char* val = static_cast<char*>(value);
161  std::copy(item, item + m_array->getNumberOfComponents()*sizeOf, val);
162 }
163 
164 //------------------------------------------------------------------------------
165 
166 void Array::getItem(const ::fwData::Array::IndexType& id, const size_t component, void* value) const
167 {
168  size_t sizeOf = m_array->getType().sizeOf();
169  const char* item = this->getBufferPtr(id, component, sizeOf);
170  char* val = static_cast<char*>(value);
171  std::copy(item, item + m_array->getNumberOfComponents()*sizeOf, val);
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 ::fwMemory::BufferObject::Lock Array::getLock() const
177 {
178  return m_lock;
179 }
180 
181 //------------------------------------------------------------------------------
182 
183 } // namespace helper
184 
185 } // namespace fwDataTools
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
base class for BufferObject Lock
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308