fw4spl
BufferObject.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 #define CAMP_COMPILATION
8 
9 #include "fwMemory/BufferObject.hpp"
10 
11 fwCampImplementMacro((fwMemory)(BufferObject))
12 {
13  builder.tag("buffer")
14  .function("classname", &::fwMemory::BufferObject::className)
15  .function("is_a", (bool(::fwMemory::BufferObject::*)(const std::string&) const) & ::fwMemory::BufferObject::isA);
16 }
17 
18 namespace fwMemory
19 {
20 
22  m_buffer(0),
23  m_size(0),
24  m_bufferManager(::fwMemory::BufferManager::getDefault()),
25  m_allocPolicy(::fwMemory::BufferNoAllocPolicy::New())
26 {
27  m_bufferManager->registerBuffer(&m_buffer).get();
28 }
29 
30 //------------------------------------------------------------------------------
31 
33 {
34  // In the past we asserted that m_count was expired, but it can not be ensured because the unlock is asynchronous
35  // So we simply unregister the buffer and we will check the counter value on the buffer manager thread instead
36  m_bufferManager->unregisterBuffer(&m_buffer).get();
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 void BufferObject::allocate(SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr& policy)
42 {
43  m_bufferManager->allocateBuffer(&m_buffer, size, policy).get();
44  m_allocPolicy = policy;
45  m_size = size;
46 }
47 
48 //------------------------------------------------------------------------------
49 
50 void BufferObject::reallocate(SizeType size)
51 {
52  m_bufferManager->reallocateBuffer(&m_buffer, size).get();
53  m_size = size;
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
60  m_bufferManager->destroyBuffer(&m_buffer).get();
61  m_allocPolicy = ::fwMemory::BufferNoAllocPolicy::New();
62  m_size = 0;
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 void BufferObject::setBuffer(::fwMemory::BufferManager::BufferType buffer, SizeType size,
68  const ::fwMemory::BufferAllocationPolicy::sptr& policy)
69 {
70  m_bufferManager->setBuffer(&m_buffer, buffer, size, policy).get();
71  m_allocPolicy = policy;
72  m_size = size;
73 }
74 
75 //------------------------------------------------------------------------------
76 
78 {
79  return BufferObject::Lock(this->getSptr());
80 }
81 
82 //------------------------------------------------------------------------------
83 
85 {
86  return BufferObject::ConstLock(this->getConstSptr());
87 }
88 
89 //------------------------------------------------------------------------------
90 
91 void BufferObject::swap( const BufferObject::sptr& _source )
92 {
93  m_bufferManager->swapBuffer(&m_buffer, &(_source->m_buffer)).get();
94 
95  std::swap(m_size, _source->m_size);
96  m_bufferManager.swap(_source->m_bufferManager);
97  m_allocPolicy.swap(_source->m_allocPolicy);
98 }
99 
100 //------------------------------------------------------------------------------
101 
102 BufferManager::StreamInfo BufferObject::getStreamInfo() const
103 {
104  return m_bufferManager->getStreamInfo(&m_buffer).get();
105 }
106 
107 //------------------------------------------------------------------------------
108 
110  SizeType size,
111  const ::boost::filesystem::path& sourceFile,
112  ::fwMemory::FileFormatType format,
113  const ::fwMemory::BufferAllocationPolicy::sptr& policy
114  )
115 {
116  m_size = size;
117  m_allocPolicy = policy;
118  m_bufferManager->setIStreamFactory(&m_buffer, factory, size, sourceFile, format, policy).get();
119 }
120 
121 } //namespace fwMemory
#define SPTR(_cls_)
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
FWMEMORY_API void swap(const BufferObject::sptr &_source)
Exchanges the content of the BufferObject with the content of _source.
std::string className() const
return the sub class classname : an alias of this->getClassname
base class for BufferObject Lock
Contains fwAtomsFilter::factory utilities.
virtual FWMEMORY_API void allocate(SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy=::fwMemory::BufferMallocPolicy::New())
Buffer allocation.
virtual FWMEMORY_API ~BufferObject()
BufferObject destructor.
FWMEMORY_API void setIStreamFactory(const std::shared_ptr< ::fwMemory::stream::in::IFactory > &factory, SizeType size, const ::boost::filesystem::path &sourceFile="",::fwMemory::FileFormatType format=::fwMemory::OTHER, const ::fwMemory::BufferAllocationPolicy::sptr &policy=::fwMemory::BufferMallocPolicy::New())
Set a stream factory for the buffer manager The factory will be used to load data on demand by the bu...
virtual FWMEMORY_API void setBuffer(::fwMemory::BufferManager::BufferType buffer, SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy=::fwMemory::BufferMallocPolicy::New())
Buffer setter.
BufferManager implementation.
virtual FWMEMORY_API void reallocate(SizeType size)
Buffer reallocation.
virtual FWMEMORY_API Lock lock()
Return a lock on the BufferObject.
Define Base class for FW4SPL buffers.
virtual FWMEMORY_API void destroy()
Buffer deallocation.
FWMEMORY_API BufferObject()
BufferObject constructor.