/usr/local/include/OpenEXR/ImfAttribute.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_ATTRIBUTE_H |
7 | | #define INCLUDED_IMF_ATTRIBUTE_H |
8 | | |
9 | | //----------------------------------------------------------------------------- |
10 | | // |
11 | | // class Attribute |
12 | | // |
13 | | //----------------------------------------------------------------------------- |
14 | | |
15 | | #include "ImfForward.h" |
16 | | |
17 | | #include "ImfIO.h" |
18 | | #include "ImfXdr.h" |
19 | | |
20 | | #include "IexBaseExc.h" |
21 | | |
22 | | #include <cstring> |
23 | | #include <typeinfo> |
24 | | |
25 | | #if defined(_MSC_VER) |
26 | | // suppress warning about non-exported base classes |
27 | | # pragma warning(push) |
28 | | # pragma warning(disable : 4251) |
29 | | # pragma warning(disable : 4275) |
30 | | #endif |
31 | | |
32 | | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER |
33 | | |
34 | | class IMF_EXPORT_TYPE Attribute |
35 | | { |
36 | | public: |
37 | | //--------------------------- |
38 | | // Constructor and destructor |
39 | | //--------------------------- |
40 | | |
41 | | IMF_EXPORT Attribute (); |
42 | | IMF_EXPORT virtual ~Attribute (); |
43 | | |
44 | | //------------------------------- |
45 | | // Get this attribute's type name |
46 | | //------------------------------- |
47 | | |
48 | | virtual const char* typeName () const = 0; |
49 | | |
50 | | //------------------------------ |
51 | | // Make a copy of this attribute |
52 | | //------------------------------ |
53 | | |
54 | | virtual Attribute* copy () const = 0; |
55 | | |
56 | | //---------------------------------------- |
57 | | // Type-specific attribute I/O and copying |
58 | | //---------------------------------------- |
59 | | |
60 | | virtual void writeValueTo ( |
61 | | OPENEXR_IMF_INTERNAL_NAMESPACE::OStream& os, int version) const = 0; |
62 | | |
63 | | virtual void readValueFrom ( |
64 | | OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int size, int version) = 0; |
65 | | |
66 | | virtual void copyValueFrom (const Attribute& other) = 0; |
67 | | |
68 | | //------------------ |
69 | | // Attribute factory |
70 | | //------------------ |
71 | | |
72 | | IMF_EXPORT static Attribute* newAttribute (const char typeName[]); |
73 | | |
74 | | //----------------------------------------------------------- |
75 | | // Test if a given attribute type has already been registered |
76 | | //----------------------------------------------------------- |
77 | | |
78 | | IMF_EXPORT static bool knownType (const char typeName[]); |
79 | | |
80 | | protected: |
81 | | //-------------------------------------------------- |
82 | | // Register an attribute type so that newAttribute() |
83 | | // knows how to make objects of this type. |
84 | | //-------------------------------------------------- |
85 | | IMF_EXPORT |
86 | | static void registerAttributeType ( |
87 | | const char typeName[], Attribute* (*newAttribute) ()); |
88 | | |
89 | | //------------------------------------------------------ |
90 | | // Un-register an attribute type so that newAttribute() |
91 | | // no longer knows how to make objects of this type (for |
92 | | // debugging only). |
93 | | //------------------------------------------------------ |
94 | | IMF_EXPORT |
95 | | static void unRegisterAttributeType (const char typeName[]); |
96 | | }; |
97 | | |
98 | | //------------------------------------------------- |
99 | | // Class template for attributes of a specific type |
100 | | //------------------------------------------------- |
101 | | |
102 | | template <class T> |
103 | | class IMF_EXPORT_TEMPLATE_TYPE TypedAttribute : public Attribute |
104 | | { |
105 | | public: |
106 | | //------------------------------------------------------------ |
107 | | // Constructors and destructor: default behavior. This assumes |
108 | | // that the type T is copyable/assignable/moveable. |
109 | | // |
110 | | // Notes: although the copy/move constructors and assignment operator |
111 | | // have default behavior, provide explicit implementations rather than |
112 | | // =default, since Windows mingw/msys2 clang fails to export implicitly |
113 | | // defined member functions. |
114 | | // ------------------------------------------------------------ |
115 | | |
116 | | TypedAttribute (); |
117 | | TypedAttribute (const T& value); |
118 | | TypedAttribute (const TypedAttribute<T>& other); |
119 | | TypedAttribute (TypedAttribute<T>&& other); |
120 | | virtual ~TypedAttribute (); |
121 | | |
122 | | TypedAttribute& operator= (const TypedAttribute<T>& other); |
123 | | TypedAttribute& operator= (TypedAttribute<T>&& other); |
124 | | |
125 | | //-------------------------------- |
126 | | // Access to the attribute's value |
127 | | //-------------------------------- |
128 | | |
129 | | T& value (); |
130 | | const T& value () const; |
131 | | |
132 | | //-------------------------------- |
133 | | // Get this attribute's type name. |
134 | | //-------------------------------- |
135 | | |
136 | | virtual const char* typeName () const; |
137 | | |
138 | | //--------------------------------------------------------- |
139 | | // Static version of typeName() |
140 | | // This function must be specialized for each value type T. |
141 | | //--------------------------------------------------------- |
142 | | |
143 | | static const char* staticTypeName (); |
144 | | |
145 | | //--------------------- |
146 | | // Make a new attribute |
147 | | //--------------------- |
148 | | |
149 | | static Attribute* makeNewAttribute (); |
150 | | |
151 | | //------------------------------ |
152 | | // Make a copy of this attribute |
153 | | //------------------------------ |
154 | | |
155 | | virtual Attribute* copy () const; |
156 | | |
157 | | //----------------------------------------------------------------- |
158 | | // Type-specific attribute I/O and copying. |
159 | | // Depending on type T, these functions may have to be specialized. |
160 | | //----------------------------------------------------------------- |
161 | | |
162 | | virtual void writeValueTo ( |
163 | | OPENEXR_IMF_INTERNAL_NAMESPACE::OStream& os, int version) const; |
164 | | |
165 | | virtual void readValueFrom ( |
166 | | OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int size, int version); |
167 | | |
168 | | virtual void copyValueFrom (const Attribute& other); |
169 | | |
170 | | //------------------------------------------------------------ |
171 | | // Dynamic casts that throw exceptions instead of returning 0. |
172 | | //------------------------------------------------------------ |
173 | | |
174 | | static TypedAttribute* cast (Attribute* attribute); |
175 | | static const TypedAttribute* cast (const Attribute* attribute); |
176 | | static TypedAttribute& cast (Attribute& attribute); |
177 | | static const TypedAttribute& cast (const Attribute& attribute); |
178 | | |
179 | | //--------------------------------------------------------------- |
180 | | // Register this attribute type so that Attribute::newAttribute() |
181 | | // knows how to make objects of this type. |
182 | | // |
183 | | // Note that this function is not thread-safe because it modifies |
184 | | // a global variable in the IlmIlm library. A thread in a multi- |
185 | | // threaded program may call registerAttributeType() only when no |
186 | | // other thread is accessing any functions or classes in the |
187 | | // OpenEXR library. |
188 | | // |
189 | | //--------------------------------------------------------------- |
190 | | |
191 | | static void registerAttributeType (); |
192 | | |
193 | | //----------------------------------------------------- |
194 | | // Un-register this attribute type (for debugging only) |
195 | | //----------------------------------------------------- |
196 | | |
197 | | static void unRegisterAttributeType (); |
198 | | |
199 | | private: |
200 | | T _value; |
201 | | }; |
202 | | |
203 | | //------------------------------------ |
204 | | // Implementation of TypedAttribute<T> |
205 | | //------------------------------------ |
206 | | |
207 | | template <class T> |
208 | | TypedAttribute<T>::TypedAttribute() |
209 | | : Attribute (), _value () |
210 | | { |
211 | | // empty |
212 | | } |
213 | | |
214 | | template <class T> |
215 | | TypedAttribute<T>::TypedAttribute (const T& value) |
216 | | : Attribute (), _value (value) |
217 | | { |
218 | | // empty |
219 | | } |
220 | | |
221 | | template <class T> |
222 | | TypedAttribute<T>::TypedAttribute (const TypedAttribute<T>& other) |
223 | | : Attribute (other), _value (other._value) |
224 | | { |
225 | | // empty |
226 | | } |
227 | | |
228 | | template <class T> |
229 | | TypedAttribute<T>::TypedAttribute (TypedAttribute<T>&& other) |
230 | | : Attribute (other), _value (std::move (other._value)) |
231 | | { |
232 | | // empty |
233 | | } |
234 | | |
235 | | template <class T> |
236 | | TypedAttribute<T>& |
237 | | TypedAttribute<T>::operator= (const TypedAttribute<T>& other) |
238 | | { |
239 | | if (this != &other) _value = other._value; |
240 | | return *this; |
241 | | } |
242 | | |
243 | | template <class T> |
244 | | TypedAttribute<T>& |
245 | | TypedAttribute<T>::operator= (TypedAttribute<T>&& other) |
246 | | { |
247 | | if (this != &other) _value = std::move (other._value); |
248 | | return *this; |
249 | | } |
250 | | |
251 | | template <class T> TypedAttribute<T>::~TypedAttribute () |
252 | | { |
253 | | // empty |
254 | | } |
255 | | |
256 | | template <class T> |
257 | | inline T& |
258 | | TypedAttribute<T>::value () |
259 | | { |
260 | | return _value; |
261 | | } |
262 | | |
263 | | template <class T> |
264 | | inline const T& |
265 | | TypedAttribute<T>::value () const |
266 | 5.11k | { |
267 | 5.11k | return _value; |
268 | 5.11k | } Imf_3_5::TypedAttribute<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::value() const Line | Count | Source | 266 | 605 | { | 267 | 605 | return _value; | 268 | 605 | } |
Imf_3_5::TypedAttribute<float>::value() const Line | Count | Source | 266 | 3.34k | { | 267 | 3.34k | return _value; | 268 | 3.34k | } |
Imf_3_5::TypedAttribute<Imf_3_5::Chromaticities>::value() const Line | Count | Source | 266 | 664 | { | 267 | 664 | return _value; | 268 | 664 | } |
Imf_3_5::TypedAttribute<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::value() const Line | Count | Source | 266 | 505 | { | 267 | 505 | return _value; | 268 | 505 | } |
|
269 | | |
270 | | template <class T> |
271 | | const char* |
272 | | TypedAttribute<T>::typeName () const |
273 | | { |
274 | | return staticTypeName (); |
275 | | } |
276 | | |
277 | | template <class T> |
278 | | Attribute* |
279 | | TypedAttribute<T>::makeNewAttribute () |
280 | | { |
281 | | return new TypedAttribute<T> (); |
282 | | } |
283 | | |
284 | | template <class T> |
285 | | Attribute* |
286 | | TypedAttribute<T>::copy () const |
287 | | { |
288 | | Attribute* attribute = new TypedAttribute<T> (); |
289 | | attribute->copyValueFrom (*this); |
290 | | return attribute; |
291 | | } |
292 | | |
293 | | template <class T> |
294 | | void |
295 | | TypedAttribute<T>::writeValueTo ( |
296 | | OPENEXR_IMF_INTERNAL_NAMESPACE::OStream& os, int version) const |
297 | | { |
298 | | OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write< |
299 | | OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _value); |
300 | | } |
301 | | |
302 | | template <class T> |
303 | | void |
304 | | TypedAttribute<T>::readValueFrom ( |
305 | | OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int size, int version) |
306 | | { |
307 | | OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read< |
308 | | OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _value); |
309 | | } |
310 | | |
311 | | template <class T> |
312 | | void |
313 | | TypedAttribute<T>::copyValueFrom (const Attribute& other) |
314 | | { |
315 | | _value = cast (other)._value; |
316 | | } |
317 | | |
318 | | template <class T> |
319 | | TypedAttribute<T>* |
320 | | TypedAttribute<T>::cast (Attribute* attribute) |
321 | | { |
322 | | TypedAttribute<T>* t = dynamic_cast<TypedAttribute<T>*> (attribute); |
323 | | |
324 | | if (t == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type."); |
325 | | |
326 | | return t; |
327 | | } |
328 | | |
329 | | template <class T> |
330 | | const TypedAttribute<T>* |
331 | | TypedAttribute<T>::cast (const Attribute* attribute) |
332 | | { |
333 | | const TypedAttribute<T>* t = |
334 | | dynamic_cast<const TypedAttribute<T>*> (attribute); |
335 | | |
336 | | if (t == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type."); |
337 | | |
338 | | return t; |
339 | | } |
340 | | |
341 | | template <class T> |
342 | | inline TypedAttribute<T>& |
343 | | TypedAttribute<T>::cast (Attribute& attribute) |
344 | | { |
345 | | return *cast (&attribute); |
346 | | } |
347 | | |
348 | | template <class T> |
349 | | inline const TypedAttribute<T>& |
350 | | TypedAttribute<T>::cast (const Attribute& attribute) |
351 | | { |
352 | | return *cast (&attribute); |
353 | | } |
354 | | |
355 | | template <class T> |
356 | | inline void |
357 | | TypedAttribute<T>::registerAttributeType () |
358 | | { |
359 | | Attribute::registerAttributeType (staticTypeName (), makeNewAttribute); |
360 | | } |
361 | | |
362 | | template <class T> |
363 | | inline void |
364 | | TypedAttribute<T>::unRegisterAttributeType () |
365 | | { |
366 | | Attribute::unRegisterAttributeType (staticTypeName ()); |
367 | | } |
368 | | |
369 | | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT |
370 | | |
371 | | #if defined(_MSC_VER) |
372 | | # pragma warning(pop) |
373 | | #endif |
374 | | |
375 | | #endif |