Main MRPT website > C++ reference for MRPT 1.4.0
CReferencedMemBlock.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CReferencedMemBlock_H
10 #define CReferencedMemBlock_H
11 
12 #include <mrpt/base/link_pragmas.h>
13 #include <vector>
14 #include <utility>
15 
16 // STL+ library:
18 
19 namespace mrpt
20 {
21  namespace utils
22  {
23  /** Represents a memory block (via "void*") that can be shared between several objects through copy operator (=).
24  * It keeps the reference count and only when it comes to zero, the memory block is really freed.
25  * \ingroup mrpt_base_grp
26  */
27  class BASE_IMPEXP CReferencedMemBlock : public stlplus::smart_ptr< std::vector<char> >
28  {
30  public:
31  /** Constructor with an optional size of the memory block */
32  CReferencedMemBlock(size_t mem_block_size = 0 );
33 
34  /** Destructor, calls dereference_once. */
36 
37  /** Resize the shared memory block. */
38  void resize(size_t mem_block_size );
39 
40  template <class T> T getAs()
41  {
42  if (!base_t::present())
43  throw std::runtime_error("Trying to access to an uninitialized memory block");
44 
45  if( base_t::operator ->()->empty() )
46  throw std::runtime_error("Trying to access to a memory block of size 0");
47 
48  return reinterpret_cast<T>( & base_t::operator ->()->operator [](0) );
49  }
50 
51  template <class T> T getAs() const
52  {
53  if (!base_t::present())
54  throw std::runtime_error("Trying to access to an uninitialized memory block");
55 
56  if( base_t::operator ->()->empty() )
57  throw std::runtime_error("Trying to access to a memory block of size 0");
58 
59  return reinterpret_cast<const T>( & base_t::operator ->()->operator [](0) );
60  }
61 
62  }; // End of class
63 
64  } // End of namespace
65 } // End of namespace
66 
67 #endif
Represents a memory block (via "void*") that can be shared between several objects through copy opera...
CReferencedMemBlock(size_t mem_block_size=0)
Constructor with an optional size of the memory block.
void resize(size_t mem_block_size)
Resize the shared memory block.
stlplus::smart_ptr< std::vector< char > > base_t
virtual ~CReferencedMemBlock()
Destructor, calls dereference_once.
EIGEN_STRONG_INLINE bool empty() const
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.1 for MRPT 1.4.0 SVN: at Sun Mar 7 18:43:46 UTC 2021