MRPT  2.0.4
safe_pointers.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 <mrpt/core/exceptions.h> // ASSERT_()
12 
13 namespace mrpt
14 {
15 /** A wrapper class for pointers that can be safely copied with "=" operator
16  * without problems.
17  * This class does not keep any reference count nor automatically destroy the
18  * pointed data.
19  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
20  * \ingroup mrpt_core_grp
21  */
22 template <class T>
24 {
25  protected:
26  T* ptr;
27 
28  public:
29  safe_ptr_basic() : ptr(nullptr) {}
31  safe_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) {}
33  {
34  ptr = p;
35  return *this;
36  }
37 
39  {
40  ptr = o.ptr;
41  return *this;
42  }
43 
44  virtual ~safe_ptr_basic() = default;
45  bool operator==(const T* o) const { return o == ptr; }
46  bool operator==(const safe_ptr_basic<T>& o) const { return o.ptr == ptr; }
47  bool operator!=(const T* o) const { return o != ptr; }
48  bool operator!=(const safe_ptr_basic<T>& o) const { return o.ptr != ptr; }
49  T*& get() { return ptr; }
50  const T* get() const { return ptr; }
51  T*& operator->()
52  {
53  ASSERT_(ptr);
54  return ptr;
55  }
56  const T* operator->() const
57  {
58  ASSERT_(ptr);
59  return ptr;
60  }
61 };
62 
63 /** A wrapper class for pointers that can be safely copied with "=" operator
64  * without problems.
65  * This class does not keep any reference count nor automatically destroy the
66  * pointed data.
67  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
68  * \ingroup mrpt_core_grp
69  */
70 template <class T>
72 {
73  public:
75  safe_ptr(const safe_ptr<T>& o) : safe_ptr_basic<T>(o) {}
76  safe_ptr(const T* p) : safe_ptr_basic<T>(p) {}
78  {
80  return *this;
81  }
82  ~safe_ptr() override = default;
83  T& operator*()
84  {
86  return *safe_ptr_basic<T>::ptr;
87  }
88  const T& operator*() const
89  {
91  return *safe_ptr_basic<T>::ptr;
92  }
93 
94  T& operator[](size_t i)
95  {
97  return safe_ptr_basic<T>::ptr[i];
98  }
99  const T& operator[](size_t i) const
100  {
102  return safe_ptr_basic<T>::ptr[i];
103  }
104 };
105 
106 /** A wrapper class for pointers that can NOT be copied with "=" operator,
107  * raising an exception at runtime if a copy is attempted.
108  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
109  * \ingroup mrpt_core_grp
110  */
111 template <class T>
113 {
114  protected:
115  T* ptr;
116 
117  public:
118  non_copiable_ptr_basic() : ptr(nullptr) {}
120  {
121  THROW_EXCEPTION("Pointer non-copiable...");
122  }
123  non_copiable_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) {}
125  {
126  ptr = p;
127  return *this;
128  }
129 
131  {
132  THROW_EXCEPTION("Pointer non-copiable...");
133  }
134 
135  /** This method can change the pointer, since the change is made explicitly,
136  * not through copy operators transparent to the user. */
137  void set(const T* p) { ptr = const_cast<T*>(p); }
138  virtual ~non_copiable_ptr_basic() = default;
139  bool operator==(const T* o) const { return o == ptr; }
141  {
142  return o.ptr == ptr;
143  }
144 
145  bool operator!=(const T* o) const { return o != ptr; }
147  {
148  return o.ptr != ptr;
149  }
150 
151  T*& get() { return ptr; }
152  const T* get() const { return ptr; }
153  T** getPtrToPtr() { return &ptr; }
155  {
156  ASSERT_(ptr);
157  return ptr;
158  }
159  const T* operator->() const
160  {
161  ASSERT_(ptr);
162  return ptr;
163  }
164 };
165 
166 /** A wrapper class for pointers that can NOT be copied with "=" operator,
167  * raising an exception at runtime if a copy is attempted.
168  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
169  * \ingroup mrpt_core_grp
170  */
171 template <class T>
173 {
174  public:
177  : non_copiable_ptr_basic<T>(o)
178  {
179  }
182  {
183  non_copiable_ptr_basic<T>::ptr = const_cast<T*>(p);
184  return *this;
185  }
186 
188  {
189  THROW_EXCEPTION("Pointer non-copiable...");
190  }
191 
192  virtual ~non_copiable_ptr() = default;
194  {
197  }
198  const T& operator*() const
199  {
202  }
203 
204  T& operator[](size_t i)
205  {
208  }
209  const T& operator[](size_t i) const
210  {
213  }
214 };
215 
216 /** A wrapper class for pointers whose copy operations from other objects of the
217  * same type are ignored, that is, doing "a=b;" has no effect neiter on "a" or
218  * "b".
219  * In turn, assigning a pointer with a direct "=" operation from a plain "T*"
220  * type is permited.
221  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
222  * \ingroup mrpt_core_grp
223  */
224 template <class T>
226 {
227  protected:
228  T* ptr;
229 
230  public:
231  ignored_copy_ptr() : ptr(nullptr) {}
233  ignored_copy_ptr(const T* p) : ptr(const_cast<T*>(p)) {}
235  {
236  ptr = p;
237  return *this;
238  }
239 
241  /** This method can change the pointer, since the change is made explicitly,
242  * not through copy operators transparent to the user. */
243  void set(const T* p) { ptr = const_cast<T*>(p); }
244  virtual ~ignored_copy_ptr() = default;
245  bool operator==(const T* o) const { return o == ptr; }
246  bool operator==(const ignored_copy_ptr<T>& o) const { return o.ptr == ptr; }
247  bool operator!=(const T* o) const { return o != ptr; }
248  bool operator!=(const ignored_copy_ptr<T>& o) const { return o.ptr != ptr; }
249  T*& get() { return ptr; }
250  const T* get() const { return ptr; }
251  T** getPtrToPtr() { return &ptr; }
253  {
254  ASSERT_(ptr);
255  return ptr;
256  }
257  const T* operator->() const
258  {
259  ASSERT_(ptr);
260  return ptr;
261  }
262 };
263 
264 /** A wrapper class for pointers that, if copied with the "=" operator, should
265  * be set to nullptr in the copy.
266  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
267  * \ingroup mrpt_core_grp
268  */
269 template <class T>
271 {
272  protected:
273  T* ptr;
274 
275  public:
276  copiable_NULL_ptr_basic() : ptr(nullptr) {}
279  {
280  ptr = p;
281  return *this;
282  }
283 
285  {
286  ptr = nullptr;
287  return *this;
288  }
289 
290  virtual ~copiable_NULL_ptr_basic() = default;
291  bool operator==(const T* o) const { return o == ptr; }
293  {
294  return o.ptr == ptr;
295  }
296 
297  bool operator!=(const T* o) const { return o != ptr; }
299  {
300  return o.ptr != ptr;
301  }
302 
303  T*& get() { return ptr; }
304  const T*& get() const { return ptr; }
306  {
307  ASSERT_(ptr);
308  return ptr;
309  }
310  const T*& operator->() const
311  {
312  ASSERT_(ptr);
313  return ptr;
314  }
315 };
316 
317 /** A wrapper class for pointers that, if copied with the "=" operator, should
318  * be set to nullptr in the new copy.
319  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
320  * \ingroup mrpt_core_grp
321  */
322 template <class T>
324 {
325  public:
329  {
330  }
331 
333  {
335  return *this;
336  }
337 
338  virtual ~copiable_NULL_ptr() = default;
340  {
343  }
344  const T& operator*() const
345  {
348  }
349 
350  T& operator[](size_t i)
351  {
354  }
355  const T& operator[](size_t i) const
356  {
359  }
360 };
361 
364 
365 } // namespace mrpt
const T *& operator->() const
virtual ~non_copiable_ptr_basic()=default
bool operator!=(const T *o) const
Definition: safe_pointers.h:47
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
safe_ptr_basic< T > & operator=(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:38
safe_ptr_basic(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:30
non_copiable_ptr(const non_copiable_ptr< T > &o)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
copiable_NULL_ptr_basic(const copiable_NULL_ptr_basic< T > &)
bool operator==(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:46
safe_ptr(const safe_ptr< T > &o)
Definition: safe_pointers.h:75
ignored_copy_ptr(const T *p)
non_copiable_ptr_basic< T > & operator=(const non_copiable_ptr_basic< T > &)
copiable_NULL_ptr< T > & operator=(T *p)
const T & operator[](size_t i) const
T & operator[](size_t i)
bool operator==(const T *o) const
safe_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:32
bool operator==(const copiable_NULL_ptr_basic< T > &o) const
ignored_copy_ptr< T > & operator=(T *p)
non_copiable_ptr< T > & operator=(const T *p)
virtual ~ignored_copy_ptr()=default
T & operator[](size_t i)
Definition: safe_pointers.h:94
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
bool operator!=(const ignored_copy_ptr< T > &o) const
virtual ~copiable_NULL_ptr_basic()=default
bool operator!=(const T *o) const
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
const T & operator[](size_t i) const
Definition: safe_pointers.h:99
bool operator!=(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:48
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:23
A wrapper class for pointers whose copy operations from other objects of the same type are ignored...
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the c...
const T & operator*() const
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:71
virtual ~safe_ptr_basic()=default
const T * operator->() const
Definition: safe_pointers.h:56
const T & operator*() const
copiable_NULL_ptr(const copiable_NULL_ptr< T > &o)
non_copiable_ptr(const T *p)
T & operator[](size_t i)
const T & operator[](size_t i) const
bool operator==(const T *o) const
non_copiable_ptr< T > & operator=(const non_copiable_ptr< T > &)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool operator==(const T *o) const
Definition: safe_pointers.h:45
virtual ~copiable_NULL_ptr()=default
copiable_NULL_ptr_basic< T > & operator=(const copiable_NULL_ptr_basic< T > &)
non_copiable_ptr_basic< T > & operator=(T *p)
bool operator!=(const T *o) const
bool operator!=(const non_copiable_ptr_basic< T > &o) const
bool operator!=(const copiable_NULL_ptr_basic< T > &o) const
safe_ptr_basic(const T *p)
Definition: safe_pointers.h:31
bool operator==(const T *o) const
const T * operator->() const
const T * operator->() const
safe_ptr(const T *p)
Definition: safe_pointers.h:76
safe_ptr & operator=(T *p)
Definition: safe_pointers.h:77
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the n...
bool operator!=(const T *o) const
virtual ~non_copiable_ptr()=default
bool operator==(const ignored_copy_ptr< T > &o) const
ignored_copy_ptr(const ignored_copy_ptr< T > &)
bool operator==(const non_copiable_ptr_basic< T > &o) const
non_copiable_ptr_basic(const non_copiable_ptr_basic< T > &)
const T & operator*() const
Definition: safe_pointers.h:88
~safe_ptr() override=default
ignored_copy_ptr< T > & operator=(const ignored_copy_ptr< T > &)
copiable_NULL_ptr_basic< T > & operator=(T *p)



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020