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



Page generated by Doxygen 1.8.14 for MRPT 1.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019