HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfArray.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 
7 #ifndef INCLUDED_IMF_ARRAY_H
8 #define INCLUDED_IMF_ARRAY_H
9 
10 #include "ImfForward.h"
11 
12 //-------------------------------------------------------------------------
13 //
14 // class Array
15 // class Array2D
16 //
17 // "Arrays of T" whose sizes are not known at compile time.
18 // When an array goes out of scope, its elements are automatically
19 // deleted.
20 //
21 // Usage example:
22 //
23 // struct C
24 // {
25 // C () {std::cout << "C::C (" << this << ")\n";};
26 // virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
27 // };
28 //
29 // int
30 // main ()
31 // {
32 // Array <C> a(3);
33 //
34 // C &b = a[1];
35 // const C &c = a[1];
36 // C *d = a + 2;
37 // const C *e = a;
38 //
39 // return 0;
40 // }
41 //
42 //-------------------------------------------------------------------------
43 
45 
46 template <class T>
48 {
49  public:
50 
51  //-----------------------------
52  // Constructors and destructors
53  //-----------------------------
54 
55  Array () {_data = 0; _size = 0;}
56  Array (long size) {_data = new T[size]; _size = size;}
57  ~Array () {delete [] _data;}
58 
59 
60  //-----------------------------
61  // Access to the array elements
62  //-----------------------------
63 
64  operator T * () {return _data;}
65  operator const T * () const {return _data;}
66 
67 
68  //------------------------------------------------------
69  // Resize and clear the array (the contents of the array
70  // are not preserved across the resize operation).
71  //
72  // resizeEraseUnsafe() is more memory efficient than
73  // resizeErase() because it deletes the old memory block
74  // before allocating a new one, but if allocating the
75  // new block throws an exception, resizeEraseUnsafe()
76  // leaves the array in an unusable state.
77  //
78  //------------------------------------------------------
79 
80  void resizeErase (long size);
81  void resizeEraseUnsafe (long size);
82 
83 
84  //-------------------------------
85  // Return the size of this array.
86  //-------------------------------
87 
88  long size() const {return _size;}
89 
90 
91  private:
92 
93  Array (const Array &) = delete;
94  Array & operator = (const Array &) = delete;
95  Array (Array &&) = delete;
96  Array & operator = (Array &&) = delete;
97 
98  long _size;
99  T * _data;
100 };
101 
102 
103 template <class T>
105 {
106  public:
107 
108  //-----------------------------
109  // Constructors and destructors
110  //-----------------------------
111 
112  Array2D (); // empty array, 0 by 0 elements
113  Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
114  ~Array2D ();
115 
116 
117  //-----------------------------
118  // Access to the array elements
119  //-----------------------------
120 
121  T * operator [] (long x);
122  const T * operator [] (long x) const;
123 
124 
125  //------------------------------------------------------
126  // Resize and clear the array (the contents of the array
127  // are not preserved across the resize operation).
128  //
129  // resizeEraseUnsafe() is more memory efficient than
130  // resizeErase() because it deletes the old memory block
131  // before allocating a new one, but if allocating the
132  // new block throws an exception, resizeEraseUnsafe()
133  // leaves the array in an unusable state.
134  //
135  //------------------------------------------------------
136 
137  void resizeErase (long sizeX, long sizeY);
138  void resizeEraseUnsafe (long sizeX, long sizeY);
139 
140 
141  //-------------------------------
142  // Return the size of this array.
143  //-------------------------------
144 
145  long height() const {return _sizeX;}
146  long width() const {return _sizeY;}
147 
148 
149  private:
150 
151  Array2D (const Array2D &) = delete;
152  Array2D & operator = (const Array2D &) = delete;
153  Array2D (Array2D &&) = delete;
154  Array2D & operator = (Array2D &&) = delete;
155 
156  long _sizeX;
157  long _sizeY;
158  T * _data;
159 };
160 
161 
162 //---------------
163 // Implementation
164 //---------------
165 
166 template <class T>
167 inline void
169 {
170  T *tmp = new T[size];
171  delete [] _data;
172  _size = size;
173  _data = tmp;
174 }
175 
176 
177 template <class T>
178 inline void
180 {
181  delete [] _data;
182  _data = 0;
183  _size = 0;
184  _data = new T[size];
185  _size = size;
186 }
187 
188 
189 template <class T>
190 inline
192  _sizeX(0), _sizeY (0), _data (0)
193 {
194  // emtpy
195 }
196 
197 
198 template <class T>
199 inline
200 Array2D<T>::Array2D (long sizeX, long sizeY):
201  _sizeX (sizeX), _sizeY (sizeY), _data (new T[sizeX * sizeY])
202 {
203  // emtpy
204 }
205 
206 
207 template <class T>
208 inline
210 {
211  delete [] _data;
212 }
213 
214 
215 template <class T>
216 inline T *
218 {
219  return _data + x * _sizeY;
220 }
221 
222 
223 template <class T>
224 inline const T *
226 {
227  return _data + x * _sizeY;
228 }
229 
230 
231 template <class T>
232 inline void
233 Array2D<T>::resizeErase (long sizeX, long sizeY)
234 {
235  T *tmp = new T[sizeX * sizeY];
236  delete [] _data;
237  _sizeX = sizeX;
238  _sizeY = sizeY;
239  _data = tmp;
240 }
241 
242 
243 template <class T>
244 inline void
245 Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
246 {
247  delete [] _data;
248  _data = 0;
249  _sizeX = 0;
250  _sizeY = 0;
251  _data = new T[sizeX * sizeY];
252  _sizeX = sizeX;
253  _sizeY = sizeY;
254 }
255 
257 
258 
259 #endif
T * operator[](long x)
Definition: ImfArray.h:217
Definition: ImfArray.h:47
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:80
long size() const
Definition: ImfArray.h:88
void resizeEraseUnsafe(long sizeX, long sizeY)
Definition: ImfArray.h:245
Array(long size)
Definition: ImfArray.h:56
long height() const
Definition: ImfArray.h:145
GLint GLenum GLint x
Definition: glcorearb.h:409
GLsizeiptr size
Definition: glcorearb.h:664
void resizeErase(long sizeX, long sizeY)
Definition: ImfArray.h:233
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER class IMF_EXPORT_TEMPLATE_TYPE Array
Definition: ImfForward.h:22
Array2D()
Definition: ImfArray.h:191
~Array2D()
Definition: ImfArray.h:209
~Array()
Definition: ImfArray.h:57
#define IMF_EXPORT_TEMPLATE_TYPE
Definition: ImfExport.h:58
Array()
Definition: ImfArray.h:55
long width() const
Definition: ImfArray.h:146
class IMF_EXPORT_TEMPLATE_TYPE Array2D
Definition: ImfForward.h:23
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:79
void resizeErase(long size)
Definition: ImfArray.h:168
void resizeEraseUnsafe(long size)
Definition: ImfArray.h:179