1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === S T A R T =========================================================== */
26 #ifndef __SYNFIG_VALUE_H
27 #define __SYNFIG_VALUE_H
29 /* === H E A D E R S ======================================================= */
36 #include <ETL/trivial>
39 #include "blinepoint.h"
40 #include "exception.h"
43 #include <OpenEXR/half.h>
47 #include <ETL/ref_count>
49 /* === M A C R O S ========================================================= */
51 /* === T Y P E D E F S ===================================================== */
53 /* === C L A S S E S & S T R U C T S ======================================= */
71 -- ** -- T Y P E S -----------------------------------------------------------
79 TYPE_NIL=0, //!< Represents an empty value
83 TYPE_ANGLE, //!< Angle
85 // All types after this point are larger than 32 bits
90 // All types after this point are larger than 64 bits
92 TYPE_VECTOR, //!< Vector
93 TYPE_COLOR, //!< Color
94 TYPE_SEGMENT, //!< Segment
95 TYPE_BLINEPOINT, //!< BLinePoint
97 // All types after this point require construction/destruction
100 TYPE_CANVAS, //!< Canvas
101 TYPE_STRING, //!< String
102 TYPE_GRADIENT, //!< Color Gradient
104 TYPE_END //!< Not a valid type, used for sanity checks
109 typedef std::vector<ValueBase> list_type;
112 -- ** -- D A T A -------------------------------------------------------------
119 etl::reference_counter ref_count;
123 -- ** -- C O N S T R U C T O R S -----------------------------------
132 template <typename T>
133 ValueBase(const T &x, bool loop_=false):
134 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_)
144 -- ** -- O P E R A T O R S ---------------------------------------------------
150 template <class T> ValueBase& operator=(const T& x)
151 { set(x); return *this; }
154 ValueBase& operator=(const ValueBase& x);
157 bool operator==(const ValueBase& rhs)const;
160 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
162 //! Constant index operator for when value is of type TYPE_LIST
163 const ValueBase &operator[](int index)const
164 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
167 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
176 bool get_loop()const { return loop_; }
179 void set_loop(bool x) { loop_=x; }
185 Type get_contained_type()const;
187 //! Returns true if the contained value is defined and valid.
188 bool is_valid()const;
190 //! Returns a string containing the name of the type
191 String type_name()const { return type_name(type); }
193 //! Returns the type of the contained value
194 const Type & get_type()const { return type; }
196 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
197 template <class T> bool
198 same_type_as(const T &x)const
200 const Type testtype(get_type(x));
202 return same_type_as(type, testtype);
205 bool same_type_as(const Type testtype)const
207 return same_type_as(type, testtype);
210 //! Compares two types. Returns true if they are the same type.
211 static bool same_type_as(const Type type1, const Type type2)
213 if (type1 == type2) return true;
214 if ((type1 == TYPE_REAL || type1 == TYPE_TIME) &&
215 (type2 == TYPE_REAL || type2 == TYPE_TIME))
221 // === GET MEMBERS ========================================================
222 template <typename T>
223 const T &get(const T& x __attribute__ ((unused)))const
225 assert(is_valid() && same_type_as(x));
226 return *static_cast<const T*>(data);
228 float get(const float &)const { return get(Real()); }
229 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
230 { return get(etl::loose_handle<Canvas>()); }
231 etl::loose_handle<Canvas> get(Canvas*)const
232 { return get(etl::loose_handle<Canvas>()); }
233 const char* get(const char*)const;
234 const list_type& get_list()const { return get(list_type()); }
237 String get_string() const;
239 // ========================================================================
243 // === PUT MEMBERS ========================================================
244 template <typename T>
247 assert(same_type_as(*x));
248 *x=*static_cast<const T*>(data);
250 void put(float* x)const { *x=get(Real()); }
251 void put(char** x)const;
252 // ========================================================================
256 // === SET MEMBERS ========================================================
257 template <typename T> void set(const T& x) { _set(x); }
258 void set(const float &x) { _set(Real(x)); }
259 void set(const list_type &x);
260 void set(const char* x);
263 void set(etl::loose_handle<Canvas> x);
264 void set(etl::handle<Canvas> x);
265 template <class T> void set(const std::vector<T> &x)
266 { _set(list_type(x.begin(),x.end())); }
267 template <class T> void set(const std::list<T> &x)
268 { _set(list_type(x.begin(),x.end())); }
269 // ========================================================================
273 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
278 //! Returns a string containing the name of the given Type
279 static String type_name(Type id);
281 //! Returns a string containing the translated name of the given Type
282 static String type_local_name(Type id);
284 //! Returns a the corresponding Type of the described type
285 static Type ident_type(const String &str);
288 // === GET TYPE MEMBERS ===================================================
289 static Type get_type(bool) { return TYPE_BOOL; }
290 static Type get_type(int) { return TYPE_INTEGER; }
291 static Type get_type(const Time&) { return TYPE_TIME; }
292 static Type get_type(const Real&) { return TYPE_REAL; }
293 static Type get_type(const float&) { return TYPE_REAL; }
294 static Type get_type(const Vector&) { return TYPE_VECTOR; }
295 static Type get_type(const Color&) { return TYPE_COLOR; }
296 static Type get_type(const Segment&) { return TYPE_SEGMENT; }
297 static Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
298 static Type get_type(const String&) { return TYPE_STRING; }
299 static Type get_type(const Gradient&) { return TYPE_GRADIENT; }
300 static Type get_type(Canvas*) { return TYPE_CANVAS; }
301 static Type get_type(const etl::handle<Canvas>&)
302 { return TYPE_CANVAS; }
303 static Type get_type(const etl::loose_handle<Canvas>&)
304 { return TYPE_CANVAS; }
305 static Type get_type(const list_type&) { return TYPE_LIST; }
306 template <class T> static Type get_type(const std::vector<T> &/*x*/)
307 { return TYPE_LIST; }
308 template <class T> static Type get_type(const std::list<T> &/*x*/)
309 { return TYPE_LIST; }
310 // ========================================================================
314 -- ** -- C A S T O P E R A T O R S -----------------------------------------
319 operator const list_type&()const { return get_list(); }
320 //operator const Color&()const { return get(Color()); }
321 operator const Real&()const { return get(Real()); }
322 //operator const Time&()const { return get(Time()); }
324 operator const Vector&()const { return get(Vector()); }
325 operator const BLinePoint&()const { return get(BLinePoint()); }
326 //operator const int&()const { return get(int()); }
327 //operator const String&()const { return get(String()); }
328 //operator const char *()const { return get(String()).c_str(); }
329 operator const Segment&()const { return get(Segment()); }
333 -- ** -- O T H E R -----------------------------------------------------------
339 half get(const half &)const { return get(Real()); }
340 void put(half*x)const { *x=get(Real()); }
341 void set(const half &x) { _set(Real(x)); }
342 static Type get_type(const half&) { return TYPE_REAL; }
343 operator half()const { return get(Real()); }
346 operator const Angle&()const { return get(Angle()); }
347 static Type get_type(const Angle&) { return TYPE_ANGLE; }
350 operator std::list<T>()const
352 assert(type==TYPE_LIST);
353 std::list<T> ret(get_list().begin(),get_list().end());
357 operator std::vector<T>()const
359 assert(type==TYPE_LIST);
360 std::vector<T> ret(get_list().begin(),get_list().end());
367 template <typename T> void
370 const Type newtype(get_type(x));
372 assert(newtype!=TYPE_NIL);
376 if(ref_count.unique())
378 *reinterpret_cast<T*>(data)=x;
389 }; // END of class ValueBase
396 class Value : public ValueBase
399 Value(const T &x):ValueBase(x)
403 Value(const ValueBase &x):ValueBase(x)
405 if(!x.same_type_as(T()))
406 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
413 T get()const { return ValueBase::get(T()); }
415 void put(T* x)const { ValueBase::put(x); }
417 void set(const T& x) { ValueBase::operator=(x); }
419 Value<T>& operator=(const T& x) { set(x); return *this; }
421 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
423 Value<T>& operator=(const ValueBase& x)
425 if(!x.same_type_as(T()))
426 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
427 return ValueBase::operator=(x);
430 }; // END of class Value
434 class Value< std::list<CT> > : public ValueBase
437 Value(const T &x):ValueBase(x)
440 Value(const ValueBase &x):ValueBase(x)
442 if(!x.same_type_as(T()))
443 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
449 T get()const { return ValueBase::get(T()); }
451 void put(T* x)const { ValueBase::put(x); }
453 void set(const T& x) { ValueBase::operator=(x); }
455 Value<T>& operator=(const T& x) { set(x); return *this; }
457 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
459 Value<T>& operator=(const ValueBase& x)
461 if(!x.same_type_as(T()))
462 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
463 return ValueBase::operator=(x);
466 }; // END of class Value
469 }; // END of namespace synfig
471 /* === E N D =============================================================== */