/src/FreeImage/Source/OpenEXR/IlmImf/ImfAttribute.h

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