Main MRPT website > C++ reference for MRPT 1.5.7
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/boost_join.h>
14 #include <mrpt/utils/mrpt_macros.h> // ASSERT_()
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20  /** A wrapper class for pointers that can be safely copied with "=" operator without problems.
21  * This class does not keep any reference count nor automatically destroy the pointed data.
22  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
23  * \ingroup mrpt_base_grp
24  */
25  template <class T>
27  {
28  protected:
29  T *ptr;
30 
31  public:
32  safe_ptr_basic() : ptr(NULL) { }
34  safe_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) { }
35  safe_ptr_basic<T> &operator =(T * p) { ptr = p; return *this; }
36 
38  {
39  ptr = o.ptr;
40  return *this;
41  }
42 
43  virtual ~safe_ptr_basic() { }
44 
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 
48  bool operator != ( const T *o )const { return o!=ptr; }
49  bool operator != ( const safe_ptr_basic<T> &o )const { return o.ptr!=ptr; }
50 
51  T*& get() { return ptr; }
52  const T* get()const { return ptr; }
53 
54  T *& operator ->() { ASSERT_(ptr); return ptr; }
55  const T * operator ->() const { ASSERT_(ptr); return ptr; }
56  };
57 
58  /** A wrapper class for pointers that can be safely copied with "=" operator without problems.
59  * This class does not keep any reference count nor automatically destroy the pointed data.
60  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
61  * \ingroup mrpt_base_grp
62  */
63  template <class T>
65  {
66  public:
67  safe_ptr() : safe_ptr_basic<T>() { }
68  safe_ptr(const safe_ptr<T> &o) : safe_ptr_basic<T>(o) { }
69  safe_ptr(const T* p) : safe_ptr_basic<T>(p) { }
70 
71  virtual ~safe_ptr() { }
72 
75 
76  T & operator [](const size_t &i) { ASSERT_(safe_ptr_basic<T>::ptr); return safe_ptr_basic<T>::ptr[i]; }
77  const T & operator [](const size_t &i) const { ASSERT_(safe_ptr_basic<T>::ptr); return safe_ptr_basic<T>::ptr[i]; }
78  };
79 
80 
81  /** A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtime if a copy is attempted.
82  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
83  * \ingroup mrpt_base_grp
84  */
85  template <class T>
87  {
88  protected:
89  T *ptr;
90 
91  public:
93  non_copiable_ptr_basic(const non_copiable_ptr_basic<T> &) : ptr(NULL) { THROW_EXCEPTION("Pointer non-copiable..."); }
94  non_copiable_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) { }
95  non_copiable_ptr_basic<T> &operator =(T * p) { ptr = p; return *this; }
96 
98  { THROW_EXCEPTION("Pointer non-copiable..."); }
99 
100  /** This method can change the pointer, since the change is made explicitly, not through copy operators transparent to the user. */
101  void set( const T* p ) { ptr = const_cast<T*>(p); }
102 
104 
105  bool operator == ( const T *o ) const { return o==ptr; }
106  bool operator == ( const non_copiable_ptr_basic<T> &o )const { return o.ptr==ptr; }
107 
108  bool operator != ( const T *o )const { return o!=ptr; }
109  bool operator != ( const non_copiable_ptr_basic<T> &o )const { return o.ptr!=ptr; }
110 
111  T*& get() { return ptr; }
112  const T* get()const { return ptr; }
113 
114  T** getPtrToPtr() { return &ptr; }
115 
116  T *& operator ->() { ASSERT_(ptr); return ptr; }
117  const T * operator ->() const { ASSERT_(ptr); return ptr; }
118  };
119 
120  /** A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtime if a copy is attempted.
121  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
122  * \ingroup mrpt_base_grp
123  */
124  template <class T>
126  {
127  public:
131 
132  non_copiable_ptr<T> &operator =(const T* p) { non_copiable_ptr_basic<T>::ptr = const_cast<T*>(p); return *this; }
133 
135  { THROW_EXCEPTION("Pointer non-copiable..."); }
136 
137  virtual ~non_copiable_ptr() { }
138 
141 
143  const T & operator [](const size_t &i) const { ASSERT_(non_copiable_ptr_basic<T>::ptr); return non_copiable_ptr_basic<T>::ptr[i]; }
144  };
145 
146  /** A wrapper class for pointers whose copy operations from other objects of the same type are ignored, that is, doing "a=b;" has no effect neiter on "a" or "b".
147  * In turn, assigning a pointer with a direct "=" operation from a plain "T*" type is permited.
148  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
149  * \ingroup mrpt_base_grp
150  */
151  template <class T>
153  {
154  protected:
155  T *ptr;
156 
157  public:
158  ignored_copy_ptr() : ptr(NULL) { }
160  ignored_copy_ptr(const T* p) : ptr(const_cast<T*>(p)) { }
161  ignored_copy_ptr<T> &operator =(T * p) { ptr=p; return *this; }
162 
164 
165  /** This method can change the pointer, since the change is made explicitly, not through copy operators transparent to the user. */
166  void set( const T* p ) { ptr = const_cast<T*>(p); }
167 
168  virtual ~ignored_copy_ptr() { }
169 
170  bool operator == ( const T *o ) const { return o==ptr; }
171  bool operator == ( const ignored_copy_ptr<T> &o )const { return o.ptr==ptr; }
172 
173  bool operator != ( const T *o )const { return o!=ptr; }
174  bool operator != ( const ignored_copy_ptr<T> &o )const { return o.ptr!=ptr; }
175 
176  T*& get() { return ptr; }
177  const T* get()const { return ptr; }
178 
179  T** getPtrToPtr() { return &ptr; }
180 
181  T *& operator ->() { ASSERT_(ptr); return ptr; }
182  const T * operator ->() const { ASSERT_(ptr); return ptr; }
183  };
184 
185 
186  /** A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the copy.
187  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
188  * \ingroup mrpt_base_grp
189  */
190  template <class T>
192  {
193  protected:
194  T *ptr;
195 
196  public:
199 
200  copiable_NULL_ptr_basic<T> &operator =(T * p) { ptr=p; return *this; }
201 
203 
205 
206  bool operator == ( const T *o ) const { return o==ptr; }
207  bool operator == ( const copiable_NULL_ptr_basic<T> &o )const { return o.ptr==ptr; }
208 
209  bool operator != ( const T *o )const { return o!=ptr; }
210  bool operator != ( const copiable_NULL_ptr_basic<T> &o )const { return o.ptr!=ptr; }
211 
212  T*& get() { return ptr; }
213  const T*& get()const { return ptr; }
214 
215  T *& operator ->() { ASSERT_(ptr); return ptr; }
216  const T *& operator ->() const { ASSERT_(ptr); return ptr; }
217  };
218 
219  /** A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the new copy.
220  * \sa safe_ptr, non_copiable_ptr, copiable_NULL_ptr
221  * \ingroup mrpt_base_grp
222  */
223  template <class T>
225  {
226  public:
229 
231 
232  virtual ~copiable_NULL_ptr() { }
233 
236 
238  const T & operator [](const size_t &i) const { ASSERT_(copiable_NULL_ptr_basic<T>::ptr); return copiable_NULL_ptr_basic<T>::ptr[i]; }
239  };
240 
241 
242 
245 
246  } // End of namespace
247 } // End of namespace
248 #endif
ignored_copy_ptr< T > & operator=(T *p)
T & operator[](const size_t &i)
Definition: safe_pointers.h:76
bool operator!=(const T *o) const
Definition: safe_pointers.h:48
A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the copy...
A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtim...
Definition: safe_pointers.h:86
#define THROW_EXCEPTION(msg)
T & operator[](const size_t &i)
copiable_NULL_ptr(const copiable_NULL_ptr< T > &o)
ignored_copy_ptr(const ignored_copy_ptr< T > &)
bool operator!=(const T *o) const
bool operator==(const T *o) const
Definition: safe_pointers.h:45
non_copiable_ptr(const non_copiable_ptr< T > &o)
safe_ptr_basic(const safe_ptr_basic< T > &o)
Definition: safe_pointers.h:33
bool operator==(const T *o) const
bool operator!=(const T *o) const
copiable_NULL_ptr< T > & operator=(T *p)
non_copiable_ptr< T > & operator=(const T *p)
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:68
non_copiable_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:95
copiable_NULL_ptr_basic(const copiable_NULL_ptr_basic< T > &)
bool operator!=(const T *o) const
safe_ptr_basic< void > void_ptr
#define ASSERT_(f)
bool operator==(const T *o) const
A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the new ...
copiable_NULL_ptr_basic< T > & operator=(T *p)
bool operator==(const T *o) const
GLfloat GLfloat p
Definition: glext.h:5587
safe_ptr_basic< T > & operator=(T *p)
Definition: safe_pointers.h:35
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:26
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:64
non_copiable_ptr_basic(const non_copiable_ptr_basic< T > &)
Definition: safe_pointers.h:93



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019