Main MRPT website > C++ reference for MRPT 1.9.9
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-2017, 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 <vector>
13 #include <memory>
14 
15 namespace mrpt
16 {
17 namespace utils
18 {
19 /** Represents a memory block (via "void*") that can be shared between several
20  * objects through copy operator (=).
21  * It keeps the reference count and only when it comes to zero, the memory
22  * block is really freed.
23  * Behaves like std::shared_ptr<>.
24  * \ingroup mrpt_base_grp
25  */
27 {
28  public:
29  /** Constructor with an optional size of the memory block */
30  CReferencedMemBlock(size_t mem_block_size = 0);
31 
32  /** Destructor, calls dereference_once. */
33  virtual ~CReferencedMemBlock();
34 
35  /** Resize the shared memory block. */
36  void resize(size_t mem_block_size);
37 
38  template <class T>
39  T* getAsPtr()
40  {
41  if (!m_data)
42  throw std::runtime_error(
43  "Trying to access to an uninitialized memory block");
44  if (m_data->empty())
45  throw std::runtime_error(
46  "Trying to access to a memory block of size 0");
47  return reinterpret_cast<T*>(&((*m_data)[0]));
48  }
49  template <class T>
50  const T* getAsPtr() const
51  {
52  if (!m_data)
53  throw std::runtime_error(
54  "Trying to access to an uninitialized memory block");
55  if (m_data->empty())
56  throw std::runtime_error(
57  "Trying to access to a memory block of size 0");
58  return reinterpret_cast<const T*>(&((*m_data)[0]));
59  }
60 
61  template <class T>
62  T& getAs()
63  {
64  return *getAsPtr<T>();
65  }
66  template <class T>
67  const T& getAs() const
68  {
69  return *getAsPtr<T>();
70  }
71 
72  unsigned int alias_count() const;
73  /** Frees the underlying memory block */
74  void clear();
75 
76  protected:
77  std::shared_ptr<std::vector<char>> m_data;
78 }; // End of class
79 
80 } // End of namespace
81 } // End of namespace
82 
83 #endif
Represents a memory block (via "void*") that can be shared between several objects through copy opera...
void resize(size_t mem_block_size)
Resize the shared memory block.
void clear()
Frees the underlying memory block.
std::shared_ptr< std::vector< char > > m_data
CReferencedMemBlock(size_t mem_block_size=0)
Constructor with an optional size of the memory block.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual ~CReferencedMemBlock()
Destructor, calls dereference_once.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019