Main MRPT website > C++ reference
MRPT logo
safe_pointers.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef safe_pointers_H
29 #define safe_pointers_H
30 
31 #include <mrpt/config.h>
32 #include <mrpt/utils/utils_defs.h>
33 
34 /*---------------------------------------------------------------
35  Class
36  ---------------------------------------------------------------*/
37 namespace mrpt
38 {
39 namespace utils
40 {
41  /** A wrapper class for pointers that can be safely copied with "=" operator without problems.
42  * This class does not keep any reference count nor automatically destroy the pointed data.
43  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
44  */
45  template <class T>
47  {
48  protected:
49  T *ptr;
50 
51  public:
52  safe_ptr_basic() : ptr(NULL) { }
54  safe_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) { }
55  safe_ptr_basic<T> &operator =(T * p) { ptr = p; return *this; }
56 
58  {
59  ptr = o.ptr;
60  return *this;
61  }
62 
63  virtual ~safe_ptr_basic() { }
64 
65  bool operator == ( const T *o ) const { return o==ptr; }
66  bool operator == ( const safe_ptr_basic<T> &o )const { return o.ptr==ptr; }
67 
68  bool operator != ( const T *o )const { return o!=ptr; }
69  bool operator != ( const safe_ptr_basic<T> &o )const { return o.ptr!=ptr; }
70 
71  T*& get() { return ptr; }
72  const T* get()const { return ptr; }
73 
74  T *& operator ->() { ASSERT_(ptr); return ptr; }
75  const T * operator ->() const { ASSERT_(ptr); return ptr; }
76  };
77 
78  /** A wrapper class for pointers that can be safely copied with "=" operator without problems.
79  * This class does not keep any reference count nor automatically destroy the pointed data.
80  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
81  */
82  template <class T>
84  {
85  public:
86  safe_ptr() : safe_ptr_basic<T>() { }
87  safe_ptr(const safe_ptr<T> &o) : safe_ptr_basic<T>(o) { }
88  safe_ptr(const T* p) : safe_ptr_basic<T>(p) { }
89 
90  virtual ~safe_ptr() { }
91 
94 
95  T & operator [](const size_t &i) { ASSERT_(safe_ptr_basic<T>::ptr); return safe_ptr_basic<T>::ptr[i]; }
96  const T & operator [](const size_t &i) const { ASSERT_(safe_ptr_basic<T>::ptr); return safe_ptr_basic<T>::ptr[i]; }
97  };
98 
99 
100  /** A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtime if a copy is attempted.
101  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
102  */
103  template <class T>
105  {
106  protected:
107  T *ptr;
108 
109  public:
111  non_copiable_ptr_basic(const non_copiable_ptr_basic<T> &o) : ptr(NULL) { THROW_EXCEPTION("Pointer non-copiable..."); }
112  non_copiable_ptr_basic(const T* p) : ptr(const_cast<T*>(p)) { }
113  non_copiable_ptr_basic<T> &operator =(T * p) { ptr = p; return *this; }
114 
116  { THROW_EXCEPTION("Pointer non-copiable..."); }
117 
118  /** This method can change the pointer, since the change is made explicitly, not through copy operators transparent to the user. */
119  void set( const T* p ) { ptr = const_cast<T*>(p); }
120 
122 
123  bool operator == ( const T *o ) const { return o==ptr; }
124  bool operator == ( const non_copiable_ptr_basic<T> &o )const { return o.ptr==ptr; }
125 
126  bool operator != ( const T *o )const { return o!=ptr; }
127  bool operator != ( const non_copiable_ptr_basic<T> &o )const { return o.ptr!=ptr; }
128 
129  T*& get() { return ptr; }
130  const T* get()const { return ptr; }
131 
132  T** getPtrToPtr() { return &ptr; }
133 
134  T *& operator ->() { ASSERT_(ptr); return ptr; }
135  const T * operator ->() const { ASSERT_(ptr); return ptr; }
136  };
137 
138  /** A wrapper class for pointers that can NOT be copied with "=" operator, raising an exception at runtime if a copy is attempted.
139  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
140  */
141  template <class T>
143  {
144  public:
147  non_copiable_ptr(const T* p) : non_copiable_ptr_basic<T>(p) { }
148 
149  non_copiable_ptr<T> &operator =(const T* p) { non_copiable_ptr_basic<T>::ptr = const_cast<T*>(p); return *this; }
150 
152  { THROW_EXCEPTION("Pointer non-copiable..."); }
153 
154  virtual ~non_copiable_ptr() { }
155 
158 
160  const T & operator [](const size_t &i) const { ASSERT_(non_copiable_ptr_basic<T>::ptr); return non_copiable_ptr_basic<T>::ptr[i]; }
161  };
162 
163 
164  /** A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the copy.
165  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
166  */
167  template <class T>
169  {
170  protected:
171  T *ptr;
172 
173  public:
176 
177  copiable_NULL_ptr_basic<T> &operator =(T * p) { ptr=p; return *this; }
178 
180 
182 
183  bool operator == ( const T *o ) const { return o==ptr; }
184  bool operator == ( const copiable_NULL_ptr_basic<T> &o )const { return o.ptr==ptr; }
185 
186  bool operator != ( const T *o )const { return o!=ptr; }
187  bool operator != ( const copiable_NULL_ptr_basic<T> &o )const { return o.ptr!=ptr; }
188 
189  T*& get() { return ptr; }
190  const T*& get()const { return ptr; }
191 
192  T *& operator ->() { ASSERT_(ptr); return ptr; }
193  const T *& operator ->() const { ASSERT_(ptr); return ptr; }
194  };
195 
196  /** A wrapper class for pointers that, if copied with the "=" operator, should be set to NULL in the new copy.
197  * \sa CReferencedMemBlock, safe_ptr, non_copiable_ptr, copiable_NULL_ptr
198  */
199  template <class T>
201  {
202  public:
205 
207 
208  virtual ~copiable_NULL_ptr() { }
209 
212 
214  const T & operator [](const size_t &i) const { ASSERT_(copiable_NULL_ptr_basic<T>::ptr); return copiable_NULL_ptr_basic<T>::ptr[i]; }
215  };
216 
217 
218 
221 
222  } // End of namespace
223 } // End of namespace
224 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013