Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_auto_ptr.h

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