X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fvalue.h;h=7079dbae0a2aa33e79845710d7033efc2274e19e;hb=9459638ad6797b8139f1e9f0715c96076dbf0890;hp=6370826f5240b849c371c92a7b8f6dbad6020621;hpb=28f28705612902c15cd0702cc891fba35bf2d2df;p=synfig.git diff --git a/synfig-core/trunk/src/synfig/value.h b/synfig-core/trunk/src/synfig/value.h index 6370826..7079dba 100644 --- a/synfig-core/trunk/src/synfig/value.h +++ b/synfig-core/trunk/src/synfig/value.h @@ -2,19 +2,21 @@ /*! \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 Robert B. Quattlebaum Jr. +** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** Copyright (c) 2007, 2008 Chris Moore ** -** This software and associated documentation -** are CONFIDENTIAL and PROPRIETARY property of -** the above-mentioned copyright holder. +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. ** -** You may not copy, print, publish, or in any -** other way distribute this software without -** a prior written agreement with -** the copyright holder. +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. ** \endlegal */ /* ========================================================================= */ @@ -40,6 +42,10 @@ #include "blinepoint.h" #include "exception.h" +#ifdef USE_HALF_TYPE +#include +#endif + #ifndef SYNFIG_NO_ANGLE #include "angle.h" #endif @@ -61,7 +67,7 @@ class Segment; class Gradient; class BLinePoint; class Color; - + /*! \class ValueBase ** \todo writeme */ @@ -77,25 +83,25 @@ public: 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 @@ -103,7 +109,7 @@ public: TYPE_END //!< Not a valid type, used for sanity checks }; - + private: typedef std::vector list_type; @@ -113,16 +119,16 @@ private: */ 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 @@ -139,7 +145,7 @@ public: //! \writeme ~ValueBase(); - + /* -- ** -- O P E R A T O R S --------------------------------------------------- */ @@ -151,7 +157,7 @@ public: { set(x); return *this; } //! \writeme - ValueBase& operator=(const ValueBase& x); + ValueBase& operator=(const ValueBase& x); //! \writeme bool operator==(const ValueBase& rhs)const; @@ -176,14 +182,14 @@ public: 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; @@ -192,26 +198,37 @@ public: //! 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 bool - same_as(const T &x)const + same_type_as(const T &x)const { const Type testtype(get_type(x)); - - if(testtype==type)return true; - if( (type==TYPE_REAL || type==TYPE_TIME) && - (testtype==TYPE_REAL || testtype==TYPE_TIME) ) + + return same_type_as(type, testtype); + } + + bool same_type_as(const Type testtype)const + { + return same_type_as(type, testtype); + } + + //! Compares two types. Returns true if they are the same type. + static bool same_type_as(const Type type1, const Type type2) + { + if (type1 == type2) return true; + if ((type1 == TYPE_REAL || type1 == TYPE_TIME) && + (type2 == TYPE_REAL || type2 == TYPE_TIME)) return true; return false; } - - - // === GET MEMBERS ======================================================== + + + // === GET MEMBERS ======================================================== template - const T &get(const T& x)const + const T &get(const T& x __attribute__ ((unused)))const { - assert(is_valid() && same_as(x)); + assert(is_valid() && same_type_as(x)); return *static_cast(data); } float get(const float &)const { return get(Real()); } @@ -221,38 +238,43 @@ public: { return get(etl::loose_handle()); } const char* get(const char*)const; const list_type& get_list()const { return get(list_type()); } + +#ifdef _DEBUG + String get_string() const; +#endif // _DEBUG // ======================================================================== - - - - // === PUT MEMBERS ======================================================== + + + + // === PUT MEMBERS ======================================================== template void put(T* x)const { - assert(same_as(*x)); + assert(same_type_as(*x)); *x=*static_cast(data); } void put(float* x)const { *x=get(Real()); } void put(char** x)const; // ======================================================================== - - - // === SET MEMBERS ======================================================== + + + // === SET MEMBERS ======================================================== template 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(char* x); void set(Canvas*x); void set(etl::loose_handle x); void set(etl::handle x); template void set(const std::vector &x) { _set(list_type(x.begin(),x.end())); } template void set(const std::list &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 ------------------------------------- */ @@ -262,60 +284,49 @@ public: //! Returns a string containing the name of the given Type static String type_name(Type id); + //! Returns a string containing the translated name of the given Type + static String type_local_name(Type id); + //! Returns a the corresponding Type of the described type static Type ident_type(const String &str); - // === 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 Real&) { return TYPE_REAL; } - static const Type get_type(const float&) { return TYPE_REAL; } - static const Type get_type(const Vector&) { return TYPE_VECTOR; } - static const Type get_type(const Color&) { return TYPE_COLOR; } - static const Type get_type(const Segment&) { return TYPE_SEGMENT; } - static const Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; } - static const Type get_type(const String&) { return TYPE_STRING; } - static const Type get_type(const Gradient&) { return TYPE_GRADIENT; } - static const Type get_type(Canvas*) { return TYPE_CANVAS; } - static const Type get_type(const etl::handle&) + // === GET TYPE MEMBERS =================================================== + static Type get_type(bool) { return TYPE_BOOL; } + static Type get_type(int) { return TYPE_INTEGER; } + static Type get_type(const Time&) { return TYPE_TIME; } + static Type get_type(const Real&) { return TYPE_REAL; } + static Type get_type(const float&) { return TYPE_REAL; } + static Type get_type(const Vector&) { return TYPE_VECTOR; } + static Type get_type(const Color&) { return TYPE_COLOR; } + static Type get_type(const Segment&) { return TYPE_SEGMENT; } + static Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; } + static Type get_type(const String&) { return TYPE_STRING; } + static Type get_type(const Gradient&) { return TYPE_GRADIENT; } + static Type get_type(Canvas*) { return TYPE_CANVAS; } + static Type get_type(const etl::handle&) { return TYPE_CANVAS; } - static const Type get_type(const etl::loose_handle&) - { return TYPE_CANVAS; } - static const Type get_type(const list_type&) { return TYPE_LIST; } - template static const Type get_type(const std::vector &x) + static Type get_type(const etl::loose_handle&) + { return TYPE_CANVAS; } + static Type get_type(const list_type&) { return TYPE_LIST; } + template static Type get_type(const std::vector &/*x*/) { return TYPE_LIST; } - template static const Type get_type(const std::list &x) + template static Type get_type(const std::list &/*x*/) { return TYPE_LIST; } // ======================================================================== - + /* -- ** -- C A S T O P E R A T O R S ----------------------------------------- */ public: - template - operator std::list()const - { - assert(type==TYPE_LIST); - std::list ret(get_list().begin(),get_list().end()); - return ret; - } - template - operator std::vector()const - { - assert(type==TYPE_LIST); - std::vector ret(get_list().begin(),get_list().end()); - return ret; - } operator const list_type&()const { return get_list(); } //operator const Color&()const { return get(Color()); } - //operator const Real&()const { return get(Real()); } + 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()); } @@ -323,6 +334,7 @@ public: //operator const char *()const { return get(String()).c_str(); } operator const Segment&()const { return get(Segment()); } + /* -- ** -- O T H E R ----------------------------------------------------------- */ @@ -333,25 +345,40 @@ public: half get(const half &)const { return get(Real()); } void put(half*x)const { *x=get(Real()); } void set(const half &x) { _set(Real(x)); } - static const Type get_type(const half&) { return TYPE_REAL; } + static 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; } + static Type get_type(const Angle&) { return TYPE_ANGLE; } #endif - + template + operator std::list()const + { + assert(type==TYPE_LIST); + std::list ret(get_list().begin(),get_list().end()); + return ret; + } + template + operator std::vector()const + { + assert(type==TYPE_LIST); + std::vector ret(get_list().begin(),get_list().end()); + return ret; + } + + private: - + template void _set(const T& x) { const Type newtype(get_type(x)); - + assert(newtype!=TYPE_NIL); - + if(newtype==type) { if(ref_count.unique()) @@ -366,7 +393,7 @@ private: type=newtype; ref_count.reset(); data=new T(x); - } + } }; // END of class ValueBase @@ -380,21 +407,21 @@ public: 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(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& operator=(const T& x) { set(x); return *this; } @@ -403,11 +430,11 @@ public: Value& operator=(const ValueBase& x) { - if(!x.same_as(T())) + if(!x.same_type_as(T())) throw Exception::BadType("Value(ValueBase): Type Mismatch"); return ValueBase::operator=(x); } - + }; // END of class Value /* @@ -420,17 +447,17 @@ public: } Value(const ValueBase &x):ValueBase(x) { - if(!x.same_as(T())) + if(!x.same_type_as(T())) throw Exception::BadType("Value(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& operator=(const T& x) { set(x); return *this; } @@ -439,11 +466,11 @@ public: Value& operator=(const ValueBase& x) { - if(!x.same_as(T())) + if(!x.same_type_as(T())) throw Exception::BadType("Value(ValueBase): Type Mismatch"); return ValueBase::operator=(x); } - + }; // END of class Value */