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