Main MRPT website > C++ reference for MRPT 1.9.9
safe_pointers.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 #ifndef safe_pointers_H
10 #define safe_pointers_H
11 
12 #include <mrpt/config.h>
13 #include <mrpt/utils/mrpt_macros.h> // ASSERT_()
14 
15 namespace mrpt
16 {
17 namespace utils
18 {
19 /** A wrapper class for pointers that can be safely copied with "=" operator
20  * without problems.
21  * This class does not keep any reference count nor automatically destroy the
22  * pointed data.
23  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
24  * \ingroup mrpt_base_grp
25  */
26 template <class T>
28 {
29  protected:
30  T* ptr;
31 
32  public:
33  safe_ptr_basic() : ptr(nullptr) {}
35  safe_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) {}
37  {
38  ptr = p;
39  return *this;
40  }
41 
43  {
44  ptr = o.ptr;
45  return *this;
46  }
47 
48  virtual ~safe_ptr_basic() {}
49  bool operator==(const T* o) const { return o == ptr; }
50  bool operator==(const safe_ptr_basic<T>& o) const { return o.ptr == ptr; }
51  bool operator!=(const T* o) const { return o != ptr; }
52  bool operator!=(const safe_ptr_basic<T>& o) const { return o.ptr != ptr; }
53  T*& get() { return ptr; }
54  const T* get() const { return ptr; }
55  T*& operator->()
56  {
57  ASSERT_(ptr);
58  return ptr;
59  }
60  const T* operator->() const
61  {
62  ASSERT_(ptr);
63  return ptr;
64  }
65 };
66 
67 /** A wrapper class for pointers that can be safely copied with "=" operator
68  * without problems.
69  * This class does not keep any reference count nor automatically destroy the
70  * pointed data.
71  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
72  * \ingroup mrpt_base_grp
73  */
74 template <class T>
76 {
77  public:
79  safe_ptr(const safe_ptr<T>& o) : safe_ptr_basic<T>(o) {}
80  safe_ptr(const T* p) : safe_ptr_basic<T>(p) {}
81  virtual ~safe_ptr() {}
82  T& operator*()
83  {
85  return *safe_ptr_basic<T>::ptr;
86  }
87  const T& operator*() const
88  {
90  return *safe_ptr_basic<T>::ptr;
91  }
92 
93  T& operator[](const size_t& i)
94  {
96  return safe_ptr_basic<T>::ptr[i];
97  }
98  const T& operator[](const size_t& i) const
99  {
101  return safe_ptr_basic<T>::ptr[i];
102  }
103 };
104 
105 /** A wrapper class for pointers that can NOT be copied with "=" operator,
106  * raising an exception at runtime if a copy is attempted.
107  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
108  * \ingroup mrpt_base_grp
109  */
110 template <class T>
112 {
113  protected:
114  T* ptr;
115 
116  public:
117  non_copiable_ptr_basic() : ptr(nullptr) {}
119  {
120  THROW_EXCEPTION("Pointer non-copiable...");
121  }
122  non_copiable_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) {}
124  {
125  ptr = p;
126  return *this;
127  }
128 
130  {
131  THROW_EXCEPTION("Pointer non-copiable...");
132  }
133 
134  /** This method can change the pointer, since the change is made explicitly,
135  * not through copy operators transparent to the user. */
136  void set(const T* p) { ptr = const_cast<T*>(p); }
138  bool operator==(const T* o) const { return o == ptr; }
140  {
141  return o.ptr == ptr;
142  }
143 
144  bool operator!=(const T* o) const { return o != ptr; }
146  {
147  return o.ptr != ptr;
148  }
149 
150  T*& get() { return ptr; }
151  const T* get() const { return ptr; }
152  T** getPtrToPtr() { return &ptr; }
154  {
155  ASSERT_(ptr);
156  return ptr;
157  }
158  const T* operator->() const
159  {
160  ASSERT_(ptr);
161  return ptr;
162  }
163 };
164 
165 /** A wrapper class for pointers that can NOT be copied with "=" operator,
166  * raising an exception at runtime if a copy is attempted.
167  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
168  * \ingroup mrpt_base_grp
169  */
170 template <class T>
172 {
173  public:
176  : non_copiable_ptr_basic<T>(o)
177  {
178  }
181  {
182  non_copiable_ptr_basic<T>::ptr = const_cast<T*>(p);
183  return *this;
184  }
185 
187  {
188  THROW_EXCEPTION("Pointer non-copiable...");
189  }
190 
191  virtual ~non_copiable_ptr() {}
193  {
196  }
197  const T& operator*() const
198  {
201  }
202 
203  T& operator[](const size_t& i)
204  {
207  }
208  const T& operator[](const size_t& i) const
209  {
212  }
213 };
214 
215 /** A wrapper class for pointers whose copy operations from other objects of the
216  * same type are ignored, that is, doing "a=b;" has no effect neiter on "a" or
217  * "b".
218  * In turn, assigning a pointer with a direct "=" operation from a plain "T*"
219  * type is permited.
220  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
221  * \ingroup mrpt_base_grp
222  */
223 template <class T>
225 {
226  protected:
227  T* ptr;
228 
229  public:
230  ignored_copy_ptr() : ptr(nullptr) {}
232  ignored_copy_ptr(const T* p) : ptr(const_cast<T*>(p)) {}
234  {
235  ptr = p;
236  return *this;
237  }
238 
240  /** This method can change the pointer, since the change is made explicitly,
241  * not through copy operators transparent to the user. */
242  void set(const T* p) { ptr = const_cast<T*>(p); }
243  virtual ~ignored_copy_ptr() {}
244  bool operator==(const T* o) const { return o == ptr; }
245  bool operator==(const ignored_copy_ptr<T>& o) const { return o.ptr == ptr; }
246  bool operator!=(const T* o) const { return o != ptr; }
247  bool operator!=(const ignored_copy_ptr<T>& o) const { return o.ptr != ptr; }
248  T*& get() { return ptr; }
249  const T* get() const { return ptr; }
250  T** getPtrToPtr() { return &ptr; }
252  {
253  ASSERT_(ptr);
254  return ptr;
255  }
256  const T* operator->() const
257  {
258  ASSERT_(ptr);
259  return ptr;
260  }
261 };
262 
263 /** A wrapper class for pointers that, if copied with the "=" operator, should
264  * be set to nullptr in the copy.
265  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
266  * \ingroup mrpt_base_grp
267  */
268 template <class T>
270 {
271  protected:
272  T* ptr;
273 
274  public:
275  copiable_NULL_ptr_basic() : ptr(nullptr) {}
278  {
279  ptr = p;
280  return *this;
281  }
282 
284  {
285  ptr = nullptr;
286  return *this;
287  }
288 
290  bool operator==(const T* o) const { return o == ptr; }
292  {
293  return o.ptr == ptr;
294  }
295 
296  bool operator!=(const T* o) const { return o != ptr; }
298  {
299  return o.ptr != ptr;
300  }
301 
302  T*& get() { return ptr; }
303  const T*& get() const { return ptr; }
305  {
306  ASSERT_(ptr);
307  return ptr;
308  }
309  const T*& operator->() const
310  {
311  ASSERT_(ptr);
312  return ptr;
313  }
314 };
315 
316 /** A wrapper class for pointers that, if copied with the "=" operator, should
317  * be set to nullptr in the new copy.
318  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
319  * \ingroup mrpt_base_grp
320  */
321 template <class T>
323 {
324  public:
328  {
329  }
330 
332  {
334  return *this;
335  }
336 
337  virtual ~copiable_NULL_ptr() {}
339  {
342  }
343  const T& operator*() const
344  {
347  }
348 
349  T& operator[](const size_t& i)
350  {
353  }
354  const T& operator[](const size_t& i) const
355  {
358  }
359 };
360 
363 
364 } // End of namespace
365 } // End of namespace
366 #endif
bool operator==(const non_copiable_ptr_basic< T > &o) const
non_copiable_ptr_basic< T > & operator=(const non_copiable_ptr_basic< T > &)
ignored_copy_ptr< T > & operator=(T *p)
T & operator[](const size_t &i)
Definition: safe_pointers.h:93
bool operator!=(const T *o) const
Definition: safe_pointers.h:51
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the c...
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
bool operator!=(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:52
#define THROW_EXCEPTION(msg)
T & operator[](const size_t &i)
copiable_NULL_ptr(const copiable_NULL_ptr< T > &o)
const T & operator[](const size_t &i) const
ignored_copy_ptr< T > & operator=(const ignored_copy_ptr< T > &)
bool operator==(const ignored_copy_ptr< T > &o) const
ignored_copy_ptr(const ignored_copy_ptr< T > &)
bool operator!=(const T *o) const
non_copiable_ptr< T > & operator=(const non_copiable_ptr< T > &)
bool operator==(const T *o) const
Definition: safe_pointers.h:49
non_copiable_ptr(const non_copiable_ptr< T > &o)
bool operator==(const copiable_NULL_ptr_basic< T > &o) const
const T * operator->() const
safe_ptr_basic(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:34
bool operator==(const T *o) const
bool operator!=(const T *o) const
copiable_NULL_ptr< T > & operator=(T *p)
bool operator!=(const non_copiable_ptr_basic< T > &o) const
non_copiable_ptr< T > & operator=(const T *p)
const T & operator[](const size_t &i) const
Definition: safe_pointers.h:98
non_copiable_ptr_basic< void > void_ptr_noncopy
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
safe_ptr(const safe_ptr< T > &o)
Definition: safe_pointers.h:79
non_copiable_ptr_basic< T > & operator=(T *p)
copiable_NULL_ptr_basic(const copiable_NULL_ptr_basic< T > &)
bool operator!=(const T *o) const
const T * operator->() const
Definition: safe_pointers.h:60
safe_ptr_basic< void > void_ptr
#define ASSERT_(f)
bool operator==(const T *o) const
const T & operator*() const
Definition: safe_pointers.h:87
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the n...
bool operator!=(const copiable_NULL_ptr_basic< T > &o) const
copiable_NULL_ptr_basic< T > & operator=(const copiable_NULL_ptr_basic< T > &)
bool operator!=(const ignored_copy_ptr< T > &o) const
safe_ptr_basic< T > & operator=(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:42
copiable_NULL_ptr_basic< T > & operator=(T *p)
bool operator==(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:50
bool operator==(const T *o) const
GLfloat GLfloat p
Definition: glext.h:6305
safe_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:36
const T & operator[](const size_t &i) const
A wrapper class for pointers whose copy operations from other objects of the same type are ignored...
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:27
T & operator[](const size_t &i)
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:75
non_copiable_ptr_basic(const non_copiable_ptr_basic< T > &)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019