Path: blob/master/3rdparty/openexr/IlmImf/ImfAttribute.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMF_ATTRIBUTE_H37#define INCLUDED_IMF_ATTRIBUTE_H3839//-----------------------------------------------------------------------------40//41// class Attribute42//43//-----------------------------------------------------------------------------4445#include "IexBaseExc.h"46#include <ImfIO.h>47#include <ImfXdr.h>484950namespace Imf {515253class Attribute54{55public:5657//---------------------------58// Constructor and destructor59//---------------------------6061Attribute ();62virtual ~Attribute ();636465//-------------------------------66// Get this attribute's type name67//-------------------------------6869virtual const char * typeName () const = 0;707172//------------------------------73// Make a copy of this attribute74//------------------------------7576virtual Attribute * copy () const = 0;777879//----------------------------------------80// Type-specific attribute I/O and copying81//----------------------------------------8283virtual void writeValueTo (OStream &os,84int version) const = 0;8586virtual void readValueFrom (IStream &is,87int size,88int version) = 0;8990virtual void copyValueFrom (const Attribute &other) = 0;919293//------------------94// Attribute factory95//------------------9697static Attribute * newAttribute (const char typeName[]);9899100//-----------------------------------------------------------101// Test if a given attribute type has already been registered102//-----------------------------------------------------------103104static bool knownType (const char typeName[]);105106107protected:108109//--------------------------------------------------110// Register an attribute type so that newAttribute()111// knows how to make objects of this type.112//--------------------------------------------------113114static void registerAttributeType (const char typeName[],115Attribute *(*newAttribute)());116117//------------------------------------------------------118// Un-register an attribute type so that newAttribute()119// no longer knows how to make objects of this type (for120// debugging only).121//------------------------------------------------------122123static void unRegisterAttributeType (const char typeName[]);124};125126127//-------------------------------------------------128// Class template for attributes of a specific type129//-------------------------------------------------130131template <class T>132class TypedAttribute: public Attribute133{134public:135136//----------------------------137// Constructors and destructor138//------------_---------------139140TypedAttribute ();141TypedAttribute (const T &value);142TypedAttribute (const TypedAttribute<T> &other);143virtual ~TypedAttribute ();144145146//--------------------------------147// Access to the attribute's value148//--------------------------------149150T & value ();151const T & value () const;152153154//--------------------------------155// Get this attribute's type name.156//--------------------------------157158virtual const char * typeName () const;159160161//---------------------------------------------------------162// Static version of typeName()163// This function must be specialized for each value type T.164//---------------------------------------------------------165166static const char * staticTypeName ();167168169//---------------------170// Make a new attribute171//---------------------172173static Attribute * makeNewAttribute ();174175176//------------------------------177// Make a copy of this attribute178//------------------------------179180virtual Attribute * copy () const;181182183//-----------------------------------------------------------------184// Type-specific attribute I/O and copying.185// Depending on type T, these functions may have to be specialized.186//-----------------------------------------------------------------187188virtual void writeValueTo (OStream &os,189int version) const;190191virtual void readValueFrom (IStream &is,192int size,193int version);194195virtual void copyValueFrom (const Attribute &other);196197198//------------------------------------------------------------199// Dynamic casts that throw exceptions instead of returning 0.200//------------------------------------------------------------201202static TypedAttribute * cast (Attribute *attribute);203static const TypedAttribute * cast (const Attribute *attribute);204static TypedAttribute & cast (Attribute &attribute);205static const TypedAttribute & cast (const Attribute &attribute);206207208//---------------------------------------------------------------209// Register this attribute type so that Attribute::newAttribute()210// knows how to make objects of this type.211//212// Note that this function is not thread-safe because it modifies213// a global variable in the IlmIlm library. A thread in a multi-214// threaded program may call registerAttributeType() only when no215// other thread is accessing any functions or classes in the216// IlmImf library.217//218//---------------------------------------------------------------219220static void registerAttributeType ();221222223//-----------------------------------------------------224// Un-register this attribute type (for debugging only)225//-----------------------------------------------------226227static void unRegisterAttributeType ();228229230private:231232T _value;233};234235236//------------------------------------237// Implementation of TypedAttribute<T>238//------------------------------------239240template <class T>241TypedAttribute<T>::TypedAttribute ():242Attribute (),243_value (T())244{245// empty246}247248249template <class T>250TypedAttribute<T>::TypedAttribute (const T &value):251Attribute (),252_value (value)253{254// empty255}256257258template <class T>259TypedAttribute<T>::TypedAttribute (const TypedAttribute<T> &other):260Attribute (other),261_value ()262{263copyValueFrom (other);264}265266267template <class T>268TypedAttribute<T>::~TypedAttribute ()269{270// empty271}272273274template <class T>275inline T &276TypedAttribute<T>::value ()277{278return _value;279}280281282template <class T>283inline const T &284TypedAttribute<T>::value () const285{286return _value;287}288289290template <class T>291const char *292TypedAttribute<T>::typeName () const293{294return staticTypeName();295}296297298template <class T>299Attribute *300TypedAttribute<T>::makeNewAttribute ()301{302return new TypedAttribute<T>();303}304305306template <class T>307Attribute *308TypedAttribute<T>::copy () const309{310Attribute * attribute = new TypedAttribute<T>();311attribute->copyValueFrom (*this);312return attribute;313}314315316template <class T>317void318TypedAttribute<T>::writeValueTo (OStream &os, int) const319{320Xdr::write <StreamIO> (os, _value);321}322323324template <class T>325void326TypedAttribute<T>::readValueFrom (IStream &is, int, int)327{328Xdr::read <StreamIO> (is, _value);329}330331332template <class T>333void334TypedAttribute<T>::copyValueFrom (const Attribute &other)335{336_value = cast(other)._value;337}338339340template <class T>341TypedAttribute<T> *342TypedAttribute<T>::cast (Attribute *attribute)343{344TypedAttribute<T> *t =345dynamic_cast <TypedAttribute<T> *> (attribute);346347if (t == 0)348throw Iex::TypeExc ("Unexpected attribute type.");349350return t;351}352353354template <class T>355const TypedAttribute<T> *356TypedAttribute<T>::cast (const Attribute *attribute)357{358const TypedAttribute<T> *t =359dynamic_cast <const TypedAttribute<T> *> (attribute);360361if (t == 0)362throw Iex::TypeExc ("Unexpected attribute type.");363364return t;365}366367368template <class T>369inline TypedAttribute<T> &370TypedAttribute<T>::cast (Attribute &attribute)371{372return *cast (&attribute);373}374375376template <class T>377inline const TypedAttribute<T> &378TypedAttribute<T>::cast (const Attribute &attribute)379{380return *cast (&attribute);381}382383384template <class T>385inline void386TypedAttribute<T>::registerAttributeType ()387{388Attribute::registerAttributeType (staticTypeName(), makeNewAttribute);389}390391392template <class T>393inline void394TypedAttribute<T>::unRegisterAttributeType ()395{396Attribute::unRegisterAttributeType (staticTypeName());397}398399400} // namespace Imf401402#if defined(OPENEXR_DLL) && defined(_MSC_VER)403// Tell MS VC++ to disable "non dll-interface class used as base404// for dll-interface class" and "no suitable definition provided405// for explicit template"406#pragma warning (disable : 4275 4661)407408#if defined (ILMIMF_EXPORTS)409#define IMF_EXPIMP_TEMPLATE410#else411#define IMF_EXPIMP_TEMPLATE extern412#endif413414IMF_EXPIMP_TEMPLATE template class Imf::TypedAttribute<float>;415IMF_EXPIMP_TEMPLATE template class Imf::TypedAttribute<double>;416417#pragma warning(default : 4251)418#undef EXTERN_TEMPLATE419#endif420421// Metrowerks compiler wants the .cpp file inlined, too422#ifdef __MWERKS__423#include <ImfAttribute.cpp>424#endif425426#endif427428429