MRPT  1.9.9
deepcopy_ptr.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <cstdlib>
12 #include <stdexcept>
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 
66  /** move ctor */
68  {
69  m_ptr = o.m_ptr;
70  o.m_ptr = nullptr;
71  }
72  /** move operator */
74  {
75  if (this == &o) return *this;
76  m_ptr = o.m_ptr;
77  o.m_ptr = nullptr;
78  return *this;
79  }
80 
81  /** copy operator */
84  {
85  if (this == &o) return *this;
86  this->reset();
87  m_ptr = Copier().copy(o.m_ptr);
88  return *this;
89  }
90 
92  {
93  if (m_ptr)
94  return m_ptr;
95  else
96  throw std::runtime_error("dereferencing nullptr poly_ptr");
97  }
98  const T* operator->() const
99  {
100  if (m_ptr)
101  return m_ptr;
102  else
103  throw std::runtime_error("dereferencing nullptr poly_ptr");
104  }
105 
107  {
108  if (m_ptr)
109  return *m_ptr;
110  else
111  throw std::runtime_error("dereferencing nullptr poly_ptr");
112  }
113  const T& operator*() const
114  {
115  if (m_ptr)
116  return *m_ptr;
117  else
118  throw std::runtime_error("dereferencing nullptr poly_ptr");
119  }
120 
121  T* get() { return m_ptr; }
122  const T* get() const { return m_ptr; }
123  operator bool() const { return m_ptr != nullptr; }
124  bool operator!() const { return m_ptr == nullptr; }
125  /** Releases the pointer (do not destroy the object) */
126  T* release()
127  {
128  T* r = m_ptr;
129  m_ptr = nullptr;
130  return r;
131  }
132 
133  void reset(T* ptr = nullptr)
134  {
135  if (ptr == m_ptr) return;
136  if (m_ptr) delete m_ptr;
137  m_ptr = ptr;
138  }
139  void resetDefaultCtor() { this->reset(new T()); }
140 
141  protected:
142  T* m_ptr;
143 };
144 
145 } // namespace internal
146 
147 /** Smart pointer for polymorphic classes with a `clone()` method.
148  * No shared copies, that is, each `poly_ptr<T>` owns a unique instance of `T`.
149  * Copying a `poly_ptr<T>` invokes the copy operator for `T`.
150  * \sa copy_ptr<T>
151  */
152 template <typename T>
154 
155 /** Smart pointer for non-polymorphic classes.
156  * No shared copies, that is, each `copy_ptr<T>` owns a unique instance of `T`.
157  * Copying a `copy_ptr<T>` invokes the copy operator for `T`.
158  * \sa poly_ptr<T>
159  */
160 template <typename T>
162 
163 /** @} */ // end of grouping
164 } // namespace containers
165 } // namespace mrpt
generic_copier_ptr(generic_copier_ptr< T, Copier > &&o)
move ctor
Definition: deepcopy_ptr.h:67
generic_copier_ptr< T, Copier > & operator=(const generic_copier_ptr< T, Copier > &o)
copy operator
Definition: deepcopy_ptr.h:82
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:73
T * release()
Releases the pointer (do not destroy the object)
Definition: deepcopy_ptr.h:126
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020