Blame IlmImf/ImfAttribute.h

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
Packit 0d464f
// Digital Ltd. LLC
Packit 0d464f
// 
Packit 0d464f
// All rights reserved.
Packit 0d464f
// 
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
// *       Redistributions of source code must retain the above copyright
Packit 0d464f
// notice, this list of conditions and the following disclaimer.
Packit 0d464f
// *       Redistributions in binary form must reproduce the above
Packit 0d464f
// copyright notice, this list of conditions and the following disclaimer
Packit 0d464f
// in the documentation and/or other materials provided with the
Packit 0d464f
// distribution.
Packit 0d464f
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
// its contributors may be used to endorse or promote products derived
Packit 0d464f
// from this software without specific prior written permission. 
Packit 0d464f
// 
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 0d464f
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 0d464f
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 0d464f
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 0d464f
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 0d464f
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 0d464f
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 0d464f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 0d464f
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
Packit 0d464f
Packit 0d464f
#ifndef INCLUDED_IMF_ATTRIBUTE_H
Packit 0d464f
#define INCLUDED_IMF_ATTRIBUTE_H
Packit 0d464f
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//	class Attribute
Packit 0d464f
//
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include "IexBaseExc.h"
Packit 0d464f
#include "ImfIO.h"
Packit 0d464f
#include "ImfXdr.h"
Packit 0d464f
#include "ImfForward.h"
Packit 0d464f
#include "ImfExport.h"
Packit 0d464f
#include "ImfNamespace.h"
Packit 0d464f
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 0d464f
Packit 0d464f
Packit 0d464f
class IMF_EXPORT Attribute
Packit 0d464f
{
Packit 0d464f
  public:
Packit 0d464f
Packit 0d464f
    //---------------------------
Packit 0d464f
    // Constructor and destructor
Packit 0d464f
    //---------------------------
Packit 0d464f
Packit 0d464f
    Attribute ();
Packit 0d464f
    virtual ~Attribute ();
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //-------------------------------
Packit 0d464f
    // Get this attribute's type name
Packit 0d464f
    //-------------------------------
Packit 0d464f
Packit 0d464f
    virtual const char *	typeName () const = 0;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //------------------------------
Packit 0d464f
    // Make a copy of this attribute
Packit 0d464f
    //------------------------------
Packit 0d464f
Packit 0d464f
    virtual Attribute *		copy () const = 0;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //----------------------------------------
Packit 0d464f
    // Type-specific attribute I/O and copying
Packit 0d464f
    //----------------------------------------
Packit 0d464f
Packit 0d464f
    virtual void		writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
Packit 0d464f
					      int version) const = 0;
Packit 0d464f
Packit 0d464f
    virtual void		readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
Packit 0d464f
					       int size,
Packit 0d464f
					       int version) = 0;
Packit 0d464f
Packit 0d464f
    virtual void		copyValueFrom (const Attribute &other) = 0;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //------------------
Packit 0d464f
    // Attribute factory
Packit 0d464f
    //------------------
Packit 0d464f
Packit 0d464f
    static Attribute *		newAttribute (const char typeName[]);
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //-----------------------------------------------------------
Packit 0d464f
    // Test if a given attribute type has already been registered
Packit 0d464f
    //-----------------------------------------------------------
Packit 0d464f
Packit 0d464f
    static bool			knownType (const char typeName[]);
Packit 0d464f
Packit 0d464f
Packit 0d464f
  protected:
Packit 0d464f
Packit 0d464f
    //--------------------------------------------------
Packit 0d464f
    // Register an attribute type so that newAttribute()
Packit 0d464f
    // knows how to make objects of this type.
Packit 0d464f
    //--------------------------------------------------
Packit 0d464f
Packit 0d464f
    static void		registerAttributeType (const char typeName[],
Packit 0d464f
					       Attribute *(*newAttribute)());
Packit 0d464f
Packit 0d464f
    //------------------------------------------------------
Packit 0d464f
    // Un-register an attribute type so that newAttribute()
Packit 0d464f
    // no longer knows how to make objects of this type (for
Packit 0d464f
    // debugging only).
Packit 0d464f
    //------------------------------------------------------
Packit 0d464f
Packit 0d464f
    static void		unRegisterAttributeType (const char typeName[]);
Packit 0d464f
};
Packit 0d464f
Packit 0d464f
Packit 0d464f
//-------------------------------------------------
Packit 0d464f
// Class template for attributes of a specific type
Packit 0d464f
//-------------------------------------------------
Packit 0d464f
    
Packit 0d464f
template <class T>
Packit 0d464f
class TypedAttribute: public Attribute
Packit 0d464f
{
Packit 0d464f
  public:
Packit 0d464f
Packit 0d464f
    //----------------------------
Packit 0d464f
    // Constructors and destructor
Packit 0d464f
    //------------_---------------
Packit 0d464f
Packit 0d464f
    TypedAttribute ();
Packit 0d464f
    TypedAttribute (const T &value);
Packit 0d464f
    TypedAttribute (const TypedAttribute<T> &other);
Packit 0d464f
    virtual ~TypedAttribute ();
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //--------------------------------
Packit 0d464f
    // Access to the attribute's value
Packit 0d464f
    //--------------------------------
Packit 0d464f
Packit 0d464f
    T &					value ();
Packit 0d464f
    const T &				value () const;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //--------------------------------
Packit 0d464f
    // Get this attribute's type name.
Packit 0d464f
    //--------------------------------
Packit 0d464f
Packit 0d464f
    virtual const char *		typeName () const;
Packit 0d464f
    
Packit 0d464f
Packit 0d464f
    //---------------------------------------------------------
Packit 0d464f
    // Static version of typeName()
Packit 0d464f
    // This function must be specialized for each value type T.
Packit 0d464f
    //---------------------------------------------------------
Packit 0d464f
Packit 0d464f
    static const char *			staticTypeName ();
Packit 0d464f
    
Packit 0d464f
Packit 0d464f
    //---------------------
Packit 0d464f
    // Make a new attribute
Packit 0d464f
    //---------------------
Packit 0d464f
Packit 0d464f
    static Attribute *			makeNewAttribute ();
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //------------------------------
Packit 0d464f
    // Make a copy of this attribute
Packit 0d464f
    //------------------------------
Packit 0d464f
Packit 0d464f
    virtual Attribute *			copy () const;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //-----------------------------------------------------------------
Packit 0d464f
    // Type-specific attribute I/O and copying.
Packit 0d464f
    // Depending on type T, these functions may have to be specialized.
Packit 0d464f
    //-----------------------------------------------------------------
Packit 0d464f
Packit 0d464f
    virtual void		writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
Packit 0d464f
					      int version) const;
Packit 0d464f
Packit 0d464f
    virtual void		readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
Packit 0d464f
					       int size,
Packit 0d464f
					       int version);
Packit 0d464f
Packit 0d464f
    virtual void		copyValueFrom (const Attribute &other);
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //------------------------------------------------------------
Packit 0d464f
    // Dynamic casts that throw exceptions instead of returning 0.
Packit 0d464f
    //------------------------------------------------------------
Packit 0d464f
Packit 0d464f
    static TypedAttribute *		cast (Attribute *attribute);
Packit 0d464f
    static const TypedAttribute *	cast (const Attribute *attribute);
Packit 0d464f
    static TypedAttribute &		cast (Attribute &attribute);
Packit 0d464f
    static const TypedAttribute &	cast (const Attribute &attribute);
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //---------------------------------------------------------------
Packit 0d464f
    // Register this attribute type so that Attribute::newAttribute()
Packit 0d464f
    // knows how to make objects of this type.
Packit 0d464f
    //
Packit 0d464f
    // Note that this function is not thread-safe because it modifies
Packit 0d464f
    // a global variable in the IlmIlm library.  A thread in a multi-
Packit 0d464f
    // threaded program may call registerAttributeType() only when no
Packit 0d464f
    // other thread is accessing any functions or classes in the
Packit 0d464f
    // IlmImf library.
Packit 0d464f
    //
Packit 0d464f
    //---------------------------------------------------------------
Packit 0d464f
Packit 0d464f
    static void				registerAttributeType ();
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //-----------------------------------------------------
Packit 0d464f
    // Un-register this attribute type (for debugging only)
Packit 0d464f
    //-----------------------------------------------------
Packit 0d464f
Packit 0d464f
    static void				 unRegisterAttributeType ();
Packit 0d464f
Packit 0d464f
Packit 0d464f
  private:
Packit 0d464f
Packit 0d464f
    T					_value;
Packit 0d464f
};
Packit 0d464f
Packit 0d464f
//------------------------------------
Packit 0d464f
// Implementation of TypedAttribute<T>
Packit 0d464f
//------------------------------------
Packit 0d464f
template <class T>
Packit 0d464f
TypedAttribute<T>::TypedAttribute ():
Packit 0d464f
    Attribute (),
Packit 0d464f
    _value (T())
Packit 0d464f
{
Packit 0d464f
    // empty
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedAttribute<T>::TypedAttribute (const T & value):
Packit 0d464f
    Attribute (),
Packit 0d464f
    _value (value)
Packit 0d464f
{
Packit 0d464f
    // empty
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedAttribute<T>::TypedAttribute (const TypedAttribute<T> &other):
Packit 0d464f
    Attribute (other),
Packit 0d464f
    _value ()
Packit 0d464f
{
Packit 0d464f
    copyValueFrom (other);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedAttribute<T>::~TypedAttribute ()
Packit 0d464f
{
Packit 0d464f
    // empty
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline T &
Packit 0d464f
TypedAttribute<T>::value ()
Packit 0d464f
{
Packit 0d464f
    return _value;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline const T &
Packit 0d464f
TypedAttribute<T>::value () const
Packit 0d464f
{
Packit 0d464f
    return _value;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
const char *	
Packit 0d464f
TypedAttribute<T>::typeName () const
Packit 0d464f
{
Packit 0d464f
    return staticTypeName();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
Attribute *
Packit 0d464f
TypedAttribute<T>::makeNewAttribute ()
Packit 0d464f
{
Packit 0d464f
    return new TypedAttribute<T>();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
Attribute *
Packit 0d464f
TypedAttribute<T>::copy () const
Packit 0d464f
{
Packit 0d464f
    Attribute * attribute = new TypedAttribute<T>();
Packit 0d464f
    attribute->copyValueFrom (*this);
Packit 0d464f
    return attribute;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
void		
Packit 0d464f
TypedAttribute<T>::writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
Packit 0d464f
                                    int version) const
Packit 0d464f
{
Packit 0d464f
    OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, _value);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
void		
Packit 0d464f
TypedAttribute<T>::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
Packit 0d464f
                                     int size,
Packit 0d464f
                                     int version)
Packit 0d464f
{
Packit 0d464f
    OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, _value);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
void		
Packit 0d464f
TypedAttribute<T>::copyValueFrom (const Attribute &other)
Packit 0d464f
{
Packit 0d464f
    _value = cast(other)._value;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedAttribute<T> *
Packit 0d464f
TypedAttribute<T>::cast (Attribute *attribute)
Packit 0d464f
{
Packit 0d464f
    TypedAttribute<T> *t =
Packit 0d464f
	dynamic_cast <TypedAttribute<T> *> (attribute);
Packit 0d464f
Packit 0d464f
    if (t == 0)
Packit 0d464f
	throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
Packit 0d464f
Packit 0d464f
    return t;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
const TypedAttribute<T> *
Packit 0d464f
TypedAttribute<T>::cast (const Attribute *attribute)
Packit 0d464f
{
Packit 0d464f
    const TypedAttribute<T> *t =
Packit 0d464f
	dynamic_cast <const TypedAttribute<T> *> (attribute);
Packit 0d464f
Packit 0d464f
    if (t == 0)
Packit 0d464f
	throw IEX_NAMESPACE::TypeExc ("Unexpected attribute type.");
Packit 0d464f
Packit 0d464f
    return t;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline TypedAttribute<T> &
Packit 0d464f
TypedAttribute<T>::cast (Attribute &attribute)
Packit 0d464f
{
Packit 0d464f
    return *cast (&attribute);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline const TypedAttribute<T> &
Packit 0d464f
TypedAttribute<T>::cast (const Attribute &attribute)
Packit 0d464f
{
Packit 0d464f
    return *cast (&attribute);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline void
Packit 0d464f
TypedAttribute<T>::registerAttributeType ()
Packit 0d464f
{
Packit 0d464f
    Attribute::registerAttributeType (staticTypeName(), makeNewAttribute);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline void
Packit 0d464f
TypedAttribute<T>::unRegisterAttributeType ()
Packit 0d464f
{
Packit 0d464f
    Attribute::unRegisterAttributeType (staticTypeName());
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 0d464f
Packit 0d464f
Packit 0d464f
#endif