1 /*****************************************************************************/
2 // Copyright 2006-2019 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 /** \file
10  * Class to implement std::auto_ptr like functionality even on platforms which do not
11  * have a full Standard C++ library.
12  */
13 
14 /*****************************************************************************/
15 
16 #ifndef __dng_auto_ptr__
17 #define __dng_auto_ptr__
18 
19 #include <stddef.h>
20 
21 #include "dng_uncopyable.h"
22 
23 /*****************************************************************************/
24 
25 // The following template has similar functionality to the STL auto_ptr, without
26 // requiring all the weight of STL.
27 
28 /*****************************************************************************/
29 
30 /// \brief A class intended to be used in stack scope to hold a pointer from new. The
31 /// held pointer will be deleted automatically if the scope is left without calling
32 /// Release on the AutoPtr first.
33 
34 template<class T>
35 class AutoPtr: private dng_uncopyable
36 	{
37 
38 	private:
39 
40 		T *p_;
41 
42 	public:
43 
44 		/// Construct an AutoPtr with no referent.
45 
AutoPtr()46 		AutoPtr () : p_ (0) { }
47 
48 		/// Construct an AutoPtr which owns the argument pointer.
49 		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
50 		/// deleted on destruction or Reset unless Release is called first.
51 
AutoPtr(T * p)52 		explicit AutoPtr (T *p) :  p_( p ) { }
53 
54 		/// Reset is called on destruction.
55 
56 		~AutoPtr ();
57 
58 		/// Call Reset with a pointer from new. Uses T's default constructor.
59 
60 		void Alloc ();
61 
62 		/// Return the owned pointer of this AutoPtr, NULL if none. No change in
63 		/// ownership or other effects occur.
64 
Get()65 		T *Get () const { return p_; }
66 
67 		/// Return the owned pointer of this AutoPtr, NULL if none. The AutoPtr gives
68 		/// up ownership and takes NULL as its value.
69 
70 		T *Release ();
71 
72 		/// If a pointer is owned, it is deleted. Ownership is taken of passed in
73 		/// pointer.
74 		/// \param p pointer which constructed AutoPtr takes ownership of. p will be
75 		/// deleted on destruction or Reset unless Release is called first.
76 
77 		void Reset (T *p);
78 
79 		/// If a pointer is owned, it is deleted and the AutoPtr takes NULL as its
80 		/// value.
81 
82 		void Reset ();
83 
84 		/// Allows members of the owned pointer to be accessed directly. It is an
85 		/// error to call this if the AutoPtr has NULL as its value.
86 
87 		T *operator-> () const { return p_; }
88 
89 		/// Returns a reference to the object that the owned pointer points to. It is
90 		/// an error to call this if the AutoPtr has NULL as its value.
91 
92 		T &operator* () const { return *p_; }
93 
94 		/// Swap with another auto ptr.
95 
Swap(AutoPtr<T> & x,AutoPtr<T> & y)96 		friend inline void Swap (AutoPtr< T > &x, AutoPtr< T > &y)
97 			{
98 			T* temp = x.p_;
99 			x.p_ = y.p_;
100 			y.p_ = temp;
101 			}
102 
103 	};
104 
105 /*****************************************************************************/
106 
107 template<class T>
~AutoPtr()108 AutoPtr<T>::~AutoPtr ()
109 	{
110 
111 	delete p_;
112 	p_ = 0;
113 
114 	}
115 
116 /*****************************************************************************/
117 
118 template<class T>
Release()119 T *AutoPtr<T>::Release ()
120 	{
121 	T *result = p_;
122 	p_ = 0;
123 	return result;
124 	}
125 
126 /*****************************************************************************/
127 
128 template<class T>
Reset(T * p)129 void AutoPtr<T>::Reset (T *p)
130 	{
131 
132 	if (p_ != p)
133 		{
134 		if (p_ != 0)
135 			delete p_;
136 		p_ = p;
137 		}
138 
139 	}
140 
141 /*****************************************************************************/
142 
143 template<class T>
Reset()144 void AutoPtr<T>::Reset ()
145 	{
146 
147 	if (p_ != 0)
148 		{
149 		delete p_;
150 		p_ = 0;
151 		}
152 
153 	}
154 
155 /*****************************************************************************/
156 
157 template<class T>
Alloc()158 void AutoPtr<T>::Alloc ()
159 	{
160 	this->Reset (new T);
161 	}
162 
163 /*****************************************************************************/
164 
165 /// \brief A class intended to be used similarly to AutoPtr but for arrays.
166 
167 template<typename T>
168 class AutoArray: private dng_uncopyable
169 	{
170 
171 	public:
172 
173 		/// Construct an AutoArray which owns the argument pointer.
174 		/// \param p_ array pointer which constructed AutoArray takes ownership of. p_
175 		/// will be deleted on destruction or Reset unless Release is called first.
176 
p(p_)177 		explicit AutoArray (T *p_ = 0) : p (p_) { }
178 
179 		/// Reset is called on destruction.
180 
~AutoArray()181 		~AutoArray ()
182 			{
183 			delete [] p;
184 			p = 0;
185 			}
186 
187 		/// Return the owned array pointer of this AutoArray, NULL if none. The
188 		/// AutoArray gives up ownership and takes NULL as its value.
189 
Release()190 		T *Release ()
191 			{
192 			T *p_ = p;
193 			p = 0;
194 			return p_;
195 			}
196 
197 		/// If an array pointer is owned, it is deleted. Ownership is
198 		/// taken of the passed in pointer p_.
199 		/// \param p_ array pointer which constructed AutoArray takes ownership of. p_
200 		/// will be deleted on destruction or Reset unless Release is called first.
201 
202 		void Reset (T *p_ = 0)
203 			{
204 			if (p != p_)
205 				{
206 				delete [] p;
207 				p = p_;
208 				}
209 			}
210 
211 		/// Allows indexing into the AutoArray. It is an error to call this if the
212 		/// AutoArray has NULL as its value.
213 
214 		T &operator[] (ptrdiff_t i) const
215 			{
216 			return p [i];
217 			}
218 
219 		/// Return the owned pointer of this AutoArray, NULL if none. No change in
220 		/// ownership or other effects occur.
221 
Get()222 		T *Get () const
223 			{
224 			return p;
225 			}
226 
227 	private:
228 
229 		// Owned pointer or NULL.
230 
231 		T *p;
232 
233 	};
234 
235 /*****************************************************************************/
236 
237 #endif
238 
239 /*****************************************************************************/
240