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-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 <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() {}
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  virtual ~safe_ptr() {}
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); }
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() {}
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() {}
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 
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() {}
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
mrpt::copiable_NULL_ptr::copiable_NULL_ptr
copiable_NULL_ptr()
Definition: safe_pointers.h:321
mrpt::ignored_copy_ptr::operator->
T *& operator->()
Definition: safe_pointers.h:247
mrpt::non_copiable_ptr_basic::operator=
non_copiable_ptr_basic< T > & operator=(const non_copiable_ptr_basic< T > &)
Definition: safe_pointers.h:125
mrpt::non_copiable_ptr_basic::operator->
const T * operator->() const
Definition: safe_pointers.h:154
mrpt::ignored_copy_ptr::get
T *& get()
Definition: safe_pointers.h:244
exceptions.h
mrpt::non_copiable_ptr_basic::ptr
T * ptr
Definition: safe_pointers.h:110
mrpt::safe_ptr::safe_ptr
safe_ptr(const safe_ptr< T > &o)
Definition: safe_pointers.h:75
mrpt::copiable_NULL_ptr_basic::ptr
T * ptr
Definition: safe_pointers.h:268
mrpt::safe_ptr_basic::operator==
bool operator==(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:46
mrpt::non_copiable_ptr::operator=
non_copiable_ptr< T > & operator=(const T *p)
Definition: safe_pointers.h:176
mrpt::copiable_NULL_ptr_basic::operator=
copiable_NULL_ptr_basic< T > & operator=(const copiable_NULL_ptr_basic< T > &)
Definition: safe_pointers.h:279
mrpt::ignored_copy_ptr::operator==
bool operator==(const ignored_copy_ptr< T > &o) const
Definition: safe_pointers.h:241
mrpt::non_copiable_ptr::non_copiable_ptr
non_copiable_ptr(const non_copiable_ptr< T > &o)
Definition: safe_pointers.h:171
mrpt::safe_ptr_basic::~safe_ptr_basic
virtual ~safe_ptr_basic()
Definition: safe_pointers.h:44
mrpt::non_copiable_ptr::operator[]
const T & operator[](const size_t &i) const
Definition: safe_pointers.h:204
mrpt::ignored_copy_ptr::ignored_copy_ptr
ignored_copy_ptr(const ignored_copy_ptr< T > &)
Definition: safe_pointers.h:227
mrpt::safe_ptr::operator[]
const T & operator[](const size_t &i) const
Definition: safe_pointers.h:94
mrpt::safe_ptr_basic::operator->
T *& operator->()
Definition: safe_pointers.h:51
mrpt::safe_ptr::~safe_ptr
virtual ~safe_ptr()
Definition: safe_pointers.h:77
mrpt::non_copiable_ptr
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
Definition: safe_pointers.h:167
mrpt::non_copiable_ptr::operator*
const T & operator*() const
Definition: safe_pointers.h:193
mrpt::non_copiable_ptr_basic::set
void set(const T *p)
This method can change the pointer, since the change is made explicitly, not through copy operators t...
Definition: safe_pointers.h:132
mrpt::ignored_copy_ptr::operator==
bool operator==(const T *o) const
Definition: safe_pointers.h:240
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::copiable_NULL_ptr_basic::operator->
T *& operator->()
Definition: safe_pointers.h:300
mrpt::ignored_copy_ptr::operator!=
bool operator!=(const ignored_copy_ptr< T > &o) const
Definition: safe_pointers.h:243
mrpt::safe_ptr_basic::operator!=
bool operator!=(const T *o) const
Definition: safe_pointers.h:47
mrpt::safe_ptr_basic::operator=
safe_ptr_basic< T > & operator=(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:38
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::safe_ptr_basic::operator!=
bool operator!=(const safe_ptr_basic< T > &o) const
Definition: safe_pointers.h:48
mrpt::copiable_NULL_ptr::operator=
copiable_NULL_ptr< T > & operator=(T *p)
Definition: safe_pointers.h:327
mrpt::non_copiable_ptr_basic::operator!=
bool operator!=(const T *o) const
Definition: safe_pointers.h:140
mrpt::copiable_NULL_ptr_basic::get
T *& get()
Definition: safe_pointers.h:298
mrpt::ignored_copy_ptr
A wrapper class for pointers whose copy operations from other objects of the same type are ignored,...
Definition: safe_pointers.h:220
mrpt::copiable_NULL_ptr_basic::get
const T *& get() const
Definition: safe_pointers.h:299
mrpt::safe_ptr_basic::operator=
safe_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:32
mrpt::non_copiable_ptr_basic::non_copiable_ptr_basic
non_copiable_ptr_basic()
Definition: safe_pointers.h:113
mrpt::copiable_NULL_ptr_basic::operator!=
bool operator!=(const T *o) const
Definition: safe_pointers.h:292
mrpt::non_copiable_ptr_basic::getPtrToPtr
T ** getPtrToPtr()
Definition: safe_pointers.h:148
mrpt::copiable_NULL_ptr_basic::operator=
copiable_NULL_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:273
mrpt::copiable_NULL_ptr_basic::operator!=
bool operator!=(const copiable_NULL_ptr_basic< T > &o) const
Definition: safe_pointers.h:293
mrpt::copiable_NULL_ptr
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the n...
Definition: safe_pointers.h:318
mrpt::non_copiable_ptr_basic::~non_copiable_ptr_basic
virtual ~non_copiable_ptr_basic()
Definition: safe_pointers.h:133
mrpt::copiable_NULL_ptr::copiable_NULL_ptr
copiable_NULL_ptr(const copiable_NULL_ptr< T > &o)
Definition: safe_pointers.h:322
mrpt::safe_ptr_basic::ptr
T * ptr
Definition: safe_pointers.h:26
mrpt::ignored_copy_ptr::get
const T * get() const
Definition: safe_pointers.h:245
mrpt::copiable_NULL_ptr::~copiable_NULL_ptr
virtual ~copiable_NULL_ptr()
Definition: safe_pointers.h:333
mrpt::safe_ptr_basic::get
T *& get()
Definition: safe_pointers.h:49
mrpt::safe_ptr_basic::safe_ptr_basic
safe_ptr_basic(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:30
mrpt::copiable_NULL_ptr::operator[]
T & operator[](const size_t &i)
Definition: safe_pointers.h:345
mrpt::ignored_copy_ptr::ignored_copy_ptr
ignored_copy_ptr()
Definition: safe_pointers.h:226
mrpt::non_copiable_ptr_basic::operator==
bool operator==(const non_copiable_ptr_basic< T > &o) const
Definition: safe_pointers.h:135
mrpt::ignored_copy_ptr::ignored_copy_ptr
ignored_copy_ptr(const T *p)
Definition: safe_pointers.h:228
mrpt::non_copiable_ptr::operator*
T & operator*()
Definition: safe_pointers.h:188
mrpt::non_copiable_ptr::~non_copiable_ptr
virtual ~non_copiable_ptr()
Definition: safe_pointers.h:187
mrpt::non_copiable_ptr_basic::non_copiable_ptr_basic
non_copiable_ptr_basic(const T *p)
Definition: safe_pointers.h:118
mrpt::safe_ptr::operator[]
T & operator[](const size_t &i)
Definition: safe_pointers.h:89
mrpt::ignored_copy_ptr::operator=
ignored_copy_ptr< T > & operator=(T *p)
Definition: safe_pointers.h:229
mrpt::non_copiable_ptr_basic::operator->
T *& operator->()
Definition: safe_pointers.h:149
mrpt::ignored_copy_ptr::operator=
ignored_copy_ptr< T > & operator=(const ignored_copy_ptr< T > &)
Definition: safe_pointers.h:235
mrpt::copiable_NULL_ptr::operator*
T & operator*()
Definition: safe_pointers.h:334
mrpt::copiable_NULL_ptr_basic
A wrapper class for pointers that, if copied with the "=" operator, should be set to nullptr in the c...
Definition: safe_pointers.h:265
mrpt::non_copiable_ptr_basic::non_copiable_ptr_basic
non_copiable_ptr_basic(const non_copiable_ptr_basic< T > &)
Definition: safe_pointers.h:114
mrpt::copiable_NULL_ptr_basic::copiable_NULL_ptr_basic
copiable_NULL_ptr_basic(const copiable_NULL_ptr_basic< T > &)
Definition: safe_pointers.h:272
mrpt::non_copiable_ptr_basic::operator==
bool operator==(const T *o) const
Definition: safe_pointers.h:134
mrpt::ignored_copy_ptr::getPtrToPtr
T ** getPtrToPtr()
Definition: safe_pointers.h:246
mrpt::safe_ptr_basic::get
const T * get() const
Definition: safe_pointers.h:50
mrpt::non_copiable_ptr::operator=
non_copiable_ptr< T > & operator=(const non_copiable_ptr< T > &)
Definition: safe_pointers.h:182
mrpt::ignored_copy_ptr::ptr
T * ptr
Definition: safe_pointers.h:223
mrpt::copiable_NULL_ptr_basic::operator==
bool operator==(const copiable_NULL_ptr_basic< T > &o) const
Definition: safe_pointers.h:287
mrpt::safe_ptr_basic::operator==
bool operator==(const T *o) const
Definition: safe_pointers.h:45
mrpt::non_copiable_ptr_basic::operator=
non_copiable_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:119
mrpt::non_copiable_ptr::non_copiable_ptr
non_copiable_ptr()
Definition: safe_pointers.h:170
mrpt::safe_ptr_basic::safe_ptr_basic
safe_ptr_basic()
Definition: safe_pointers.h:29
mrpt::copiable_NULL_ptr_basic::copiable_NULL_ptr_basic
copiable_NULL_ptr_basic()
Definition: safe_pointers.h:271
mrpt::non_copiable_ptr_basic::operator!=
bool operator!=(const non_copiable_ptr_basic< T > &o) const
Definition: safe_pointers.h:141
mrpt::ignored_copy_ptr::operator!=
bool operator!=(const T *o) const
Definition: safe_pointers.h:242
mrpt::safe_ptr_basic::operator->
const T * operator->() const
Definition: safe_pointers.h:56
mrpt::non_copiable_ptr::operator[]
T & operator[](const size_t &i)
Definition: safe_pointers.h:199
mrpt::safe_ptr_basic::safe_ptr_basic
safe_ptr_basic(const T *p)
Definition: safe_pointers.h:31
mrpt::copiable_NULL_ptr::operator*
const T & operator*() const
Definition: safe_pointers.h:339
mrpt::safe_ptr_basic
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:23
mrpt::ignored_copy_ptr::~ignored_copy_ptr
virtual ~ignored_copy_ptr()
Definition: safe_pointers.h:239
mrpt::safe_ptr::safe_ptr
safe_ptr(const T *p)
Definition: safe_pointers.h:76
mrpt::safe_ptr::operator*
T & operator*()
Definition: safe_pointers.h:78
mrpt::safe_ptr::operator*
const T & operator*() const
Definition: safe_pointers.h:83
mrpt::safe_ptr::safe_ptr
safe_ptr()
Definition: safe_pointers.h:74
mrpt::copiable_NULL_ptr::operator[]
const T & operator[](const size_t &i) const
Definition: safe_pointers.h:350
mrpt::copiable_NULL_ptr_basic::operator==
bool operator==(const T *o) const
Definition: safe_pointers.h:286
mrpt::non_copiable_ptr_basic::get
const T * get() const
Definition: safe_pointers.h:147
mrpt::copiable_NULL_ptr_basic::~copiable_NULL_ptr_basic
virtual ~copiable_NULL_ptr_basic()
Definition: safe_pointers.h:285
mrpt::non_copiable_ptr::non_copiable_ptr
non_copiable_ptr(const T *p)
Definition: safe_pointers.h:175
mrpt::safe_ptr
A wrapper class for pointers that can be safely copied with "=" operator without problems.
Definition: safe_pointers.h:71
mrpt::ignored_copy_ptr::operator->
const T * operator->() const
Definition: safe_pointers.h:252
mrpt::non_copiable_ptr_basic::get
T *& get()
Definition: safe_pointers.h:146
mrpt::ignored_copy_ptr::set
void set(const T *p)
This method can change the pointer, since the change is made explicitly, not through copy operators t...
Definition: safe_pointers.h:238
mrpt::non_copiable_ptr_basic
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
Definition: safe_pointers.h:107
mrpt::copiable_NULL_ptr_basic::operator->
const T *& operator->() const
Definition: safe_pointers.h:305



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST