/*! \file value.h
** \brief Template Header
**
-** $Id: value.h,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
+** $Id$
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
#include "blinepoint.h"
#include "exception.h"
+#ifdef USE_HALF_TYPE
+#include <OpenEXR/half.h>
+#endif
+
#ifndef SYNFIG_NO_ANGLE
#include "angle.h"
#endif
class Gradient;
class BLinePoint;
class Color;
-
+
/*! \class ValueBase
** \todo writeme
*/
enum Type
{
TYPE_NIL=0, //!< Represents an empty value
-
+
TYPE_BOOL,
TYPE_INTEGER,
TYPE_ANGLE, //!< Angle
// All types after this point are larger than 32 bits
-
+
TYPE_TIME, //!< Time
TYPE_REAL, //!< Real
// All types after this point are larger than 64 bits
-
+
TYPE_VECTOR, //!< Vector
TYPE_COLOR, //!< Color
TYPE_SEGMENT, //!< Segment
TYPE_BLINEPOINT, //!< BLinePoint
// All types after this point require construction/destruction
-
+
TYPE_LIST, //!< List
TYPE_CANVAS, //!< Canvas
TYPE_STRING, //!< String
TYPE_END //!< Not a valid type, used for sanity checks
};
-
+
private:
typedef std::vector<ValueBase> list_type;
*/
protected:
-
+
Type type;
void *data;
etl::reference_counter ref_count;
- bool loop_;
+ bool loop_;
/*
-- ** -- C O N S T R U C T O R S -----------------------------------
*/
-
+
public:
//! \writeme
//! \writeme
~ValueBase();
-
+
/*
-- ** -- O P E R A T O R S ---------------------------------------------------
*/
{ set(x); return *this; }
//! \writeme
- ValueBase& operator=(const ValueBase& x);
+ ValueBase& operator=(const ValueBase& x);
//! \writeme
bool operator==(const ValueBase& rhs)const;
bool get_loop()const { return loop_; }
//! \writeme
- void set_loop(bool x) { loop_=x; }
-
+ void set_loop(bool x) { loop_=x; }
+
//! \writeme
bool empty()const;
//! \writeme
Type get_contained_type()const;
-
+
//! Returns true if the contained value is defined and valid.
bool is_valid()const;
//! Returns the type of the contained value
const Type & get_type()const { return type; }
-
+
//! Checks the type of the parameter against itself. Returns true if they are of the same type.
template <class T> bool
- same_as(const T &x)const
+ same_type_as(const T &x)const
{
const Type testtype(get_type(x));
-
+
+ return same_type_as(testtype);
+ }
+
+ bool same_type_as(const Type testtype)const
+ {
if(testtype==type)return true;
if( (type==TYPE_REAL || type==TYPE_TIME) &&
(testtype==TYPE_REAL || testtype==TYPE_TIME) )
return true;
return false;
}
-
-
- // === GET MEMBERS ========================================================
+
+
+ // === GET MEMBERS ========================================================
template <typename T>
const T &get(const T& x)const
{
- assert(is_valid() && same_as(x));
+ assert(is_valid() && same_type_as(x));
return *static_cast<const T*>(data);
}
float get(const float &)const { return get(Real()); }
const char* get(const char*)const;
const list_type& get_list()const { return get(list_type()); }
// ========================================================================
-
-
-
- // === PUT MEMBERS ========================================================
+
+
+
+ // === PUT MEMBERS ========================================================
template <typename T>
void put(T* x)const
{
- assert(same_as(*x));
+ assert(same_type_as(*x));
*x=*static_cast<const T*>(data);
}
void put(float* x)const { *x=get(Real()); }
void put(char** x)const;
// ========================================================================
-
-
- // === SET MEMBERS ========================================================
+
+
+ // === SET MEMBERS ========================================================
template <typename T> void set(const T& x) { _set(x); }
void set(const float &x) { _set(Real(x)); }
void set(const list_type &x);
- void set(const char* x);
+ void set(const char* x);
void set(Canvas*x);
void set(etl::loose_handle<Canvas> x);
void set(etl::handle<Canvas> x);
template <class T> void set(const std::vector<T> &x)
{ _set(list_type(x.begin(),x.end())); }
template <class T> void set(const std::list<T> &x)
- { _set(list_type(x.begin(),x.end())); }
+ { _set(list_type(x.begin(),x.end())); }
// ========================================================================
-
+
/*
-- ** -- S T A T I C F U N C T I O N S -------------------------------------
*/
static Type ident_type(const String &str);
- // === GET TYPE MEMBERS ===================================================
+ // === GET TYPE MEMBERS ===================================================
static const Type get_type(bool) { return TYPE_BOOL; }
static const Type get_type(int) { return TYPE_INTEGER; }
static const Type get_type(const Time&) { return TYPE_TIME; }
static const Type get_type(const etl::handle<Canvas>&)
{ return TYPE_CANVAS; }
static const Type get_type(const etl::loose_handle<Canvas>&)
- { return TYPE_CANVAS; }
+ { return TYPE_CANVAS; }
static const Type get_type(const list_type&) { return TYPE_LIST; }
- template <class T> static const Type get_type(const std::vector<T> &x)
+ template <class T> static const Type get_type(const std::vector<T> &/*x*/)
{ return TYPE_LIST; }
- template <class T> static const Type get_type(const std::list<T> &x)
+ template <class T> static const Type get_type(const std::list<T> &/*x*/)
{ return TYPE_LIST; }
// ========================================================================
-
+
/*
-- ** -- C A S T O P E R A T O R S -----------------------------------------
*/
//operator const Color&()const { return get(Color()); }
//operator const Real&()const { return get(Real()); }
//operator const Time&()const { return get(Time()); }
-
+
operator const Vector&()const { return get(Vector()); }
operator const BLinePoint&()const { return get(BLinePoint()); }
//operator const int&()const { return get(int()); }
static const Type get_type(const half&) { return TYPE_REAL; }
operator half()const { return get(Real()); }
#endif
-
+
#ifndef SYNFIG_NO_ANGLE
operator const Angle&()const { return get(Angle()); }
static const Type get_type(const Angle&) { return TYPE_ANGLE; }
return ret;
}
-
+
private:
-
+
template <typename T> void
_set(const T& x)
{
const Type newtype(get_type(x));
-
+
assert(newtype!=TYPE_NIL);
-
+
if(newtype==type)
{
if(ref_count.unique())
type=newtype;
ref_count.reset();
data=new T(x);
- }
+ }
}; // END of class ValueBase
Value(const T &x):ValueBase(x)
{
}
-
+
Value(const ValueBase &x):ValueBase(x)
{
- if(!x.same_as(T()))
+ if(!x.same_type_as(T()))
throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
}
-
+
Value()
{
}
-
+
T get()const { return ValueBase::get(T()); }
void put(T* x)const { ValueBase::put(x); }
-
+
void set(const T& x) { ValueBase::operator=(x); }
Value<T>& operator=(const T& x) { set(x); return *this; }
Value<T>& operator=(const ValueBase& x)
{
- if(!x.same_as(T()))
+ if(!x.same_type_as(T()))
throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
return ValueBase::operator=(x);
}
-
+
}; // END of class Value
/*
}
Value(const ValueBase &x):ValueBase(x)
{
- if(!x.same_as(T()))
+ if(!x.same_type_as(T()))
throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
}
Value()
{
}
-
+
T get()const { return ValueBase::get(T()); }
void put(T* x)const { ValueBase::put(x); }
-
+
void set(const T& x) { ValueBase::operator=(x); }
Value<T>& operator=(const T& x) { set(x); return *this; }
Value<T>& operator=(const ValueBase& x)
{
- if(!x.same_as(T()))
+ if(!x.same_type_as(T()))
throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
return ValueBase::operator=(x);
}
-
+
}; // END of class Value
*/