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