Main MRPT website > C++ reference
MRPT logo
smart_ptr.hpp
Go to the documentation of this file.
1 /*
2  The STL+ C++ Library Collection
3 
4  Website <http://stlplus.sourceforge.net/> Collection <index.html>
5 
6 
7  License Agreement
8 
9  <http://www.opensource.org/>
10 
11  * License for using the STLplus Library Collection <#license>
12  * The Intent of this License <#intent>
13  * How to Comply with this License <#compliance>
14  * Historical Note <#history>
15 
16 
17  License for using the STLplus Library Collection
18 
19  *© 1999-2008 Andy Rushton. All rights reserved.*
20 
21  Redistribution and use in source and binary forms, with or without
22  modification, are permitted provided that the following conditions are met:
23 
24  * Redistributions of source code must retain the above Copyright
25  notice, this list of conditions and the following disclaimer.
26  * Redistributions in binary form must reproduce the above Copyright
27  notice, this list of conditions and the following disclaimer in
28  the documentation and/or other materials provided with the
29  distribution.
30  * Neither the name of the STLplus library nor the names of its
31  contributors may be used to endorse or promote products derived
32  from this software without specific prior written permission.
33 
34  This software is provided by the Copyright holders and contributors "as
35  is" and any express or implied warranties, including, but not limited
36  to, the implied warranties of merchantability and fitness for a
37  particular purpose are disclaimed. In no event shall the Copyright owner
38  or contributors be liable for any direct, indirect, incidental, special,
39  exemplary, or consequential damages (including, but not limited to,
40  procurement of substitute goods or services; loss of use, data, or
41  profits; or business interruption) however caused and on any theory of
42  liability, whether in contract, strict liability, or tort (including
43  negligence or otherwise) arising in any way out of the use of this
44  software, even if advised of the possibility of such damage.
45 */
46 
47 /*
48  Modified version of STL+ sources shipped with the Mobile Robot
49  Programming Toolkit (MRPT).
50 
51  Sources have been modified to support thred-safe smart pointers
52  through atomic operations.
53 
54  2009, Jose Luis Blanco. University of Malaga.
55 */
56 
57 
58 #ifndef STLPLUS_SMART_PTR
59 #define STLPLUS_SMART_PTR
60 ////////////////////////////////////////////////////////////////////////////////
61 
62 // Author: Andy Rushton
63 // Copyright: (c) Andy Rushton, 2007
64 // License: BSD License, see ../docs/license.html
65 
66 // A smart pointer is a memory-managing pointer to an object. If you like, it
67 // is a zero-dimensional container.
68 
69 // Assignment of smart pointers result in multiple aliases of the same object.
70 // The term alias is used to differentiate from conventional pointers because
71 // the semantics are different.
72 
73 // Aliases can be turned into copies if the pointed-to class supports copying.
74 
75 // The base class is smart_ptr_base which defines the common interface. Then
76 // there are three subclasses which have the same interface but different copy
77 // semantics:
78 
79 // - smart_ptr for simple types and classes which have copy constructors
80 // - smart_ptr_clone for polymorphic class hierarchies which are copied using a clone method
81 // - smart_ptr_nocopy for any class that cannot or should not be copied
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 #include "containers_fixes.hpp"
85 #include "exceptions.hpp"
86 #include <map>
87 #include <string>
88 
89 #include <mrpt/synch/atomic_incr.h> // JLB: For atomic_count. See smart_ptr.tpp
90 
91 namespace stlplus
92 {
93 
94  ////////////////////////////////////////////////////////////////////////////////
95  // internals
96 
97  template<typename T,typename COUNTER> class smart_ptr_holder;
98 
99  ////////////////////////////////////////////////////////////////////////////////
100  // Base class
101  ////////////////////////////////////////////////////////////////////////////////
102 
103  template<typename T, typename C, typename COUNTER> // Typically: COUNTER = mrpt::synch::CAtomicCounter>
105  {
106  public:
107  //////////////////////////////////////////////////////////////////////////////
108  // member type definitions
109 
110  typedef T value_type;
111  typedef T& reference;
112  typedef const T& const_reference;
113  typedef C value_copy;
114 
115  //////////////////////////////////////////////////////////////////////////////
116  // constructors and destructors
117 
118  // create a null pointer
119  smart_ptr_base(void);
120 
121  // create a pointer containing a *copy* of the object using the template parameter C
122  // this copy is taken because the pointer class maintains a dynamically allocated object
123  // and the T& may not be (usually is not) dynamically allocated
124  explicit smart_ptr_base(const T& data) throw(illegal_copy);
125 
126  // create a pointer containing a dynamically created object
127  // Note: the object must be allocated *by the user* with new
128  // constructor form - must be called in the form smart_ptr_base<type> x(new type(args))
129  explicit smart_ptr_base(T* data);
130 
131  // copy constructor implements aliasing so no copy is made
132  explicit smart_ptr_base(const smart_ptr_base<T,C,COUNTER>& r);
133 
134  // destructor decrements the reference count and delete only when the last reference is destroyed
135  ~smart_ptr_base(void);
136 
137  //////////////////////////////////////////////////////////////////////////////
138  // logical tests to see if there is anything contained in the pointer since it can be null
139 
140  // there are two forms:explicit and implicit
141  // implicit: if(!r) or if(r)
142  // explicit: if(r.null()) or if(r.present())
143  operator bool(void) const;
144  bool operator!(void) const;
145  bool present(void) const;
146  bool null(void) const;
147 
148  //////////////////////////////////////////////////////////////////////////////
149  // dereference operators and functions
150 
151  // dereference the smart pointer to get the object - use in the form *p1
152  T& operator*(void) throw(null_dereference);
153  const T& operator*(void) const throw(null_dereference);
154 
155  // used as a prefix to a member access to the contained object e.g. p1->print() calls T::print()
156  T* operator->(void) throw(null_dereference);
157  const T* operator->(void) const throw(null_dereference);
158 
159  //////////////////////////////////////////////////////////////////////////////
160  // explicit function forms of the above assignment and dereference operators
161 
162  // set the value - note that this does a copy using the C template parameter
163  void set_value(const T& data) throw(illegal_copy);
164  // get the value
165  T& value(void) throw(null_dereference);
166  const T& value(void) const throw(null_dereference);
167 
168  // set the pointer
169  // deletes the previous pointer and adopts the passed pointer instead
170  // Note: the object must be allocated *by the user* with new
171  // Warning: it is very easy to break the memory management with this operation
172  void set(T* data = 0);
173  // get the pointer
174  T* pointer(void);
175  const T* pointer(void) const;
176 
177  //////////////////////////////////////////////////////////////////////////////
178  // functions to manage aliases
179 
180  // make this an alias of the passed object
181  inline void alias(const smart_ptr_base<T,C,COUNTER>&);
182 
183  // test whether two pointers point to the same object(known as aliasing the object)
184  // used in the form if(a.aliases(b))
185  inline bool aliases(const smart_ptr_base<T,C,COUNTER>&) const;
186 
187  // find the number of aliases - used when you need to know whether an
188  // object is still referred to from elsewhere (rare!)
189  inline unsigned alias_count(void) const;
190 
191  // delete the object and make the pointer null - does not make it unique
192  // first, so all other pointers to this will be null too
193  inline void clear(void);
194 
195  // make the pointer unique and null in one step - does not affect other
196  // pointers that were pointing to the same object
197  inline void clear_unique(void);
198 
199  //////////////////////////////////////////////////////////////////////////////
200  // functions that involve copying
201 
202  // these functions use the copy functor passed as the template parameter C
203  // to copy the object with the right copy semantics. If the copy functor
204  // is no_copy, an exception will be thrown.
205 
206  // make this pointer unique with respect to any other references to the same object
207  // if this pointer is already unique, it does nothing - otherwise it copies the object
208  inline void make_unique(void) throw(illegal_copy);
209 
210  // make this pointer a unique copy of the parameter
211  // useful for expressions like p1.copy(p2) which makes p1 a pointer to a unique copy of the contents of p2
212  void copy(const smart_ptr_base<T,C,COUNTER>&) throw(illegal_copy);
213 
214  protected:
216 
217  public:
218  // internal use only - had to make them public because they need to be
219  // accessed by routines that could not be made friends
220  void* handle(void) const;
221  void make_alias(void* handle);
222  };
223 
224  ////////////////////////////////////////////////////////////////////////////////
225  // copy functors implementing the three possible copy semantics
226 
227  // constructor_copy uses the copy constructor of the object - used for simple types
228 
229  template <typename T>
231  {
232  public:
233  T* operator() (const T& from) throw()
234  {
235  return new T(from);
236  }
237  };
238 
239  // clone_copy uses the clone method of the object - used for polymorphic types
240 
241  template <typename T>
243  {
244  public:
245  T* operator() (const T& from) throw()
246  {
247  return from.clone();
248  }
249  };
250 
251  // no_copy throws an exception - used for types that cannot be copied
252 
253  template <typename T>
254  class no_copy
255  {
256  public:
257  T* operator() (const T& from) throw(illegal_copy)
258  {
259  throw illegal_copy("no_copy functor called");
260  return 0;
261  }
262  };
263 
264  ////////////////////////////////////////////////////////////////////////////////
265  // smart_ptr for simple types and classes which have copy constructors
266 
267  template <typename T,typename COUNTER = mrpt::synch::CAtomicCounter>
268  class smart_ptr : public smart_ptr_base<T, constructor_copy<T>, COUNTER >
269  {
270  public:
271  smart_ptr(void) {}
272  explicit smart_ptr(const T& data) : smart_ptr_base<T, constructor_copy<T>,COUNTER >(data) {}
273  explicit smart_ptr(T* data) : smart_ptr_base<T, constructor_copy<T>,COUNTER >(data) {}
274  smart_ptr<T>& operator=(const T& data) {this->set_value(data); return *this;}
275  smart_ptr<T>& operator=(const smart_ptr<T>& r) {this->alias(r); return *this;}
276  ~smart_ptr(void) {}
277  };
278 
279  ////////////////////////////////////////////////////////////////////////////////
280  // smart_ptr_clone for polymorphic class hierarchies which have a clone method
281 
282  template <typename T,typename COUNTER = mrpt::synch::CAtomicCounter>
283  class smart_ptr_clone : public smart_ptr_base<T, clone_copy<T>, COUNTER >
284  {
285  public:
287  explicit smart_ptr_clone(const T& data) : smart_ptr_base<T, clone_copy<T>, COUNTER >(data) {}
288  explicit smart_ptr_clone(T* data) : smart_ptr_base<T, clone_copy<T>, COUNTER >(data) {}
289  smart_ptr_clone<T>& operator=(const T& data) {this->set_value(data); return *this;}
290  smart_ptr_clone<T>& operator=(const smart_ptr_clone<T>& r) {this->alias(r); return *this;}
292  };
293 
294  ////////////////////////////////////////////////////////////////////////////////
295  // smart_ptr_nocopy for any class that cannot or should not be copied
296 
297  template <typename T,typename COUNTER = mrpt::synch::CAtomicCounter>
298  class smart_ptr_nocopy : public smart_ptr_base<T, no_copy<T>, COUNTER >
299  {
300  public:
302  explicit smart_ptr_nocopy(const T& data) : smart_ptr_base<T, no_copy<T>, COUNTER >(data) {}
303  explicit smart_ptr_nocopy(T* data) : smart_ptr_base<T, no_copy<T>, COUNTER >(data) {}
304  smart_ptr_nocopy<T>& operator=(const T& data) {this->set_value(data); return *this;}
305  smart_ptr_nocopy<T>& operator=(const smart_ptr_nocopy<T>& r) {this->alias(r); return *this;}
307  };
308 
309  ////////////////////////////////////////////////////////////////////////////////
310 
311 } // end namespace stlplus
312 
313 #include "smart_ptr.tpp"
314 #endif



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