MRPT  1.9.9
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-2019, 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) {}
77  ~safe_ptr() override = default;
78  T& operator*()
79  {
81  return *safe_ptr_basic<T>::ptr;
82  }
83  const T& operator*() const
84  {
86  return *safe_ptr_basic<T>::ptr;
87  }
88 
89  T& operator[](const size_t& i)
90  {
92  return safe_ptr_basic<T>::ptr[i];
93  }
94  const T& operator[](const size_t& i) const
95  {
97  return safe_ptr_basic<T>::ptr[i];
98  }
99 };
100 
101 /** A wrapper class for pointers that can NOT be copied with "=" operator,
102  * raising an exception at runtime if a copy is attempted.
103  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
104  * \ingroup mrpt_core_grp
105  */
106 template <class T>
108 {
109  protected:
110  T* ptr;
111 
112  public:
113  non_copiable_ptr_basic() : ptr(nullptr) {}
115  {
116  THROW_EXCEPTION("Pointer non-copiable...");
117  }
118  non_copiable_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) {}
120  {
121  ptr = p;
122  return *this;
123  }
124 
126  {
127  THROW_EXCEPTION("Pointer non-copiable...");
128  }
129 
130  /** This method can change the pointer, since the change is made explicitly,
131  * not through copy operators transparent to the user. */
132  void set(const T* p) { ptr = const_cast<T*>(p); }
133  virtual ~non_copiable_ptr_basic() = default;
134  bool operator==(const T* o) const { return o == ptr; }
136  {
137  return o.ptr == ptr;
138  }
139 
140  bool operator!=(const T* o) const { return o != ptr; }
142  {
143  return o.ptr != ptr;
144  }
145 
146  T*& get() { return ptr; }
147  const T* get() const { return ptr; }
148  T** getPtrToPtr() { return &ptr; }
150  {
151  ASSERT_(ptr);
152  return ptr;
153  }
154  const T* operator->() const
155  {
156  ASSERT_(ptr);
157  return ptr;
158  }
159 };
160 
161 /** A wrapper class for pointers that can NOT be copied with "=" operator,
162  * raising an exception at runtime if a copy is attempted.
163  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
164  * \ingroup mrpt_core_grp
165  */
166 template <class T>
168 {
169  public:
172  : non_copiable_ptr_basic<T>(o)
173  {
174  }
177  {
178  non_copiable_ptr_basic<T>::ptr = const_cast<T*>(p);
179  return *this;
180  }
181 
183  {
184  THROW_EXCEPTION("Pointer non-copiable...");
185  }
186 
187  virtual ~non_copiable_ptr() = default;
189  {
192  }
193  const T& operator*() const
194  {
197  }
198 
199  T& operator[](const size_t& i)
200  {
203  }
204  const T& operator[](const size_t& i) const
205  {
208  }
209 };
210 
211 /** A wrapper class for pointers whose copy operations from other objects of the
212  * same type are ignored, that is, doing "a=b;" has no effect neiter on "a" or
213  * "b".
214  * In turn, assigning a pointer with a direct "=" operation from a plain "T*"
215  * type is permited.
216  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
217  * \ingroup mrpt_core_grp
218  */
219 template <class T>
221 {
222  protected:
223  T* ptr;
224 
225  public:
226  ignored_copy_ptr() : ptr(nullptr) {}
228  ignored_copy_ptr(const T* p) : ptr(const_cast<T*>(p)) {}
230  {
231  ptr = p;
232  return *this;
233  }
234 
236  /** This method can change the pointer, since the change is made explicitly,
237  * not through copy operators transparent to the user. */
238  void set(const T* p) { ptr = const_cast<T*>(p); }
239  virtual ~ignored_copy_ptr() = default;
240  bool operator==(const T* o) const { return o == ptr; }
241  bool operator==(const ignored_copy_ptr<T>& o) const { return o.ptr == ptr; }
242  bool operator!=(const T* o) const { return o != ptr; }
243  bool operator!=(const ignored_copy_ptr<T>& o) const { return o.ptr != ptr; }
244  T*& get() { return ptr; }
245  const T* get() const { return ptr; }
246  T** getPtrToPtr() { return &ptr; }
248  {
249  ASSERT_(ptr);
250  return ptr;
251  }
252  const T* operator->() const
253  {
254  ASSERT_(ptr);
255  return ptr;
256  }
257 };
258 
259 /** A wrapper class for pointers that, if copied with the "=" operator, should
260  * be set to nullptr in the copy.
261  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
262  * \ingroup mrpt_core_grp
263  */
264 template <class T>
266 {
267  protected:
268  T* ptr;
269 
270  public:
271  copiable_NULL_ptr_basic() : ptr(nullptr) {}
274  {
275  ptr = p;
276  return *this;
277  }
278 
280  {
281  ptr = nullptr;
282  return *this;
283  }
284 
285  virtual ~copiable_NULL_ptr_basic() = default;
286  bool operator==(const T* o) const { return o == ptr; }
288  {
289  return o.ptr == ptr;
290  }
291 
292  bool operator!=(const T* o) const { return o != ptr; }
294  {
295  return o.ptr != ptr;
296  }
297 
298  T*& get() { return ptr; }
299  const T*& get() const { return ptr; }
301  {
302  ASSERT_(ptr);
303  return ptr;
304  }
305  const T*& operator->() const
306  {
307  ASSERT_(ptr);
308  return ptr;
309  }
310 };
311 
312 /** A wrapper class for pointers that, if copied with the "=" operator, should
313  * be set to nullptr in the new copy.
314  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
315  * \ingroup mrpt_core_grp
316  */
317 template <class T>
319 {
320  public:
324  {
325  }
326 
328  {
330  return *this;
331  }
332 
333  virtual ~copiable_NULL_ptr() = default;
335  {
338  }
339  const T& operator*() const
340  {
343  }
344 
345  T& operator[](const size_t& i)
346  {
349  }
350  const T& operator[](const size_t& i) const
351  {
354  }
355 };
356 
359 
360 } // 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)
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[](const size_t &i)
const T & operator[](const size_t &i) const
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
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)
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
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
T & operator[](const size_t &i)
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 > &)
GLfloat GLfloat p
Definition: glext.h:6398
const T & operator*() const
Definition: safe_pointers.h:83
~safe_ptr() override=default
const T & operator[](const size_t &i) const
ignored_copy_ptr< T > & operator=(const ignored_copy_ptr< T > &)
const T & operator[](const size_t &i) const
copiable_NULL_ptr_basic< T > & operator=(T *p)
T & operator[](const size_t &i)
Definition: safe_pointers.h:89



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019