1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
5 ** $Id: value.h,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === S T A R T =========================================================== */
25 #ifndef __SYNFIG_VALUE_H
26 #define __SYNFIG_VALUE_H
28 /* === H E A D E R S ======================================================= */
37 #include <ETL/trivial>
40 //#include "gradient.h"
41 #include "blinepoint.h"
42 #include "exception.h"
45 #include <OpenEXR/half.h>
48 #ifndef SYNFIG_NO_ANGLE
52 #include <ETL/ref_count>
54 /* === M A C R O S ========================================================= */
56 /* === T Y P E D E F S ===================================================== */
58 /* === C L A S S E S & S T R U C T S ======================================= */
76 -- ** -- T Y P E S -----------------------------------------------------------
84 TYPE_NIL=0, //!< Represents an empty value
88 TYPE_ANGLE, //!< Angle
90 // All types after this point are larger than 32 bits
95 // All types after this point are larger than 64 bits
97 TYPE_VECTOR, //!< Vector
98 TYPE_COLOR, //!< Color
99 TYPE_SEGMENT, //!< Segment
100 TYPE_BLINEPOINT, //!< BLinePoint
102 // All types after this point require construction/destruction
105 TYPE_CANVAS, //!< Canvas
106 TYPE_STRING, //!< String
107 TYPE_GRADIENT, //!< Color Gradient
109 TYPE_END //!< Not a valid type, used for sanity checks
114 typedef std::vector<ValueBase> list_type;
117 -- ** -- D A T A -------------------------------------------------------------
124 etl::reference_counter ref_count;
128 -- ** -- C O N S T R U C T O R S -----------------------------------
137 template <typename T>
138 ValueBase(const T &x, bool loop_=false):
139 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_)
149 -- ** -- O P E R A T O R S ---------------------------------------------------
155 template <class T> ValueBase& operator=(const T& x)
156 { set(x); return *this; }
159 ValueBase& operator=(const ValueBase& x);
162 bool operator==(const ValueBase& rhs)const;
165 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
167 //! Constant index operator for when value is of type TYPE_LIST
168 const ValueBase &operator[](int index)const
169 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
172 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
181 bool get_loop()const { return loop_; }
184 void set_loop(bool x) { loop_=x; }
190 Type get_contained_type()const;
192 //! Returns true if the contained value is defined and valid.
193 bool is_valid()const;
195 //! Returns a string containing the name of the type
196 String type_name()const { return type_name(type); }
198 //! Returns the type of the contained value
199 const Type & get_type()const { return type; }
201 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
202 template <class T> bool
203 same_as(const T &x)const
205 const Type testtype(get_type(x));
207 if(testtype==type)return true;
208 if( (type==TYPE_REAL || type==TYPE_TIME) &&
209 (testtype==TYPE_REAL || testtype==TYPE_TIME) )
215 // === GET MEMBERS ========================================================
216 template <typename T>
217 const T &get(const T& x)const
219 assert(is_valid() && same_as(x));
220 return *static_cast<const T*>(data);
222 float get(const float &)const { return get(Real()); }
223 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
224 { return get(etl::loose_handle<Canvas>()); }
225 etl::loose_handle<Canvas> get(Canvas*)const
226 { return get(etl::loose_handle<Canvas>()); }
227 const char* get(const char*)const;
228 const list_type& get_list()const { return get(list_type()); }
229 // ========================================================================
233 // === PUT MEMBERS ========================================================
234 template <typename T>
238 *x=*static_cast<const T*>(data);
240 void put(float* x)const { *x=get(Real()); }
241 void put(char** x)const;
242 // ========================================================================
246 // === SET MEMBERS ========================================================
247 template <typename T> void set(const T& x) { _set(x); }
248 void set(const float &x) { _set(Real(x)); }
249 void set(const list_type &x);
250 void set(const char* x);
252 void set(etl::loose_handle<Canvas> x);
253 void set(etl::handle<Canvas> x);
254 template <class T> void set(const std::vector<T> &x)
255 { _set(list_type(x.begin(),x.end())); }
256 template <class T> void set(const std::list<T> &x)
257 { _set(list_type(x.begin(),x.end())); }
258 // ========================================================================
262 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
267 //! Returns a string containing the name of the given Type
268 static String type_name(Type id);
270 //! Returns a the corresponding Type of the described type
271 static Type ident_type(const String &str);
274 // === GET TYPE MEMBERS ===================================================
275 static const Type get_type(bool) { return TYPE_BOOL; }
276 static const Type get_type(int) { return TYPE_INTEGER; }
277 static const Type get_type(const Time&) { return TYPE_TIME; }
278 static const Type get_type(const Real&) { return TYPE_REAL; }
279 static const Type get_type(const float&) { return TYPE_REAL; }
280 static const Type get_type(const Vector&) { return TYPE_VECTOR; }
281 static const Type get_type(const Color&) { return TYPE_COLOR; }
282 static const Type get_type(const Segment&) { return TYPE_SEGMENT; }
283 static const Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
284 static const Type get_type(const String&) { return TYPE_STRING; }
285 static const Type get_type(const Gradient&) { return TYPE_GRADIENT; }
286 static const Type get_type(Canvas*) { return TYPE_CANVAS; }
287 static const Type get_type(const etl::handle<Canvas>&)
288 { return TYPE_CANVAS; }
289 static const Type get_type(const etl::loose_handle<Canvas>&)
290 { return TYPE_CANVAS; }
291 static const Type get_type(const list_type&) { return TYPE_LIST; }
292 template <class T> static const Type get_type(const std::vector<T> &x)
293 { return TYPE_LIST; }
294 template <class T> static const Type get_type(const std::list<T> &x)
295 { return TYPE_LIST; }
296 // ========================================================================
300 -- ** -- C A S T O P E R A T O R S -----------------------------------------
305 operator const list_type&()const { return get_list(); }
306 //operator const Color&()const { return get(Color()); }
307 //operator const Real&()const { return get(Real()); }
308 //operator const Time&()const { return get(Time()); }
310 operator const Vector&()const { return get(Vector()); }
311 operator const BLinePoint&()const { return get(BLinePoint()); }
312 //operator const int&()const { return get(int()); }
313 //operator const String&()const { return get(String()); }
314 //operator const char *()const { return get(String()).c_str(); }
315 operator const Segment&()const { return get(Segment()); }
319 -- ** -- O T H E R -----------------------------------------------------------
325 half get(const half &)const { return get(Real()); }
326 void put(half*x)const { *x=get(Real()); }
327 void set(const half &x) { _set(Real(x)); }
328 static const Type get_type(const half&) { return TYPE_REAL; }
329 operator half()const { return get(Real()); }
332 #ifndef SYNFIG_NO_ANGLE
333 operator const Angle&()const { return get(Angle()); }
334 static const Type get_type(const Angle&) { return TYPE_ANGLE; }
338 operator std::list<T>()const
340 assert(type==TYPE_LIST);
341 std::list<T> ret(get_list().begin(),get_list().end());
345 operator std::vector<T>()const
347 assert(type==TYPE_LIST);
348 std::vector<T> ret(get_list().begin(),get_list().end());
355 template <typename T> void
358 const Type newtype(get_type(x));
360 assert(newtype!=TYPE_NIL);
364 if(ref_count.unique())
366 *reinterpret_cast<T*>(data)=x;
377 }; // END of class ValueBase
384 class Value : public ValueBase
387 Value(const T &x):ValueBase(x)
391 Value(const ValueBase &x):ValueBase(x)
394 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
401 T get()const { return ValueBase::get(T()); }
403 void put(T* x)const { ValueBase::put(x); }
405 void set(const T& x) { ValueBase::operator=(x); }
407 Value<T>& operator=(const T& x) { set(x); return *this; }
409 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
411 Value<T>& operator=(const ValueBase& x)
414 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
415 return ValueBase::operator=(x);
418 }; // END of class Value
422 class Value< std::list<CT> > : public ValueBase
425 Value(const T &x):ValueBase(x)
428 Value(const ValueBase &x):ValueBase(x)
431 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
437 T get()const { return ValueBase::get(T()); }
439 void put(T* x)const { ValueBase::put(x); }
441 void set(const T& x) { ValueBase::operator=(x); }
443 Value<T>& operator=(const T& x) { set(x); return *this; }
445 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
447 Value<T>& operator=(const ValueBase& x)
450 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
451 return ValueBase::operator=(x);
454 }; // END of class Value
457 }; // END of namespace synfig
459 /* === E N D =============================================================== */