fw4spl
BufferObject.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 "fwMemory/BufferAllocationPolicy.hpp"
10 #include "fwMemory/BufferManager.hpp"
11 #include "fwMemory/config.hpp"
12 
13 #include <fwCamp/macros.hpp>
14 
15 #include <boost/filesystem/path.hpp>
16 
17 #include <istream>
18 #include <type_traits>
19 
20 fwCampAutoDeclareMacro((fwMemory)(BufferObject), FWMEMORY_API);
21 
22 namespace fwMemory
23 {
24 
25 namespace stream
26 {
27 namespace in
28 {
29 class IFactory;
30 }
31 }
32 
51 class FWMEMORY_CLASS_API BufferObject : public ::fwCore::BaseObject
52 {
53 
54 public:
55 
56  typedef std::shared_ptr< void > CounterType;
57  typedef std::weak_ptr< void > WeakCounterType;
58 
59  typedef size_t SizeType;
60 
63 
65  std::string className() const
66  {
67  return this->getClassname();
68  }
69 
70  //------------------------------------------------------------------------------
71 
72  virtual ::fwMemory::BufferManager::BufferType getBuffer() const
73  {
74  return m_buffer;
75  }
76 
87  template <typename T>
88  class LockBase
89  {
90  public:
91 
92  typedef typename ::boost::conditional< std::is_const< T >::value, const void*, void* >::type BufferType;
93 
98  {
99  }
100 
108  LockBase( const SPTR(T)& bo ) :
109  m_bufferObject(bo)
110  {
111  SLM_ASSERT("Can't lock NULL object", bo);
112 
113  ::fwCore::mt::ScopedLock lock(bo->m_lockDumpMutex);
114  m_count = bo->m_count.lock();
115  if ( !m_count )
116  {
117  m_count = bo->m_bufferManager->lockBuffer(&(bo->m_buffer)).get();
118  bo->m_count = m_count;
119  }
120 
121  }
122 
126  typename LockBase<T>::BufferType getBuffer() const
127  {
128  SPTR(T) bufferObject = m_bufferObject.lock();
129  BufferType buffer = bufferObject->m_buffer;
130  return buffer;
131  }
132 
136  void reset()
137  {
138  m_count.reset();
139  m_bufferObject.reset();
140  }
141 
142  protected:
143 
144  BufferObject::CounterType m_count;
145  WPTR(T) m_bufferObject;
146  };
147 
162  FWMEMORY_API BufferObject();
163 
169  FWMEMORY_API virtual ~BufferObject();
170 
181  FWMEMORY_API virtual void allocate(SizeType size,
182  const ::fwMemory::BufferAllocationPolicy::sptr& policy =
183  ::fwMemory::BufferMallocPolicy::New());
184 
195  FWMEMORY_API virtual void reallocate(SizeType size);
196 
204  FWMEMORY_API virtual void destroy();
205 
216  FWMEMORY_API virtual void setBuffer(::fwMemory::BufferManager::BufferType buffer, SizeType size,
217  const ::fwMemory::BufferAllocationPolicy::sptr& policy =
218  ::fwMemory::BufferMallocPolicy::New());
219 
225  FWMEMORY_API virtual Lock lock();
226 
232  FWMEMORY_API virtual ConstLock lock() const;
233 
237  SizeType getSize() const
238  {
239  return m_size;
240  }
241 
245  bool isEmpty() const
246  {
247  return m_size == 0;
248  }
249 
253  long lockCount() const
254  {
255  return m_count.use_count();
256  }
257 
261  long isLocked() const
262  {
263  return lockCount() != 0;
264  }
265 
269  const ::fwMemory::BufferManager::ConstBufferPtrType getBufferPointer() const
270  {
271  return &m_buffer;
272  }
273 
274  //------------------------------------------------------------------------------
275 
276  ::fwCore::mt::ReadWriteMutex& getMutex()
277  {
278  return m_mutex;
279  }
280 
282  FWMEMORY_API void swap( const BufferObject::sptr& _source );
283 
284  FWMEMORY_API BufferManager::StreamInfo getStreamInfo() const;
285 
296  FWMEMORY_API void setIStreamFactory(const SPTR(::fwMemory::stream::in::IFactory)& factory,
297  SizeType size,
298  const ::boost::filesystem::path& sourceFile = "",
299  ::fwMemory::FileFormatType format = ::fwMemory::OTHER,
300  const ::fwMemory::BufferAllocationPolicy::sptr& policy = ::fwMemory::BufferMallocPolicy::New()
301  );
302 
303 protected:
304 
305  ::fwMemory::BufferManager::BufferType m_buffer;
306 
307  SizeType m_size;
308 
309  mutable WeakCounterType m_count;
310  mutable ::fwCore::mt::Mutex m_lockDumpMutex;
312 
313  ::fwMemory::BufferManager::sptr m_bufferManager;
314 
315  ::fwMemory::BufferAllocationPolicy::sptr m_allocPolicy;
316 };
317 
318 }
#define SPTR(_cls_)
bool isEmpty() const
Returns true if the buffer is empty.
SizeType getSize() const
Returns the buffer&#39;s size.
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
::boost::shared_mutex ReadWriteMutex
Defines a single writer, multiple readers mutex.
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
#define WPTR(_cls_)
std::string className() const
return the sub class classname : an alias of this->getClassname
base class for BufferObject Lock
Contains fwAtomsFilter::factory utilities.
LockBase< T >::BufferType getBuffer() const
Returns BufferObject&#39;s buffer pointer.
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
LockBase()
Build an empty 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
void reset()
Release any count on any Buffer the lock may have.
#define fwCoreAllowSharedFromThis()
Generate getSptr and getConstSptr methods.
const ::fwMemory::BufferManager::ConstBufferPtrType getBufferPointer() const
Returns pointer on BufferObject&#39;s buffer.
Define Base class for FW4SPL buffers.
long lockCount() const
Returns the number of locks on the BufferObject.
LockBase(const std::shared_ptr< T > &bo)
Build a lock on object &#39;bo&#39;.
long isLocked() const
Returns true if the buffer has any lock.