summaryrefslogtreecommitdiffstats
path: root/common/smart_ptr.h
blob: dd023454e0f821b2a9edbf11da6adcb9688bb635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 *  smart_ptr.h
 *  elftosb
 *
 *  Created by Chris Reed on 4/18/06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */
#if !defined(_smart_ptr_h_)
#define _smart_ptr_h_

/*!
 * \brief Simple, standard smart pointer class.
 *
 * This class only supports the single-owner paradigm.
 */
template <typename T>
class smart_ptr
{
public:
	typedef T data_type;
	typedef T * ptr_type;
	typedef const T * const_ptr_type;
	typedef T & ref_type;
	typedef const T & const_ref_type;
	
	//! Default constuctor. Initialises with no pointer set.
	smart_ptr() : _p(0) {}
	
	//! This constructor takes a pointer to the object to be deleted.
	smart_ptr(ptr_type p) : _p(p) {}
	
	//! Destructor. If an object (pointer) has been set, it will be deleted.
	//! Deletes the object using safe_delete().
	virtual ~smart_ptr() { safe_delete(); }
	
	//! Return the current pointer value.
	ptr_type get() { return _p; }
	
	//! Return the const form of the current pointer value.
	const_ptr_type get() const { return _p; }
	
	//! Change the pointer value, or set if if the default constructor was used.
	//! If a pointer had previously been associated with the object, and \a p is
	//! different than that previous pointer, it will be deleted before taking
	//! ownership of \a p. If this is not desired, call reset() beforehand.
	void set(ptr_type p)
	{
		if (_p && p != _p)
		{
			safe_delete();
		}
		_p = p;
	}
	
	//! Dissociates any previously set pointer value without deleting it.
	void reset() { _p = 0; }
	
	//! Dissociates a previously set pointer value, deleting it at the same time.
	void clear() { safe_delete(); }
	
	//! Forces immediate deletion of the object. If you are planning on using
	//! this method, think about just using a normal pointer. It probably makes
	//! more sense.
	virtual void safe_delete()
	{
		if (_p)
		{
			delete _p;
			_p = 0;
		} 
	}
	
	//! \name Operators
	//@{
	
	//! Makes the object transparent as the template type.
	operator ptr_type () { return _p; }
	
	//! Const version of the pointer operator.
	operator const_ptr_type () const { return _p; }
	
	//! Makes the object transparent as a reference of the template type.
	operator ref_type () { return *_p; }
	
	//! Const version of the reference operator.
	operator const_ref_type () const { return *_p; }
	
	//! Returns a boolean indicating whether the object has a pointer set or not.
	operator bool () const { return _p != 0; }
	
	//! To allow setting the pointer directly. Equivalent to a call to set().
	smart_ptr<T> & operator = (const_ptr_type p)
	{
		set(const_cast<ptr_type>(p));
		return *this;
	}
	
	//! Another operator to allow you to treat the object just like a pointer.
	ptr_type operator ->() { return _p; }
	
	//! Another operator to allow you to treat the object just like a pointer.
	const_ptr_type operator ->() const { return _p; }
	
//	//! Pointer dereferencing operator.
//	ref_type operator * () const { return *_p; }
//	
//	//! Const version of the pointer dereference operator.
//	const_ref_type operator * () const { return *_p; }
	
	//@}

protected:
	ptr_type _p;	//!< The wrapped pointer.
};

/*!
 * \brief Simple, standard smart pointer class that uses the array delete operator.
 *
 * This class only supports the single-owner paradigm.
 *
 * This is almost entirely a copy of smart_ptr since the final C++ specification
 * does not allow template subclass members to access members  of the parent that
 * do not depend on the template parameter.
 */
template <typename T>
class smart_array_ptr
{
public:
	typedef T data_type;
	typedef T * ptr_type;
	typedef const T * const_ptr_type;
	typedef T & ref_type;
	typedef const T & const_ref_type;
	
	//! Default constuctor. Initialises with no pointer set.
	smart_array_ptr() : _p(0) {}
	
	//! This constructor takes a pointer to the object to be deleted.
	smart_array_ptr(ptr_type p) : _p(p) {}
	
	//! Destructor. If an array has been set, it will be deleted.
	//! Deletes the array using safe_delete().
	virtual ~smart_array_ptr() { safe_delete(); }
	
	//! Return the current pointer value.
	ptr_type get() { return _p; }
	
	//! Return the const form of the current pointer value.
	const_ptr_type get() const { return _p; }
	
	//! Change the pointer value, or set if if the default constructor was used.
	//! If a pointer had previously been associated with the object, and \a p is
	//! different than that previous pointer, it will be deleted before taking
	//! ownership of \a p. If this is not desired, call reset() beforehand.
	void set(ptr_type p)
	{
		if (_p && p != _p)
		{
			safe_delete();
		}
		_p = p;
	}
	
	//! Dissociates any previously set pointer value without deleting it.
	void reset() { _p = 0; }
	
	//! Dissociates a previously set pointer value, deleting it at the same time.
	void clear() { safe_delete(); }
	
	//! Forces immediate deletion of the object. If you are planning on using
	//! this method, think about just using a normal pointer. It probably makes
	//! more sense.
	virtual void safe_delete()
	{
		if (_p)
		{
			delete [] _p;
			_p = 0;
		} 
	}
	
	//! \name Operators
	//@{
	
	//! Makes the object transparent as the template type.
	operator ptr_type () { return _p; }
	
	//! Const version of the pointer operator.
	operator const_ptr_type () const { return _p; }
	
	//! Makes the object transparent as a reference of the template type.
	operator ref_type () { return *_p; }
	
	//! Const version of the reference operator.
	operator const_ref_type () const { return *_p; }
	
	//! Returns a boolean indicating whether the object has a pointer set or not.
	operator bool () const { return _p != 0; }
	
	//! To allow setting the pointer directly. Equivalent to a call to set().
	smart_array_ptr<T> & operator = (const_ptr_type p)
	{
		set(const_cast<ptr_type>(p));
		return *this;
	}
	
	//! Another operator to allow you to treat the object just like a pointer.
	ptr_type operator ->() { return _p; }
	
	//! Another operator to allow you to treat the object just like a pointer.
	const_ptr_type operator ->() const { return _p; }
	
	//! Indexing operator.
	ref_type operator [] (unsigned index) { return _p[index]; }
	
	//! Indexing operator.
	const_ref_type operator [] (unsigned index) const { return _p[index]; }
	
//	//! Pointer dereferencing operator.
//	ref_type operator * () const { return *_p; }
//	
//	//! Const version of the pointer dereference operator.
//	const_ref_type operator * () const { return *_p; }
	
	//@}

protected:
	ptr_type _p;	//!< The wrapped pointer.
};

#endif // _smart_ptr_h_