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"
44 #ifndef SYNFIG_NO_ANGLE
48 #include <ETL/ref_count>
50 /* === M A C R O S ========================================================= */
52 /* === T Y P E D E F S ===================================================== */
54 /* === C L A S S E S & S T R U C T S ======================================= */
72 -- ** -- T Y P E S -----------------------------------------------------------
80 TYPE_NIL=0, //!< Represents an empty value
84 TYPE_ANGLE, //!< Angle
86 // All types after this point are larger than 32 bits
91 // All types after this point are larger than 64 bits
93 TYPE_VECTOR, //!< Vector
94 TYPE_COLOR, //!< Color
95 TYPE_SEGMENT, //!< Segment
96 TYPE_BLINEPOINT, //!< BLinePoint
98 // All types after this point require construction/destruction
101 TYPE_CANVAS, //!< Canvas
102 TYPE_STRING, //!< String
103 TYPE_GRADIENT, //!< Color Gradient
105 TYPE_END //!< Not a valid type, used for sanity checks
110 typedef std::vector<ValueBase> list_type;
113 -- ** -- D A T A -------------------------------------------------------------
120 etl::reference_counter ref_count;
124 -- ** -- C O N S T R U C T O R S -----------------------------------
133 template <typename T>
134 ValueBase(const T &x, bool loop_=false):
135 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_)
145 -- ** -- O P E R A T O R S ---------------------------------------------------
151 template <class T> ValueBase& operator=(const T& x)
152 { set(x); return *this; }
155 ValueBase& operator=(const ValueBase& x);
158 bool operator==(const ValueBase& rhs)const;
161 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
163 //! Constant index operator for when value is of type TYPE_LIST
164 const ValueBase &operator[](int index)const
165 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
168 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
177 bool get_loop()const { return loop_; }
180 void set_loop(bool x) { loop_=x; }
186 Type get_contained_type()const;
188 //! Returns true if the contained value is defined and valid.
189 bool is_valid()const;
191 //! Returns a string containing the name of the type
192 String type_name()const { return type_name(type); }
194 //! Returns the type of the contained value
195 const Type & get_type()const { return type; }
197 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
198 template <class T> bool
199 same_as(const T &x)const
201 const Type testtype(get_type(x));
203 if(testtype==type)return true;
204 if( (type==TYPE_REAL || type==TYPE_TIME) &&
205 (testtype==TYPE_REAL || testtype==TYPE_TIME) )
211 // === GET MEMBERS ========================================================
212 template <typename T>
213 const T &get(const T& x)const
215 assert(is_valid() && same_as(x));
216 return *static_cast<const T*>(data);
218 float get(const float &)const { return get(Real()); }
219 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
220 { return get(etl::loose_handle<Canvas>()); }
221 etl::loose_handle<Canvas> get(Canvas*)const
222 { return get(etl::loose_handle<Canvas>()); }
223 const char* get(const char*)const;
224 const list_type& get_list()const { return get(list_type()); }
225 // ========================================================================
229 // === PUT MEMBERS ========================================================
230 template <typename T>
234 *x=*static_cast<const T*>(data);
236 void put(float* x)const { *x=get(Real()); }
237 void put(char** x)const;
238 // ========================================================================
242 // === SET MEMBERS ========================================================
243 template <typename T> void set(const T& x) { _set(x); }
244 void set(const float &x) { _set(Real(x)); }
245 void set(const list_type &x);
246 void set(const char* x);
248 void set(etl::loose_handle<Canvas> x);
249 void set(etl::handle<Canvas> x);
250 template <class T> void set(const std::vector<T> &x)
251 { _set(list_type(x.begin(),x.end())); }
252 template <class T> void set(const std::list<T> &x)
253 { _set(list_type(x.begin(),x.end())); }
254 // ========================================================================
258 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
263 //! Returns a string containing the name of the given Type
264 static String type_name(Type id);
266 //! Returns a the corresponding Type of the described type
267 static Type ident_type(const String &str);
270 // === GET TYPE MEMBERS ===================================================
271 static const Type get_type(bool) { return TYPE_BOOL; }
272 static const Type get_type(int) { return TYPE_INTEGER; }
273 static const Type get_type(const Time&) { return TYPE_TIME; }
274 static const Type get_type(const Real&) { return TYPE_REAL; }
275 static const Type get_type(const float&) { return TYPE_REAL; }
276 static const Type get_type(const Vector&) { return TYPE_VECTOR; }
277 static const Type get_type(const Color&) { return TYPE_COLOR; }
278 static const Type get_type(const Segment&) { return TYPE_SEGMENT; }
279 static const Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
280 static const Type get_type(const String&) { return TYPE_STRING; }
281 static const Type get_type(const Gradient&) { return TYPE_GRADIENT; }
282 static const Type get_type(Canvas*) { return TYPE_CANVAS; }
283 static const Type get_type(const etl::handle<Canvas>&)
284 { return TYPE_CANVAS; }
285 static const Type get_type(const etl::loose_handle<Canvas>&)
286 { return TYPE_CANVAS; }
287 static const Type get_type(const list_type&) { return TYPE_LIST; }
288 template <class T> static const Type get_type(const std::vector<T> &x)
289 { return TYPE_LIST; }
290 template <class T> static const Type get_type(const std::list<T> &x)
291 { return TYPE_LIST; }
292 // ========================================================================
296 -- ** -- C A S T O P E R A T O R S -----------------------------------------
301 operator const list_type&()const { return get_list(); }
302 //operator const Color&()const { return get(Color()); }
303 //operator const Real&()const { return get(Real()); }
304 //operator const Time&()const { return get(Time()); }
306 operator const Vector&()const { return get(Vector()); }
307 operator const BLinePoint&()const { return get(BLinePoint()); }
308 //operator const int&()const { return get(int()); }
309 //operator const String&()const { return get(String()); }
310 //operator const char *()const { return get(String()).c_str(); }
311 operator const Segment&()const { return get(Segment()); }
315 -- ** -- O T H E R -----------------------------------------------------------
321 half get(const half &)const { return get(Real()); }
322 void put(half*x)const { *x=get(Real()); }
323 void set(const half &x) { _set(Real(x)); }
324 static const Type get_type(const half&) { return TYPE_REAL; }
325 operator half()const { return get(Real()); }
328 #ifndef SYNFIG_NO_ANGLE
329 operator const Angle&()const { return get(Angle()); }
330 static const Type get_type(const Angle&) { return TYPE_ANGLE; }
334 operator std::list<T>()const
336 assert(type==TYPE_LIST);
337 std::list<T> ret(get_list().begin(),get_list().end());
341 operator std::vector<T>()const
343 assert(type==TYPE_LIST);
344 std::vector<T> ret(get_list().begin(),get_list().end());
351 template <typename T> void
354 const Type newtype(get_type(x));
356 assert(newtype!=TYPE_NIL);
360 if(ref_count.unique())
362 *reinterpret_cast<T*>(data)=x;
373 }; // END of class ValueBase
380 class Value : public ValueBase
383 Value(const T &x):ValueBase(x)
387 Value(const ValueBase &x):ValueBase(x)
390 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
397 T get()const { return ValueBase::get(T()); }
399 void put(T* x)const { ValueBase::put(x); }
401 void set(const T& x) { ValueBase::operator=(x); }
403 Value<T>& operator=(const T& x) { set(x); return *this; }
405 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
407 Value<T>& operator=(const ValueBase& x)
410 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
411 return ValueBase::operator=(x);
414 }; // END of class Value
418 class Value< std::list<CT> > : public ValueBase
421 Value(const T &x):ValueBase(x)
424 Value(const ValueBase &x):ValueBase(x)
427 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
433 T get()const { return ValueBase::get(T()); }
435 void put(T* x)const { ValueBase::put(x); }
437 void set(const T& x) { ValueBase::operator=(x); }
439 Value<T>& operator=(const T& x) { set(x); return *this; }
441 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
443 Value<T>& operator=(const ValueBase& x)
446 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
447 return ValueBase::operator=(x);
450 }; // END of class Value
453 }; // END of namespace synfig
455 /* === E N D =============================================================== */