Main MRPT website > C++ reference for MRPT 1.5.6
generic_copier_ptr.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 #pragma once
10 
11 #include <stdexcept>
12 #include <cstdlib>
13 
14 namespace mrpt
15 {
16  namespace utils
17  {
18  /** \addtogroup stlext_grp
19  * @{ */
20 
21  namespace internal
22  {
23  template <typename T>
24  struct CopyStatic
25  {
26  T* copy(const T* o)
27  {
28  if (!o) return NULL;
29  return new T(*o);
30  }
31  };
32 
33  template <typename T>
34  struct CopyCloner
35  {
36  T* copy(const T* o)
37  {
38  if (!o) return NULL;
39  T* n = dynamic_cast<T*>(o->clone());
40  if (!n) throw std::runtime_error("error: clone() returned unexpected type");
41  return n;
42  }
43  };
44 
45  template <typename T, typename Copier >
47  {
48  public:
49  typedef T value_type;
50  typedef Copier copier_t;
51  /** Ctor from a pointer; takes ownership. */
52  explicit generic_copier_ptr(T * ptr) : m_ptr(ptr) {}
53  /** Default ctor; init to nullptr. */
55  /** copy ctor: makes a copy of the object via `clone()` */
56  generic_copier_ptr(const generic_copier_ptr<T,Copier> & o) : m_ptr(Copier().copy(o.m_ptr)) {}
57  ~generic_copier_ptr() { if (m_ptr) delete m_ptr; }
58 
60  m_ptr=o.m_ptr;
61  o.m_ptr = NULL;
62  }
63 #if (__cplusplus>199711L)
64  /** move ctor */
66  m_ptr = o.m_ptr;
67  o.m_ptr = NULL;
68  }
69  /** move operator */
70  generic_copier_ptr<T, Copier> &operator =(const generic_copier_ptr<T, Copier> && o) {
71  if (this == &o) return *this;
72  m_ptr = o.m_ptr;
73  o.m_ptr = NULL;
74  return *this;
75  }
76 #endif
77 
78  T * operator->() {
79  if (m_ptr) return m_ptr;
80  else throw std::runtime_error("dereferencing NULL generic_copier_ptr");
81  }
82  const T * operator->() const {
83  if (m_ptr) return m_ptr;
84  else throw std::runtime_error("dereferencing NULL generic_copier_ptr");
85  }
86 
87  T& operator*(void) {
88  if (m_ptr) return *m_ptr;
89  else throw std::runtime_error("dereferencing NULL generic_copier_ptr");
90  }
91  const T& operator*(void) const {
92  if (m_ptr) return *m_ptr;
93  else throw std::runtime_error("dereferencing NULL generic_copier_ptr");
94  }
95 
96  T * get() { return m_ptr; }
97  const T * get() const { return m_ptr; }
98 
99  operator bool() const { return m_ptr != NULL; }
100  bool operator!(void) const { return m_ptr == NULL; }
101 
102  /** Releases the pointer (do not destroy the object) */
103  T * release() {
104  T *r = m_ptr;
105  m_ptr = NULL;
106  return r;
107  }
108 
109  void reset(T *ptr = NULL) {
110  if (ptr == m_ptr) return;
111  if (m_ptr) delete m_ptr;
112  m_ptr = ptr;
113  }
114  void resetDefaultCtor() { this->reset(new T()); }
115 
116  protected:
117  T * m_ptr;
118  };
119 
120  } // end NS internal
121 
122  /** @} */ // end of grouping
123  } // End of namespace
124 } // End of namespace
GLenum GLsizei n
Definition: glext.h:4618
generic_copier_ptr(const generic_copier_ptr< T, Copier > &o)
copy ctor: makes a copy of the object via clone()
generic_copier_ptr()
Default ctor; init to nullptr.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
T * release()
Releases the pointer (do not destroy the object)
generic_copier_ptr(T *ptr)
Ctor from a pointer; takes ownership.



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019