Move the sources to trunk
[opencv] / otherlibs / _graphics / include / OpenEXR / ImfArray.h
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 //-------------------------------------------------------------------------
41 //
42 // class Array
43 // class Array2D
44 //
45 // "Arrays of T" whose sizes are not known at compile time.
46 // When an array goes out of scope, its elements are automatically
47 // deleted.
48 //
49 // Usage example:
50 //
51 //      struct C
52 //      {
53 //          C ()                {std::cout << "C::C  (" << this << ")\n";};
54 //          virtual ~C ()       {std::cout << "C::~C (" << this << ")\n";};
55 //      };
56 // 
57 //      int
58 //      main ()
59 //      {
60 //          Array <C> a(3);
61 // 
62 //          C &b = a[1];
63 //          const C &c = a[1];
64 //          C *d = a + 2;
65 //          const C *e = a;
66 // 
67 //          return 0;
68 //      }
69 //
70 //-------------------------------------------------------------------------
71
72 namespace Imf {
73
74
75 template <class T>
76 class Array
77 {
78   public:
79
80     //-----------------------------
81     // Constructors and destructors
82     //-----------------------------
83
84      Array ()                           {_data = 0;}
85      Array (long size)                  {_data = new T[size];}
86     ~Array ()                           {delete [] _data;}
87
88
89     //-----------------------------
90     // Access to the array elements
91     //-----------------------------
92
93     operator T * ()                     {return _data;}
94     operator const T * () const         {return _data;}
95
96
97     //------------------------------------------------------
98     // Resize and clear the array (the contents of the array
99     // are not preserved across the resize operation).
100     //
101     // resizeEraseUnsafe() is more memory efficient than
102     // resizeErase() because it deletes the old memory block
103     // before allocating a new one, but if allocating the
104     // new block throws an exception, resizeEraseUnsafe()
105     // leaves the array in an unusable state.
106     //
107     //------------------------------------------------------
108
109     void resizeErase (long size);
110     void resizeEraseUnsafe (long size);
111
112
113   private:
114
115     Array (const Array &);              // Copying and assignment
116     Array & operator = (const Array &); // are not implemented
117
118     T * _data;
119 };
120
121
122 template <class T>
123 class Array2D
124 {
125   public:
126
127     //-----------------------------
128     // Constructors and destructors
129     //-----------------------------
130
131      Array2D ();                        // empty array, 0 by 0 elements
132      Array2D (long sizeX, long sizeY);  // sizeX by sizeY elements
133     ~Array2D ();
134
135
136     //-----------------------------
137     // Access to the array elements
138     //-----------------------------
139
140     T *         operator [] (long x);
141     const T *   operator [] (long x) const;
142
143
144     //------------------------------------------------------
145     // Resize and clear the array (the contents of the array
146     // are not preserved across the resize operation).
147     //
148     // resizeEraseUnsafe() is more memory efficient than
149     // resizeErase() because it deletes the old memory block
150     // before allocating a new one, but if allocating the
151     // new block throws an exception, resizeEraseUnsafe()
152     // leaves the array in an unusable state.
153     //
154     //------------------------------------------------------
155
156     void resizeErase (long sizeX, long sizeY);
157     void resizeEraseUnsafe (long sizeX, long sizeY);
158
159
160   private:
161
162     Array2D (const Array2D &);                  // Copying and assignment
163     Array2D & operator = (const Array2D &);     // are not implemented
164
165     long        _sizeY;
166     T *         _data;
167 };
168
169
170 //---------------
171 // Implementation
172 //---------------
173
174 template <class T>
175 inline void
176 Array<T>::resizeErase (long size)
177 {
178     T *tmp = new T[size];
179     delete [] _data;
180     _data = tmp;
181 }
182
183
184 template <class T>
185 inline void
186 Array<T>::resizeEraseUnsafe (long size)
187 {
188     delete [] _data;
189     _data = 0;
190     _data = new T[size];
191 }
192
193
194 template <class T>
195 inline
196 Array2D<T>::Array2D ():
197     _sizeY (0), _data (0)
198 {
199     // emtpy
200 }
201
202
203 template <class T>
204 inline
205 Array2D<T>::Array2D (long sizeX, long sizeY):
206     _sizeY (sizeY), _data (new T[sizeX * sizeY])
207 {
208     // emtpy
209 }
210
211
212 template <class T>
213 inline
214 Array2D<T>::~Array2D ()
215 {
216     delete [] _data;
217 }
218
219
220 template <class T>
221 inline T *      
222 Array2D<T>::operator [] (long x)
223 {
224     return _data + x * _sizeY;
225 }
226
227
228 template <class T>
229 inline const T *
230 Array2D<T>::operator [] (long x) const
231 {
232     return _data + x * _sizeY;
233 }
234
235
236 template <class T>
237 inline void
238 Array2D<T>::resizeErase (long sizeX, long sizeY)
239 {
240     T *tmp = new T[sizeX * sizeY];
241     delete [] _data;
242     _sizeY = sizeY;
243     _data = tmp;
244 }
245
246
247 template <class T>
248 inline void
249 Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
250 {
251     delete [] _data;
252     _data = 0;
253     _sizeY = 0;
254     _data = new T[sizeX * sizeY];
255     _sizeY = sizeY;
256 }
257
258
259 } // namespace Imf
260
261 #endif