summaryrefslogtreecommitdiff
path: root/gpr/source/lib/dng_sdk/dng_auto_ptr.h
blob: 02154bee65d4731c29d79721750ae4bfcf5d6d2b (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*****************************************************************************/
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/

/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_auto_ptr.h#2 $ */ 
/* $DateTime: 2012/07/11 10:36:56 $ */
/* $Change: 838485 $ */
/* $Author: tknoll $ */

/** \file
 * Class to implement std::auto_ptr like functionality even on platforms which do not
 * have a full Standard C++ library.
 */

/*****************************************************************************/

#ifndef __dng_auto_ptr__
#define __dng_auto_ptr__

#include <stddef.h>

/*****************************************************************************/

// The following template has similar functionality to the STL auto_ptr, without
// requiring all the weight of STL.

/*****************************************************************************/

/// \brief A class intended to be used in stack scope to hold a pointer from new. The
/// held pointer will be deleted automatically if the scope is left without calling
/// Release on the AutoPtr first.

template<class T>
class AutoPtr
	{
	
	private:
	
		T *p_;
		
	public:

		/// Construct an AutoPtr with no referent.

		AutoPtr () : p_ (0) { }
	
		/// Construct an AutoPtr which owns the argument pointer.
		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
		/// deleted on destruction or Reset unless Release is called first.

		explicit AutoPtr (T *p) :  p_( p ) { }

		/// Reset is called on destruction.

		~AutoPtr ();

		/// Call Reset with a pointer from new. Uses T's default constructor.

		void Alloc ();

		/// Return the owned pointer of this AutoPtr, NULL if none. No change in
		/// ownership or other effects occur.

		T *Get () const { return p_; }

		/// Return the owned pointer of this AutoPtr, NULL if none. The AutoPtr gives
		/// up ownership and takes NULL as its value.

		T *Release ();

		/// If a pointer is owned, it is deleted. Ownership is taken of passed in
		/// pointer.
		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
		/// deleted on destruction or Reset unless Release is called first.

		void Reset (T *p);

		/// If a pointer is owned, it is deleted and the AutoPtr takes NULL as its
		/// value.

		void Reset ();

		/// Allows members of the owned pointer to be accessed directly. It is an
		/// error to call this if the AutoPtr has NULL as its value.

		T *operator-> () const { return p_; }

		/// Returns a reference to the object that the owned pointer points to. It is
		/// an error to call this if the AutoPtr has NULL as its value.

		T &operator* () const { return *p_; }
		
		/// Swap with another auto ptr.
		
		friend inline void Swap (AutoPtr< T > &x, AutoPtr< T > &y)
			{
			T* temp = x.p_;
			x.p_ = y.p_;
			y.p_ = temp;
			}
		
	private:
	
		// Hidden copy constructor and assignment operator. I don't think the STL
		// "feature" of grabbing ownership of the pointer is a good idea.
	
		AutoPtr (AutoPtr<T> &rhs);

		AutoPtr<T> & operator= (AutoPtr<T> &rhs);
		
	};

/*****************************************************************************/

template<class T>
AutoPtr<T>::~AutoPtr ()
	{
	
	delete p_;
	p_ = 0;
	
	}

/*****************************************************************************/

template<class T>
T *AutoPtr<T>::Release ()
	{
	T *result = p_;
	p_ = 0;
	return result;
	}

/*****************************************************************************/

template<class T>
void AutoPtr<T>::Reset (T *p)
	{
	
	if (p_ != p)
		{
		if (p_ != 0)
			delete p_;
		p_ = p;
		}
	
	}

/*****************************************************************************/

template<class T>
void AutoPtr<T>::Reset ()
	{
	
	if (p_ != 0)
		{
		delete p_;
		p_ = 0;
		}
	
	}

/*****************************************************************************/

template<class T>
void AutoPtr<T>::Alloc ()
	{
	this->Reset (new T);
	}

/*****************************************************************************/

/// \brief A class intended to be used similarly to AutoPtr but for arrays.

template<typename T>
class AutoArray
	{

	public:

		/// Construct an AutoArray which owns the argument pointer.
		/// \param p array pointer which constructed AutoArray takes ownership of. p
		/// will be deleted on destruction or Reset unless Release is called first.

		explicit AutoArray (T *p_ = 0) : p (p_) { }

		/// Reset is called on destruction.

		~AutoArray ()
			{
			delete [] p;
			p = 0;
			}

		/// Return the owned array pointer of this AutoArray, NULL if none. The
		/// AutoArray gives up ownership and takes NULL as its value.

		T *Release ()
			{
			T *p_ = p;
			p = 0;
			return p_;
			}

		/// If a pointer is owned, it is deleted. Ownership is taken of passed in
		/// pointer.
		/// \param p array pointer which constructed AutoArray takes ownership of. p
		/// will be deleted on destruction or Reset unless Release is called first.

		void Reset (T *p_ = 0)
			{
			if (p != p_)
				{
				delete [] p;
				p = p_;
				}
			}

		/// Allows indexing into the AutoArray. It is an error to call this if the
		/// AutoArray has NULL as its value.

		T &operator[] (ptrdiff_t i) const
			{
			return p [i];
			}

		/// Return the owned pointer of this AutoArray, NULL if none. No change in
		/// ownership or other effects occur.

		T *Get () const
			{
			return p;
			}

	private:

		// Hidden copy constructor and assignment operator.

		AutoArray (const AutoArray &);

		const AutoArray & operator= (const AutoArray &);

	private:

		// Owned pointer or NULL.

		T *p;

	};

/*****************************************************************************/

#endif

/*****************************************************************************/