1///////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4// Digital Ltd. LLC
5//
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// * Neither the name of Industrial Light & Magic nor the names of
18// its contributors may be used to endorse or promote products derived
19// from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33///////////////////////////////////////////////////////////////////////////
34
35
36
37#ifndef INCLUDED_IMF_ARRAY_H
38#define INCLUDED_IMF_ARRAY_H
39
40#include "ImfForward.h"
41
42//-------------------------------------------------------------------------
43//
44// class Array
45// class Array2D
46//
47// "Arrays of T" whose sizes are not known at compile time.
48// When an array goes out of scope, its elements are automatically
49// deleted.
50//
51// Usage example:
52//
53// struct C
54// {
55// C () {std::cout << "C::C (" << this << ")\n";};
56// virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
57// };
58//
59// int
60// main ()
61// {
62// Array <C> a(3);
63//
64// C &b = a[1];
65// const C &c = a[1];
66// C *d = a + 2;
67// const C *e = a;
68//
69// return 0;
70// }
71//
72//-------------------------------------------------------------------------
73
74OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
75
76template <class T>
77class Array
78{
79 public:
80
81 //-----------------------------
82 // Constructors and destructors
83 //-----------------------------
84
85 Array () {_data = 0; _size = 0;}
86 Array (long size) {_data = new T[size]; _size = size;}
87 ~Array () {delete [] _data;}
88
89
90 //-----------------------------
91 // Access to the array elements
92 //-----------------------------
93
94 operator T * () {return _data;}
95 operator const T * () const {return _data;}
96
97
98 //------------------------------------------------------
99 // Resize and clear the array (the contents of the array
100 // are not preserved across the resize operation).
101 //
102 // resizeEraseUnsafe() is more memory efficient than
103 // resizeErase() because it deletes the old memory block
104 // before allocating a new one, but if allocating the
105 // new block throws an exception, resizeEraseUnsafe()
106 // leaves the array in an unusable state.
107 //
108 //------------------------------------------------------
109
110 void resizeErase (long size);
111 void resizeEraseUnsafe (long size);
112
113
114 //-------------------------------
115 // Return the size of this array.
116 //-------------------------------
117
118 long size() const {return _size;}
119
120
121 private:
122
123 Array (const Array &) = delete;
124 Array & operator = (const Array &) = delete;
125 Array (Array &&) = delete;
126 Array & operator = (Array &&) = delete;
127
128 long _size;
129 T * _data;
130};
131
132
133template <class T>
134class Array2D
135{
136 public:
137
138 //-----------------------------
139 // Constructors and destructors
140 //-----------------------------
141
142 Array2D (); // empty array, 0 by 0 elements
143 Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
144 ~Array2D ();
145
146
147 //-----------------------------
148 // Access to the array elements
149 //-----------------------------
150
151 T * operator [] (long x);
152 const T * operator [] (long x) const;
153
154
155 //------------------------------------------------------
156 // Resize and clear the array (the contents of the array
157 // are not preserved across the resize operation).
158 //
159 // resizeEraseUnsafe() is more memory efficient than
160 // resizeErase() because it deletes the old memory block
161 // before allocating a new one, but if allocating the
162 // new block throws an exception, resizeEraseUnsafe()
163 // leaves the array in an unusable state.
164 //
165 //------------------------------------------------------
166
167 void resizeErase (long sizeX, long sizeY);
168 void resizeEraseUnsafe (long sizeX, long sizeY);
169
170
171 //-------------------------------
172 // Return the size of this array.
173 //-------------------------------
174
175 long height() const {return _sizeX;}
176 long width() const {return _sizeY;}
177
178
179 private:
180
181 Array2D (const Array2D &) = delete;
182 Array2D & operator = (const Array2D &) = delete;
183 Array2D (Array2D &&) = delete;
184 Array2D & operator = (Array2D &&) = delete;
185
186 long _sizeX;
187 long _sizeY;
188 T * _data;
189};
190
191
192//---------------
193// Implementation
194//---------------
195
196template <class T>
197inline void
198Array<T>::resizeErase (long size)
199{
200 T *tmp = new T[size];
201 delete [] _data;
202 _size = size;
203 _data = tmp;
204}
205
206
207template <class T>
208inline void
209Array<T>::resizeEraseUnsafe (long size)
210{
211 delete [] _data;
212 _data = 0;
213 _size = 0;
214 _data = new T[size];
215 _size = size;
216}
217
218
219template <class T>
220inline
221Array2D<T>::Array2D ():
222 _sizeX(0), _sizeY (0), _data (0)
223{
224 // emtpy
225}
226
227
228template <class T>
229inline
230Array2D<T>::Array2D (long sizeX, long sizeY):
231 _sizeX (sizeX), _sizeY (sizeY), _data (new T[sizeX * sizeY])
232{
233 // emtpy
234}
235
236
237template <class T>
238inline
239Array2D<T>::~Array2D ()
240{
241 delete [] _data;
242}
243
244
245template <class T>
246inline T *
247Array2D<T>::operator [] (long x)
248{
249 return _data + x * _sizeY;
250}
251
252
253template <class T>
254inline const T *
255Array2D<T>::operator [] (long x) const
256{
257 return _data + x * _sizeY;
258}
259
260
261template <class T>
262inline void
263Array2D<T>::resizeErase (long sizeX, long sizeY)
264{
265 T *tmp = new T[sizeX * sizeY];
266 delete [] _data;
267 _sizeX = sizeX;
268 _sizeY = sizeY;
269 _data = tmp;
270}
271
272
273template <class T>
274inline void
275Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
276{
277 delete [] _data;
278 _data = 0;
279 _sizeX = 0;
280 _sizeY = 0;
281 _data = new T[sizeX * sizeY];
282 _sizeX = sizeX;
283 _sizeY = sizeY;
284}
285
286OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
287
288
289#endif
290

source code of include/OpenEXR/ImfArray.h