dng_auto_ptr.h revision 7841298310de58d0cddb212ac6295d9a27cf547c
1/*****************************************************************************/
2// Copyright 2006 Adobe Systems Incorporated
3// All Rights Reserved.
4//
5// NOTICE:  Adobe permits you to use, modify, and distribute this file in
6// accordance with the terms of the Adobe license agreement accompanying it.
7/*****************************************************************************/
8
9/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_auto_ptr.h#2 $ */
10/* $DateTime: 2012/07/11 10:36:56 $ */
11/* $Change: 838485 $ */
12/* $Author: tknoll $ */
13
14/** \file
15 * Class to implement std::auto_ptr like functionality even on platforms which do not
16 * have a full Standard C++ library.
17 */
18
19/*****************************************************************************/
20
21#ifndef __dng_auto_ptr__
22#define __dng_auto_ptr__
23
24#include "dng_memory.h"
25
26#include <memory>
27#include <stddef.h>
28#include <stdlib.h>
29
30/*****************************************************************************/
31
32// The following template has similar functionality to the STL auto_ptr, without
33// requiring all the weight of STL.
34
35/*****************************************************************************/
36
37/// \brief A class intended to be used in stack scope to hold a pointer from new. The
38/// held pointer will be deleted automatically if the scope is left without calling
39/// Release on the AutoPtr first.
40
41template<class T>
42class AutoPtr
43	{
44
45	private:
46
47		T *p_;
48
49	public:
50
51		/// Construct an AutoPtr with no referent.
52
53		AutoPtr () : p_ (0) { }
54
55		/// Construct an AutoPtr which owns the argument pointer.
56		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
57		/// deleted on destruction or Reset unless Release is called first.
58
59		explicit AutoPtr (T *p) :  p_( p ) { }
60
61		/// Reset is called on destruction.
62
63		~AutoPtr ();
64
65		/// Call Reset with a pointer from new. Uses T's default constructor.
66
67		void Alloc ();
68
69		/// Return the owned pointer of this AutoPtr, NULL if none. No change in
70		/// ownership or other effects occur.
71
72		T *Get () const { return p_; }
73
74		/// Return the owned pointer of this AutoPtr, NULL if none. The AutoPtr gives
75		/// up ownership and takes NULL as its value.
76
77		T *Release ();
78
79		/// If a pointer is owned, it is deleted. Ownership is taken of passed in
80		/// pointer.
81		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
82		/// deleted on destruction or Reset unless Release is called first.
83
84		void Reset (T *p);
85
86		/// If a pointer is owned, it is deleted and the AutoPtr takes NULL as its
87		/// value.
88
89		void Reset ();
90
91		/// Allows members of the owned pointer to be accessed directly. It is an
92		/// error to call this if the AutoPtr has NULL as its value.
93
94		T *operator-> () const { return p_; }
95
96		/// Returns a reference to the object that the owned pointer points to. It is
97		/// an error to call this if the AutoPtr has NULL as its value.
98
99		T &operator* () const { return *p_; }
100
101		/// Swap with another auto ptr.
102
103		friend inline void Swap (AutoPtr< T > &x, AutoPtr< T > &y)
104			{
105			T* temp = x.p_;
106			x.p_ = y.p_;
107			y.p_ = temp;
108			}
109
110	private:
111
112		// Hidden copy constructor and assignment operator. I don't think the STL
113		// "feature" of grabbing ownership of the pointer is a good idea.
114
115		AutoPtr (AutoPtr<T> &rhs);
116
117		AutoPtr<T> & operator= (AutoPtr<T> &rhs);
118
119	};
120
121/*****************************************************************************/
122
123template<class T>
124AutoPtr<T>::~AutoPtr ()
125	{
126
127	delete p_;
128	p_ = 0;
129
130	}
131
132/*****************************************************************************/
133
134template<class T>
135T *AutoPtr<T>::Release ()
136	{
137	T *result = p_;
138	p_ = 0;
139	return result;
140	}
141
142/*****************************************************************************/
143
144template<class T>
145void AutoPtr<T>::Reset (T *p)
146	{
147
148	if (p_ != p)
149		{
150		if (p_ != 0)
151			delete p_;
152		p_ = p;
153		}
154
155	}
156
157/*****************************************************************************/
158
159template<class T>
160void AutoPtr<T>::Reset ()
161	{
162
163	if (p_ != 0)
164		{
165		delete p_;
166		p_ = 0;
167		}
168
169	}
170
171/*****************************************************************************/
172
173template<class T>
174void AutoPtr<T>::Alloc ()
175	{
176	this->Reset (new T);
177	}
178
179/*****************************************************************************/
180
181/// \brief A class that provides a variable-length array that automatically
182/// deletes the underlying memory on scope exit.
183///
184/// T is not required to be movable. The class is implemented using
185/// dng_std_vector but purposely does not use any member functions that require
186/// T to be movable.
187
188template<typename T>
189class AutoArray
190	{
191
192	public:
193		/// Construct an AutoArray that refers to a null pointer.
194
195		AutoArray () { }
196
197		/// Construct an AutoArray containing 'count' elements, which are
198		/// default-constructed. If an out-of-memory condition occurs, a
199		/// dng_exception with error code dng_error_memory is thrown.
200
201		explicit AutoArray (size_t count)
202			: vector_(new dng_std_vector<T>(count))
203			{
204			}
205
206		/// Changes the size of the AutoArray to 'count' elements. The new
207		/// elements are default-constructed. The previously existing elements
208		/// of the array are destroyed. If an out-of-memory condition occurs, a
209		/// dng_exception with error code dng_error_memory is thrown.
210
211		void Reset (size_t count)
212			{
213			vector_.reset(new dng_std_vector<T>(count));
214			}
215
216		/// Allows indexing into the AutoArray. The index 'i' must be
217		/// non-negative and smaller than size of the array (the value that was
218		/// passed to the constructor or to Reset()).
219
220		T &operator[] (ptrdiff_t i)
221			{
222			return (*vector_) [i];
223			}
224		const T &operator[] (ptrdiff_t i) const
225			{
226			return (*vector_) [i];
227			}
228
229		/// Return a pointer to the beginning of the array.
230
231		T *Get ()
232			{
233			if (vector_)
234				return vector_->data();
235			else
236				return nullptr;
237			}
238		const T *Get () const
239			{
240			if (vector_)
241				return vector_->data();
242			else
243				return nullptr;
244			}
245
246	private:
247
248		// Hidden copy constructor and assignment operator.
249
250		AutoArray (const AutoArray &);
251
252		const AutoArray & operator= (const AutoArray &);
253
254	private:
255
256		std::unique_ptr<dng_std_vector<T> > vector_;
257
258	};
259
260/*****************************************************************************/
261
262#endif
263
264/*****************************************************************************/
265