5 * Created by Chris Reed on 4/18/06.
6 * Copyright 2006 __MyCompanyName__. All rights reserved.
9 #if !defined(_smart_ptr_h_)
13 * \brief Simple, standard smart pointer class.
15 * This class only supports the single-owner paradigm.
23 typedef const T * const_ptr_type;
25 typedef const T & const_ref_type;
27 //! Default constuctor. Initialises with no pointer set.
28 smart_ptr() : _p(0) {}
30 //! This constructor takes a pointer to the object to be deleted.
31 smart_ptr(ptr_type p) : _p(p) {}
33 //! Destructor. If an object (pointer) has been set, it will be deleted.
34 //! Deletes the object using safe_delete().
35 virtual ~smart_ptr() { safe_delete(); }
37 //! Return the current pointer value.
38 ptr_type get() { return _p; }
40 //! Return the const form of the current pointer value.
41 const_ptr_type get() const { return _p; }
43 //! Change the pointer value, or set if if the default constructor was used.
44 //! If a pointer had previously been associated with the object, and \a p is
45 //! different than that previous pointer, it will be deleted before taking
46 //! ownership of \a p. If this is not desired, call reset() beforehand.
56 //! Dissociates any previously set pointer value without deleting it.
57 void reset() { _p = 0; }
59 //! Dissociates a previously set pointer value, deleting it at the same time.
60 void clear() { safe_delete(); }
62 //! Forces immediate deletion of the object. If you are planning on using
63 //! this method, think about just using a normal pointer. It probably makes
65 virtual void safe_delete()
77 //! Makes the object transparent as the template type.
78 operator ptr_type () { return _p; }
80 //! Const version of the pointer operator.
81 operator const_ptr_type () const { return _p; }
83 //! Makes the object transparent as a reference of the template type.
84 operator ref_type () { return *_p; }
86 //! Const version of the reference operator.
87 operator const_ref_type () const { return *_p; }
89 //! Returns a boolean indicating whether the object has a pointer set or not.
90 operator bool () const { return _p != 0; }
92 //! To allow setting the pointer directly. Equivalent to a call to set().
93 smart_ptr<T> & operator = (const_ptr_type p)
95 set(const_cast<ptr_type>(p));
99 //! Another operator to allow you to treat the object just like a pointer.
100 ptr_type operator ->() { return _p; }
102 //! Another operator to allow you to treat the object just like a pointer.
103 const_ptr_type operator ->() const { return _p; }
105 // //! Pointer dereferencing operator.
106 // ref_type operator * () const { return *_p; }
108 // //! Const version of the pointer dereference operator.
109 // const_ref_type operator * () const { return *_p; }
114 ptr_type _p; //!< The wrapped pointer.
118 * \brief Simple, standard smart pointer class that uses the array delete operator.
120 * This class only supports the single-owner paradigm.
122 * This is almost entirely a copy of smart_ptr since the final C++ specification
123 * does not allow template subclass members to access members of the parent that
124 * do not depend on the template parameter.
126 template <typename T>
127 class smart_array_ptr
131 typedef T * ptr_type;
132 typedef const T * const_ptr_type;
133 typedef T & ref_type;
134 typedef const T & const_ref_type;
136 //! Default constuctor. Initialises with no pointer set.
137 smart_array_ptr() : _p(0) {}
139 //! This constructor takes a pointer to the object to be deleted.
140 smart_array_ptr(ptr_type p) : _p(p) {}
142 //! Destructor. If an array has been set, it will be deleted.
143 //! Deletes the array using safe_delete().
144 virtual ~smart_array_ptr() { safe_delete(); }
146 //! Return the current pointer value.
147 ptr_type get() { return _p; }
149 //! Return the const form of the current pointer value.
150 const_ptr_type get() const { return _p; }
152 //! Change the pointer value, or set if if the default constructor was used.
153 //! If a pointer had previously been associated with the object, and \a p is
154 //! different than that previous pointer, it will be deleted before taking
155 //! ownership of \a p. If this is not desired, call reset() beforehand.
165 //! Dissociates any previously set pointer value without deleting it.
166 void reset() { _p = 0; }
168 //! Dissociates a previously set pointer value, deleting it at the same time.
169 void clear() { safe_delete(); }
171 //! Forces immediate deletion of the object. If you are planning on using
172 //! this method, think about just using a normal pointer. It probably makes
174 virtual void safe_delete()
186 //! Makes the object transparent as the template type.
187 operator ptr_type () { return _p; }
189 //! Const version of the pointer operator.
190 operator const_ptr_type () const { return _p; }
192 //! Makes the object transparent as a reference of the template type.
193 operator ref_type () { return *_p; }
195 //! Const version of the reference operator.
196 operator const_ref_type () const { return *_p; }
198 //! Returns a boolean indicating whether the object has a pointer set or not.
199 operator bool () const { return _p != 0; }
201 //! To allow setting the pointer directly. Equivalent to a call to set().
202 smart_array_ptr<T> & operator = (const_ptr_type p)
204 set(const_cast<ptr_type>(p));
208 //! Another operator to allow you to treat the object just like a pointer.
209 ptr_type operator ->() { return _p; }
211 //! Another operator to allow you to treat the object just like a pointer.
212 const_ptr_type operator ->() const { return _p; }
214 //! Indexing operator.
215 ref_type operator [] (unsigned index) { return _p[index]; }
217 //! Indexing operator.
218 const_ref_type operator [] (unsigned index) const { return _p[index]; }
220 // //! Pointer dereferencing operator.
221 // ref_type operator * () const { return *_p; }
223 // //! Const version of the pointer dereference operator.
224 // const_ref_type operator * () const { return *_p; }
229 ptr_type _p; //!< The wrapped pointer.
232 #endif // _smart_ptr_h_