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() {return _size;}
119
120
121 private:
122
123 Array (const Array &); // Copying and assignment
124 Array & operator = (const Array &); // are not implemented
125
126 long _size;
127 T * _data;
128};
129
130
131template <class T>
132class Array2D
133{
134 public:
135
136 //-----------------------------
137 // Constructors and destructors
138 //-----------------------------
139
140 Array2D (); // empty array, 0 by 0 elements
141 Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
142 ~Array2D ();
143
144
145 //-----------------------------
146 // Access to the array elements
147 //-----------------------------
148
149 T * operator [] (long x);
150 const T * operator [] (long x) const;
151
152
153 //------------------------------------------------------
154 // Resize and clear the array (the contents of the array
155 // are not preserved across the resize operation).
156 //
157 // resizeEraseUnsafe() is more memory efficient than
158 // resizeErase() because it deletes the old memory block
159 // before allocating a new one, but if allocating the
160 // new block throws an exception, resizeEraseUnsafe()
161 // leaves the array in an unusable state.
162 //
163 //------------------------------------------------------
164
165 void resizeErase (long sizeX, long sizeY);
166 void resizeEraseUnsafe (long sizeX, long sizeY);
167
168
169 //-------------------------------
170 // Return the size of this array.
171 //-------------------------------
172
173 long height() {return _sizeX;}
174 long width() {return _sizeY;}
175
176
177 private:
178
179 Array2D (const Array2D &); // Copying and assignment
180 Array2D & operator = (const Array2D &); // are not implemented
181
182 long _sizeX;
183 long _sizeY;
184 T * _data;
185};
186
187
188//---------------
189// Implementation
190//---------------
191
192template <class T>
193inline void
194Array<T>::resizeErase (long size)
195{
196 T *tmp = new T[size];
197 delete [] _data;
198 _size = size;
199 _data = tmp;
200}
201
202
203template <class T>
204inline void
205Array<T>::resizeEraseUnsafe (long size)
206{
207 delete [] _data;
208 _data = 0;
209 _size = 0;
210 _data = new T[size];
211 _size = size;
212}
213
214
215template <class T>
216inline
217Array2D<T>::Array2D ():
218 _sizeX(0), _sizeY (0), _data (0)
219{
220 // emtpy
221}
222
223
224template <class T>
225inline
226Array2D<T>::Array2D (long sizeX, long sizeY):
227 _sizeX (sizeX), _sizeY (sizeY), _data (new T[sizeX * sizeY])
228{
229 // emtpy
230}
231
232
233template <class T>
234inline
235Array2D<T>::~Array2D ()
236{
237 delete [] _data;
238}
239
240
241template <class T>
242inline T *
243Array2D<T>::operator [] (long x)
244{
245 return _data + x * _sizeY;
246}
247
248
249template <class T>
250inline const T *
251Array2D<T>::operator [] (long x) const
252{
253 return _data + x * _sizeY;
254}
255
256
257template <class T>
258inline void
259Array2D<T>::resizeErase (long sizeX, long sizeY)
260{
261 T *tmp = new T[sizeX * sizeY];
262 delete [] _data;
263 _sizeX = sizeX;
264 _sizeY = sizeY;
265 _data = tmp;
266}
267
268
269template <class T>
270inline void
271Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
272{
273 delete [] _data;
274 _data = 0;
275 _sizeX = 0;
276 _sizeY = 0;
277 _data = new T[sizeX * sizeY];
278 _sizeX = sizeX;
279 _sizeY = sizeY;
280}
281
282OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
283
284
285#endif
286