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 //
3 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMF_ATTRIBUTE_H
38 #define INCLUDED_IMF_ATTRIBUTE_H
39 
40 //-----------------------------------------------------------------------------
41 //
42 // class Attribute
43 //
44 //-----------------------------------------------------------------------------
45 
46 #include "IexBaseExc.h"
47 #include "ImfIO.h"
48 #include "ImfXdr.h"
49 #include "ImfForward.h"
50 #include "ImfExport.h"
51 #include "ImfNamespace.h"
52 
54 
55 
56 class Attribute
57 {
58  public:
59 
60  //---------------------------
61  // Constructor and destructor
62  //---------------------------
63 
65  Attribute ();
67  virtual ~Attribute ();
68 
69 
70  //-------------------------------
71  // Get this attribute's type name
72  //-------------------------------
73 
74  virtual const char * typeName () const = 0;
75 
76 
77  //------------------------------
78  // Make a copy of this attribute
79  //------------------------------
80 
81  virtual Attribute * copy () const = 0;
82 
83 
84  //----------------------------------------
85  // Type-specific attribute I/O and copying
86  //----------------------------------------
87 
88  virtual void writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
89  int version) const = 0;
90 
91  virtual void readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
92  int size,
93  int version) = 0;
94 
95  virtual void copyValueFrom (const Attribute &other) = 0;
96 
97 
98  //------------------
99  // Attribute factory
100  //------------------
101 
102  IMF_EXPORT
103  static Attribute * newAttribute (const char typeName[]);
104 
105 
106  //-----------------------------------------------------------
107  // Test if a given attribute type has already been registered
108  //-----------------------------------------------------------
109 
110  IMF_EXPORT
111  static bool knownType (const char typeName[]);
112 
113 
114  protected:
115 
116  //--------------------------------------------------
117  // Register an attribute type so that newAttribute()
118  // knows how to make objects of this type.
119  //--------------------------------------------------
120 
121  IMF_EXPORT
122  static void registerAttributeType (const char typeName[],
123  Attribute *(*newAttribute)());
124 
125  //------------------------------------------------------
126  // Un-register an attribute type so that newAttribute()
127  // no longer knows how to make objects of this type (for
128  // debugging only).
129  //------------------------------------------------------
130 
131  IMF_EXPORT
132  static void unRegisterAttributeType (const char typeName[]);
133 };
134 
135 
136 //-------------------------------------------------
137 // Class template for attributes of a specific type
138 //-------------------------------------------------
139 
140 template <class T>
142 {
143  public:
144 
145  //----------------------------
146  // Constructors and destructor
147  //------------_---------------
148 
149  TypedAttribute ();
150  TypedAttribute (const T &value);
151  TypedAttribute (const TypedAttribute<T> &other);
152  virtual ~TypedAttribute ();
153 
154 
155  //--------------------------------
156  // Access to the attribute's value
157  //--------------------------------
158 
159  T & value ();
160  const T & value () const;
161 
162 
163  //--------------------------------
164  // Get this attribute's type name.
165  //--------------------------------
166 
167  virtual const char * typeName () const;
168 
169 
170  //---------------------------------------------------------
171  // Static version of typeName()
172  // This function must be specialized for each value type T.
173  //---------------------------------------------------------
174 
175  static const char * staticTypeName ();
176 
177 
178  //---------------------
179  // Make a new attribute
180  //---------------------
181 
182  static Attribute * makeNewAttribute ();
183 
184 
185  //------------------------------
186  // Make a copy of this attribute
187  //------------------------------
188 
189  virtual Attribute * copy () const;
190 
191 
192  //-----------------------------------------------------------------
193  // Type-specific attribute I/O and copying.
194  // Depending on type T, these functions may have to be specialized.
195  //-----------------------------------------------------------------
196 
197  virtual void writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
198  int version) const;
199 
200  virtual void readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
201  int size,
202  int version);
203 
204  virtual void copyValueFrom (const Attribute &other);
205 
206 
207  //------------------------------------------------------------
208  // Dynamic casts that throw exceptions instead of returning 0.
209  //------------------------------------------------------------
210 
212  static const TypedAttribute * cast (const Attribute *attribute);
213  static TypedAttribute & cast (Attribute &attribute);
214  static const TypedAttribute & cast (const Attribute &attribute);
215 
216 
217  //---------------------------------------------------------------
218  // Register this attribute type so that Attribute::newAttribute()
219  // knows how to make objects of this type.
220  //
221  // Note that this function is not thread-safe because it modifies
222  // a global variable in the IlmIlm library. A thread in a multi-
223  // threaded program may call registerAttributeType() only when no
224  // other thread is accessing any functions or classes in the
225  // IlmImf library.
226  //
227  //---------------------------------------------------------------
228 
229  static void registerAttributeType ();
230 
231 
232  //-----------------------------------------------------
233  // Un-register this attribute type (for debugging only)
234  //-----------------------------------------------------
235 
236  static void unRegisterAttributeType ();
237 
238 
239  private:
240 
241  T _value;
242 };
243 
244 //------------------------------------
245 // Implementation of TypedAttribute<T>
246 //------------------------------------
247 template <class T>
249  Attribute (),
250  _value (T())
251 {
252  // empty
253 }
254 
255 
256 template <class T>
258  Attribute (),
259  _value (value)
260 {
261  // empty
262 }
263 
264 
265 template <class T>
267  Attribute (other),
268  _value ()
269 {
270  copyValueFrom (other);
271 }
272 
273 
274 template <class T>
276 {
277  // empty
278 }
279 
280 
281 template <class T>
282 inline T &
284 {
285  return _value;
286 }
287 
288 
289 template <class T>
290 inline const T &
292 {
293  return _value;
294 }
295 
296 
297 template <class T>
298 const char *
300 {
301  return staticTypeName();
302 }
303 
304 
305 template <class T>
306 Attribute *
308 {
309  return new TypedAttribute<T>();
310 }
311 
312 
313 template <class T>
314 Attribute *
316 {
318  attribute->copyValueFrom (*this);
319  return attribute;
320 }
321 
322 
323 template <class T>
324 void
325 TypedAttribute<T>::writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
326  int version) const
327 {
328  OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _value);
329 }
330 
331 
332 template <class T>
333 void
334 TypedAttribute<T>::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
335  int size,
336  int version)
337 {
338  OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _value);
339 }
340 
341 
342 template <class T>
343 void
345 {
346  _value = cast(other)._value;
347 }
348 
349 
350 template <class T>
353 {
355  dynamic_cast <TypedAttribute<T> *> (attribute);
356 
357  if (t == 0)
358  throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
359 
360  return t;
361 }
362 
363 
364 template <class T>
365 const TypedAttribute<T> *
367 {
368  const TypedAttribute<T> *t =
369  dynamic_cast <const TypedAttribute<T> *> (attribute);
370 
371  if (t == 0)
372  throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
373 
374  return t;
375 }
376 
377 
378 template <class T>
379 inline TypedAttribute<T> &
381 {
382  return *cast (&attribute);
383 }
384 
385 
386 template <class T>
387 inline const TypedAttribute<T> &
389 {
390  return *cast (&attribute);
391 }
392 
393 
394 template <class T>
395 inline void
397 {
398  Attribute::registerAttributeType (staticTypeName(), makeNewAttribute);
399 }
400 
401 
402 template <class T>
403 inline void
405 {
406  Attribute::unRegisterAttributeType (staticTypeName());
407 }
408 
409 
411 
412 
413 #endif
static IMF_EXPORT Attribute * newAttribute(const char typeName[])
static TypedAttribute * cast(Attribute *attribute)
Definition: ImfAttribute.h:352
GLsizeiptr size
Definition: glew.h:1681
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:109
virtual void readValueFrom(OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int size, int version)
Definition: ImfAttribute.h:334
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 IMF_EXPORT bool knownType(const char typeName[])
virtual void writeValueTo(OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, int version) const
Definition: ImfAttribute.h:325
static void unRegisterAttributeType()
Definition: ImfAttribute.h:404
virtual IMF_EXPORT ~Attribute()
#define IMF_EXPORT
Definition: ImfExport.h:44
static Attribute * makeNewAttribute()
Definition: ImfAttribute.h:307
static IMF_EXPORT void registerAttributeType(const char typeName[], Attribute *(*newAttribute)())
GT_API const UT_StringHolder version
virtual Attribute * copy() const
Definition: ImfAttribute.h:315
virtual const char * typeName() const
Definition: ImfAttribute.h:299
static void registerAttributeType()
Definition: ImfAttribute.h:396
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:108
virtual const char * typeName() const =0
virtual void readValueFrom(OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int size, int version)=0
static const char * staticTypeName()
IMF_EXPORT Attribute()
GLsizei const GLfloat * value
Definition: glew.h:1849
virtual ~TypedAttribute()
Definition: ImfAttribute.h:275
GLdouble GLdouble t
Definition: glew.h:1398
virtual void copyValueFrom(const Attribute &other)
Definition: ImfAttribute.h:344
virtual void copyValueFrom(const Attribute &other)=0