ImfAttribute.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #ifndef INCLUDED_IMF_ATTRIBUTE_H
  35. #define INCLUDED_IMF_ATTRIBUTE_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // class Attribute
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "IexBaseExc.h"
  42. #include "ImfIO.h"
  43. #include "ImfXdr.h"
  44. #include "ImfForward.h"
  45. #include "ImfExport.h"
  46. #include "ImfNamespace.h"
  47. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  48. class Attribute
  49. {
  50. public:
  51. //---------------------------
  52. // Constructor and destructor
  53. //---------------------------
  54. IMF_EXPORT
  55. Attribute ();
  56. IMF_EXPORT
  57. virtual ~Attribute ();
  58. //-------------------------------
  59. // Get this attribute's type name
  60. //-------------------------------
  61. virtual const char * typeName () const = 0;
  62. //------------------------------
  63. // Make a copy of this attribute
  64. //------------------------------
  65. virtual Attribute * copy () const = 0;
  66. //----------------------------------------
  67. // Type-specific attribute I/O and copying
  68. //----------------------------------------
  69. virtual void writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  70. int version) const = 0;
  71. virtual void readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
  72. int size,
  73. int version) = 0;
  74. virtual void copyValueFrom (const Attribute &other) = 0;
  75. //------------------
  76. // Attribute factory
  77. //------------------
  78. IMF_EXPORT
  79. static Attribute * newAttribute (const char typeName[]);
  80. //-----------------------------------------------------------
  81. // Test if a given attribute type has already been registered
  82. //-----------------------------------------------------------
  83. IMF_EXPORT
  84. static bool knownType (const char typeName[]);
  85. protected:
  86. //--------------------------------------------------
  87. // Register an attribute type so that newAttribute()
  88. // knows how to make objects of this type.
  89. //--------------------------------------------------
  90. IMF_EXPORT
  91. static void registerAttributeType (const char typeName[],
  92. Attribute *(*newAttribute)());
  93. //------------------------------------------------------
  94. // Un-register an attribute type so that newAttribute()
  95. // no longer knows how to make objects of this type (for
  96. // debugging only).
  97. //------------------------------------------------------
  98. IMF_EXPORT
  99. static void unRegisterAttributeType (const char typeName[]);
  100. };
  101. //-------------------------------------------------
  102. // Class template for attributes of a specific type
  103. //-------------------------------------------------
  104. template <class T>
  105. class TypedAttribute: public Attribute
  106. {
  107. public:
  108. //----------------------------
  109. // Constructors and destructor
  110. //------------_---------------
  111. TypedAttribute ();
  112. TypedAttribute (const T &value);
  113. TypedAttribute (const TypedAttribute<T> &other);
  114. virtual ~TypedAttribute ();
  115. //--------------------------------
  116. // Access to the attribute's value
  117. //--------------------------------
  118. T & value ();
  119. const T & value () const;
  120. //--------------------------------
  121. // Get this attribute's type name.
  122. //--------------------------------
  123. virtual const char * typeName () const;
  124. //---------------------------------------------------------
  125. // Static version of typeName()
  126. // This function must be specialized for each value type T.
  127. //---------------------------------------------------------
  128. static const char * staticTypeName ();
  129. //---------------------
  130. // Make a new attribute
  131. //---------------------
  132. static Attribute * makeNewAttribute ();
  133. //------------------------------
  134. // Make a copy of this attribute
  135. //------------------------------
  136. virtual Attribute * copy () const;
  137. //-----------------------------------------------------------------
  138. // Type-specific attribute I/O and copying.
  139. // Depending on type T, these functions may have to be specialized.
  140. //-----------------------------------------------------------------
  141. virtual void writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  142. int version) const;
  143. virtual void readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
  144. int size,
  145. int version);
  146. virtual void copyValueFrom (const Attribute &other);
  147. //------------------------------------------------------------
  148. // Dynamic casts that throw exceptions instead of returning 0.
  149. //------------------------------------------------------------
  150. static TypedAttribute * cast (Attribute *attribute);
  151. static const TypedAttribute * cast (const Attribute *attribute);
  152. static TypedAttribute & cast (Attribute &attribute);
  153. static const TypedAttribute & cast (const Attribute &attribute);
  154. //---------------------------------------------------------------
  155. // Register this attribute type so that Attribute::newAttribute()
  156. // knows how to make objects of this type.
  157. //
  158. // Note that this function is not thread-safe because it modifies
  159. // a global variable in the IlmIlm library. A thread in a multi-
  160. // threaded program may call registerAttributeType() only when no
  161. // other thread is accessing any functions or classes in the
  162. // IlmImf library.
  163. //
  164. //---------------------------------------------------------------
  165. static void registerAttributeType ();
  166. //-----------------------------------------------------
  167. // Un-register this attribute type (for debugging only)
  168. //-----------------------------------------------------
  169. static void unRegisterAttributeType ();
  170. private:
  171. T _value;
  172. };
  173. //------------------------------------
  174. // Implementation of TypedAttribute<T>
  175. //------------------------------------
  176. template <class T>
  177. TypedAttribute<T>::TypedAttribute ():
  178. Attribute (),
  179. _value (T())
  180. {
  181. // empty
  182. }
  183. template <class T>
  184. TypedAttribute<T>::TypedAttribute (const T & value):
  185. Attribute (),
  186. _value (value)
  187. {
  188. // empty
  189. }
  190. template <class T>
  191. TypedAttribute<T>::TypedAttribute (const TypedAttribute<T> &other):
  192. Attribute (other),
  193. _value ()
  194. {
  195. copyValueFrom (other);
  196. }
  197. template <class T>
  198. TypedAttribute<T>::~TypedAttribute ()
  199. {
  200. // empty
  201. }
  202. template <class T>
  203. inline T &
  204. TypedAttribute<T>::value ()
  205. {
  206. return _value;
  207. }
  208. template <class T>
  209. inline const T &
  210. TypedAttribute<T>::value () const
  211. {
  212. return _value;
  213. }
  214. template <class T>
  215. const char *
  216. TypedAttribute<T>::typeName () const
  217. {
  218. return staticTypeName();
  219. }
  220. template <class T>
  221. Attribute *
  222. TypedAttribute<T>::makeNewAttribute ()
  223. {
  224. return new TypedAttribute<T>();
  225. }
  226. template <class T>
  227. Attribute *
  228. TypedAttribute<T>::copy () const
  229. {
  230. Attribute * attribute = new TypedAttribute<T>();
  231. attribute->copyValueFrom (*this);
  232. return attribute;
  233. }
  234. template <class T>
  235. void
  236. TypedAttribute<T>::writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  237. int version) const
  238. {
  239. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _value);
  240. }
  241. template <class T>
  242. void
  243. TypedAttribute<T>::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
  244. int size,
  245. int version)
  246. {
  247. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _value);
  248. }
  249. template <class T>
  250. void
  251. TypedAttribute<T>::copyValueFrom (const Attribute &other)
  252. {
  253. _value = cast(other)._value;
  254. }
  255. template <class T>
  256. TypedAttribute<T> *
  257. TypedAttribute<T>::cast (Attribute *attribute)
  258. {
  259. TypedAttribute<T> *t =
  260. dynamic_cast <TypedAttribute<T> *> (attribute);
  261. if (t == 0)
  262. throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
  263. return t;
  264. }
  265. template <class T>
  266. const TypedAttribute<T> *
  267. TypedAttribute<T>::cast (const Attribute *attribute)
  268. {
  269. const TypedAttribute<T> *t =
  270. dynamic_cast <const TypedAttribute<T> *> (attribute);
  271. if (t == 0)
  272. throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
  273. return t;
  274. }
  275. template <class T>
  276. inline TypedAttribute<T> &
  277. TypedAttribute<T>::cast (Attribute &attribute)
  278. {
  279. return *cast (&attribute);
  280. }
  281. template <class T>
  282. inline const TypedAttribute<T> &
  283. TypedAttribute<T>::cast (const Attribute &attribute)
  284. {
  285. return *cast (&attribute);
  286. }
  287. template <class T>
  288. inline void
  289. TypedAttribute<T>::registerAttributeType ()
  290. {
  291. Attribute::registerAttributeType (staticTypeName(), makeNewAttribute);
  292. }
  293. template <class T>
  294. inline void
  295. TypedAttribute<T>::unRegisterAttributeType ()
  296. {
  297. Attribute::unRegisterAttributeType (staticTypeName());
  298. }
  299. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  300. #endif