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 #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 
33 
35 {
36 public:
37  //---------------------------
38  // Constructor and destructor
39  //---------------------------
40 
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 (
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  //--------------------------------------------------
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  //------------------------------------------------------
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>
104 {
105 public:
106  //------------------------------------------------------------
107  // Constructors and destructor: default behavior. This assumes
108  // that the type T is copyable/assignable/moveable.
109  //------------------------------------------------------------
110 
111  TypedAttribute () = default;
112  TypedAttribute (const T& value);
113  TypedAttribute (const TypedAttribute<T>& other) = default;
114  TypedAttribute (TypedAttribute<T>&& other) = default;
115  //NB: if we use a default destructor, it wreaks havoc with where the vtable and such end up
116  //at least under mingw+windows, and since we are providing extern template instantiations
117  //this will be pretty trim and should reduce code bloat
118  virtual ~TypedAttribute ();
119 
120  TypedAttribute& operator= (const TypedAttribute<T>& other) = default;
121  TypedAttribute& operator= (TypedAttribute<T>&& other) = default;
122 
123  //--------------------------------
124  // Access to the attribute's value
125  //--------------------------------
126 
127  T& value ();
128  const T& value () const;
129 
130  //--------------------------------
131  // Get this attribute's type name.
132  //--------------------------------
133 
134  virtual const char* typeName () const;
135 
136  //---------------------------------------------------------
137  // Static version of typeName()
138  // This function must be specialized for each value type T.
139  //---------------------------------------------------------
140 
141  static const char* staticTypeName ();
142 
143  //---------------------
144  // Make a new attribute
145  //---------------------
146 
147  static Attribute* makeNewAttribute ();
148 
149  //------------------------------
150  // Make a copy of this attribute
151  //------------------------------
152 
153  virtual Attribute* copy () const;
154 
155  //-----------------------------------------------------------------
156  // Type-specific attribute I/O and copying.
157  // Depending on type T, these functions may have to be specialized.
158  //-----------------------------------------------------------------
159 
160  virtual void writeValueTo (
162 
163  virtual void readValueFrom (
164  OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is, int size, int version);
165 
166  virtual void copyValueFrom (const Attribute& other);
167 
168  //------------------------------------------------------------
169  // Dynamic casts that throw exceptions instead of returning 0.
170  //------------------------------------------------------------
171 
173  static const TypedAttribute* cast (const Attribute* attribute);
174  static TypedAttribute& cast (Attribute& attribute);
175  static const TypedAttribute& cast (const Attribute& attribute);
176 
177  //---------------------------------------------------------------
178  // Register this attribute type so that Attribute::newAttribute()
179  // knows how to make objects of this type.
180  //
181  // Note that this function is not thread-safe because it modifies
182  // a global variable in the IlmIlm library. A thread in a multi-
183  // threaded program may call registerAttributeType() only when no
184  // other thread is accessing any functions or classes in the
185  // OpenEXR library.
186  //
187  //---------------------------------------------------------------
188 
189  static void registerAttributeType ();
190 
191  //-----------------------------------------------------
192  // Un-register this attribute type (for debugging only)
193  //-----------------------------------------------------
194 
195  static void unRegisterAttributeType ();
196 
197 private:
198  T _value;
199 };
200 
201 //------------------------------------
202 // Implementation of TypedAttribute<T>
203 //------------------------------------
204 
205 template <class T>
207  : Attribute (), _value (value)
208 {
209  // empty
210 }
211 
213 {
214  // empty
215 }
216 
217 template <class T>
218 inline T&
220 {
221  return _value;
222 }
223 
224 template <class T>
225 inline const T&
227 {
228  return _value;
229 }
230 
231 template <class T>
232 const char*
234 {
235  return staticTypeName ();
236 }
237 
238 template <class T>
239 Attribute*
241 {
242  return new TypedAttribute<T> ();
243 }
244 
245 template <class T>
246 Attribute*
248 {
250  attribute->copyValueFrom (*this);
251  return attribute;
252 }
253 
254 template <class T>
255 void
258 {
259  OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write<
260  OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _value);
261 }
262 
263 template <class T>
264 void
267 {
268  OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read<
269  OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _value);
270 }
271 
272 template <class T>
273 void
275 {
276  _value = cast (other)._value;
277 }
278 
279 template <class T>
282 {
283  TypedAttribute<T>* t = dynamic_cast<TypedAttribute<T>*> (attribute);
284 
285  if (t == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
286 
287  return t;
288 }
289 
290 template <class T>
291 const TypedAttribute<T>*
293 {
294  const TypedAttribute<T>* t =
295  dynamic_cast<const TypedAttribute<T>*> (attribute);
296 
297  if (t == 0) throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
298 
299  return t;
300 }
301 
302 template <class T>
303 inline TypedAttribute<T>&
305 {
306  return *cast (&attribute);
307 }
308 
309 template <class T>
310 inline const TypedAttribute<T>&
312 {
313  return *cast (&attribute);
314 }
315 
316 template <class T>
317 inline void
319 {
320  Attribute::registerAttributeType (staticTypeName (), makeNewAttribute);
321 }
322 
323 template <class T>
324 inline void
326 {
327  Attribute::unRegisterAttributeType (staticTypeName ());
328 }
329 
331 
332 #if defined(_MSC_VER)
333 # pragma warning(pop)
334 #endif
335 
336 #endif
static TypedAttribute * cast(Attribute *attribute)
Definition: ImfAttribute.h:281
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
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:325
virtual void writeValueTo(OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, int version) const
Definition: ImfAttribute.h:256
SYS_FORCE_INLINE const X * cast(const InstancablePtr *o)
#define IMF_EXPORT_TEMPLATE_TYPE
Definition: ImfExport.h:58
#define IMF_EXPORT
Definition: ImfExport.h:54
class IMF_EXPORT_TYPE OStream
Definition: ImfForward.h:86
virtual void readValueFrom(OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int size, int version)
Definition: ImfAttribute.h:265
static Attribute * makeNewAttribute()
Definition: ImfAttribute.h:240
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:247
virtual const char * typeName() const
Definition: ImfAttribute.h:233
LeafData & operator=(const LeafData &)=delete
static void registerAttributeType()
Definition: ImfAttribute.h:318
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
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:28
#define IMF_EXPORT_TYPE
Definition: ImfExport.h:57
virtual ~TypedAttribute()
Definition: ImfAttribute.h:212
virtual void copyValueFrom(const Attribute &other)
Definition: ImfAttribute.h:274
virtual void copyValueFrom(const Attribute &other)=0
TypedAttribute()=default
class IMF_EXPORT_TYPE IStream
Definition: ImfForward.h:87