MRPT  1.9.9
deepcopy_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-2018, 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 containers
17 {
18 /** \addtogroup mrpt_containers_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 nullptr;
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 nullptr;
39  T* n = dynamic_cast<T*>(o->clone());
40  if (!n)
41  throw std::runtime_error("error: clone() returned unexpected type");
42  return n;
43  }
44 };
45 
46 template <typename T, typename Copier>
48 {
49  public:
50  using value_type = T;
51  using copier_t = Copier;
52  /** Ctor from a pointer; takes ownership. */
53  explicit generic_copier_ptr(T* ptr) : m_ptr(ptr) {}
54  /** Default ctor; init to nullptr. */
55  generic_copier_ptr() : m_ptr(nullptr) {}
56  /** copy ctor: makes a copy of the object via `clone()` */
58  : m_ptr(Copier().copy(o.m_ptr))
59  {
60  }
62  {
63  if (m_ptr) delete m_ptr;
64  }
65 
67  {
68  m_ptr = o.m_ptr;
69  o.m_ptr = nullptr;
70  }
71  /** move ctor */
73  {
74  m_ptr = o.m_ptr;
75  o.m_ptr = nullptr;
76  }
77  /** move operator */
79  {
80  if (this == &o) return *this;
81  m_ptr = o.m_ptr;
82  o.m_ptr = nullptr;
83  return *this;
84  }
85 
86  /** copy operator */
89  {
90  if (this == &o) return *this;
91  this->reset();
92  m_ptr = Copier().copy(o.m_ptr);
93  return *this;
94  }
95 
97  {
98  if (m_ptr)
99  return m_ptr;
100  else
101  throw std::runtime_error("dereferencing nullptr poly_ptr");
102  }
103  const T* operator->() const
104  {
105  if (m_ptr)
106  return m_ptr;
107  else
108  throw std::runtime_error("dereferencing nullptr poly_ptr");
109  }
110 
111  T& operator*(void)
112  {
113  if (m_ptr)
114  return *m_ptr;
115  else
116  throw std::runtime_error("dereferencing nullptr poly_ptr");
117  }
118  const T& operator*(void)const
119  {
120  if (m_ptr)
121  return *m_ptr;
122  else
123  throw std::runtime_error("dereferencing nullptr poly_ptr");
124  }
125 
126  T* get() { return m_ptr; }
127  const T* get() const { return m_ptr; }
128  operator bool() const { return m_ptr != nullptr; }
129  bool operator!(void)const { return m_ptr == nullptr; }
130  /** Releases the pointer (do not destroy the object) */
131  T* release()
132  {
133  T* r = m_ptr;
134  m_ptr = nullptr;
135  return r;
136  }
137 
138  void reset(T* ptr = nullptr)
139  {
140  if (ptr == m_ptr) return;
141  if (m_ptr) delete m_ptr;
142  m_ptr = ptr;
143  }
144  void resetDefaultCtor() { this->reset(new T()); }
145  protected:
146  T* m_ptr;
147 };
148 
149 } // end NS internal
150 
151 /** Smart pointer for polymorphic classes with a `clone()` method.
152  * No shared copies, that is, each `poly_ptr<T>` owns a unique instance of `T`.
153  * Copying a `poly_ptr<T>` invokes the copy operator for `T`.
154  * \sa copy_ptr<T>
155  */
156 template <typename T>
158 
159 /** Smart pointer for non-polymorphic classes.
160 * No shared copies, that is, each `copy_ptr<T>` owns a unique instance of `T`.
161 * Copying a `copy_ptr<T>` invokes the copy operator for `T`.
162 * \sa poly_ptr<T>
163 */
164 template <typename T>
166 
167 /** @} */ // end of grouping
168 } // End of namespace
169 } // End of namespace
GLenum GLsizei n
Definition: glext.h:5074
generic_copier_ptr(generic_copier_ptr< T, Copier > &&o)
move ctor
Definition: deepcopy_ptr.h:72
generic_copier_ptr< T, Copier > & operator=(const generic_copier_ptr< T, Copier > &o)
copy operator
Definition: deepcopy_ptr.h:87
generic_copier_ptr(const generic_copier_ptr< T, Copier > &o)
copy ctor: makes a copy of the object via clone()
Definition: deepcopy_ptr.h:57
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
generic_copier_ptr< T, Copier > & operator=(generic_copier_ptr< T, Copier > &&o)
move operator
Definition: deepcopy_ptr.h:78
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
T * release()
Releases the pointer (do not destroy the object)
Definition: deepcopy_ptr.h:131
generic_copier_ptr()
Default ctor; init to nullptr.
Definition: deepcopy_ptr.h:55
generic_copier_ptr(T *ptr)
Ctor from a pointer; takes ownership.
Definition: deepcopy_ptr.h:53



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020