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 ======================================= */
66 ** \brief Base class for the Values of Synfig
71 -- ** -- T Y P E S -----------------------------------------------------------
76 //! This enum lists all the types of values
79 TYPE_NIL=0, //!< Represents an empty value
81 TYPE_BOOL, //!< Boolean value (1 or 0)
82 TYPE_INTEGER, //!< Integer value -1, 0, 1, etc.
83 TYPE_ANGLE, //!< Angle value (Real number internally)
85 // All types after this point are larger than 32 bits
87 TYPE_TIME, //!< Time value
88 TYPE_REAL, //!< Real value (floating point number)
90 // All types after this point are larger than 64 bits
92 TYPE_VECTOR, //!< Vector value (Real, Real) Points are Vectors too
93 TYPE_COLOR, //!< Color (Real, Real, Real, Real)
94 TYPE_SEGMENT, //!< Segment Point and Vector
95 TYPE_BLINEPOINT, //!< BLinePoint Origin (Point) 2xTangents (Vector) Width (Real), Origin (Real) Split Tangent (Boolean)
97 // All types after this point require construction/destruction
99 TYPE_LIST, //!< List of any of above
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 -------------------------------------------------------------
116 //! The type of value
118 //! Pointer to hold the data of the value
120 //! Counter of Value Nodes that refers to this Value Base
121 //! Value base can only be destructed if the ref_count is not greater than 0
122 //!\see etl::reference_counter
123 etl::reference_counter ref_count;
124 //! For Values with loop option like TYPE_LIST
128 -- ** -- C O N S T R U C T O R S -----------------------------------
133 //! Default constructor
136 //! Template constructor for any type
137 template <typename T>
138 ValueBase(const T &x, bool loop_=false):
139 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_)
142 //! Copy constructor. The data is not copied, just the type.
145 //! Default destructor
149 -- ** -- O P E R A T O R S ---------------------------------------------------
154 //! Template for the operator assignation operator for non ValueBase classes
156 template <class T> ValueBase& operator=(const T& x)
157 { set(x); return *this; }
159 //!Operator asignation for ValueBase classes. Does a exact copy of \x
160 ValueBase& operator=(const ValueBase& x);
162 //! Eqaul than operator. Segment, Gradient and Bline Points cannot be compared.
163 bool operator==(const ValueBase& rhs)const;
165 //! Not equal than operator.
166 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
168 //! Constant index operator for when value is of type TYPE_LIST
169 const ValueBase &operator[](int index)const
170 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
173 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
178 //! Deletes the data only if the ref count is zero
181 //! Gets the loop option.
182 bool get_loop()const { return loop_; }
184 //! Sets the loop option.
185 void set_loop(bool x) { loop_=x; }
187 //! True if the Value is not valid or is type LIST and is empty
190 //! Gets the contained type in the Value Base
191 Type get_contained_type()const;
193 //! Returns true if the contained value is defined and valid.
194 bool is_valid()const;
196 //! Returns a string containing the name of the type. Used for sif files
197 String type_name()const { return type_name(type); }
199 //! Returns the type of the contained value
200 const Type & get_type()const { return type; }
202 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
203 //! Template for any class
204 template <class T> bool
205 same_type_as(const T &x)const
207 const Type testtype(get_type(x));
209 return same_type_as(type, testtype);
211 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
212 bool same_type_as(const Type testtype)const
214 return same_type_as(type, testtype);
217 //! Compares two types. Returns true if they are the same type.
218 static bool same_type_as(const Type type1, const Type type2)
220 if (type1 == type2) return true;
221 if ((type1 == TYPE_REAL || type1 == TYPE_TIME) &&
222 (type2 == TYPE_REAL || type2 == TYPE_TIME))
228 // === GET MEMBERS ========================================================
229 //! Template to get the ValueBase class data by casting the type
230 template <typename T>
231 const T &get(const T& x __attribute__ ((unused)))const
233 assert(is_valid() && same_type_as(x));
234 return *static_cast<const T*>(data);
236 //! Gets the Real part of the data
237 float get(const float &)const { return get(Real()); }
238 //! Gets the Canvas Handle part of the data based on Canvas Handle type
239 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
240 { return get(etl::loose_handle<Canvas>()); }
241 //! Gets the Canvas Handle part of the data based on Canvas pointer type
242 etl::loose_handle<Canvas> get(Canvas*)const
243 { return get(etl::loose_handle<Canvas>()); }
244 //! Gets the data as char pointer based on char pointer
245 const char* get(const char*)const;
246 //! Gets the data as List Type
247 const list_type& get_list()const { return get(list_type()); }
250 String get_string() const;
252 // ========================================================================
256 // === PUT MEMBERS ========================================================
257 //! Put template for any class
258 template <typename T>
261 assert(same_type_as(*x));
262 *x=*static_cast<const T*>(data);
264 //! Put for float values
265 void put(float* x)const { *x=get(Real()); }
266 //! Put for char values (Not defined??)
267 void put(char** x)const;
268 // ========================================================================
272 // === SET MEMBERS ========================================================
273 //! Set template for any class
274 template <typename T> void set(const T& x) { _set(x); }
276 void set(const float &x) { _set(Real(x)); }
277 //! Set for List Type
278 void set(const list_type &x);
279 //! Set for char string
280 void set(const char* x);
281 //! Set for char string
283 //! Set for Canvas pointer
285 //! Set for Canvas handle
286 void set(etl::loose_handle<Canvas> x);
287 //! Set for Canvas handle
288 void set(etl::handle<Canvas> x);
289 //! Set template for standar vector
290 template <class T> void set(const std::vector<T> &x)
291 { _set(list_type(x.begin(),x.end())); }
292 //! Set template for standar list
293 template <class T> void set(const std::list<T> &x)
294 { _set(list_type(x.begin(),x.end())); }
295 // ========================================================================
299 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
304 //! Returns a string containing the name of the given Type
305 static String type_name(Type id);
307 //! Returns a string containing the translated name of the given Type
308 static String type_local_name(Type id);
310 //! Returns a the corresponding Type of the described type.
311 //! Notice that this is used in the loadcanvas. It should keep all
312 //! all type names used in previous sif files
313 static Type ident_type(const String &str);
316 // === GET TYPE MEMBERS ===================================================
317 static Type get_type(bool) { return TYPE_BOOL; }
318 static Type get_type(int) { return TYPE_INTEGER; }
319 static Type get_type(const Angle&) { return TYPE_ANGLE; }
320 static Type get_type(const Time&) { return TYPE_TIME; }
321 static Type get_type(const Real&) { return TYPE_REAL; }
322 static Type get_type(const float&) { return TYPE_REAL; }
323 static Type get_type(const Vector&) { return TYPE_VECTOR; }
324 static Type get_type(const Color&) { return TYPE_COLOR; }
325 static Type get_type(const Segment&) { return TYPE_SEGMENT; }
326 static Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
327 static Type get_type(const String&) { return TYPE_STRING; }
328 static Type get_type(const Gradient&) { return TYPE_GRADIENT; }
329 static Type get_type(Canvas*) { return TYPE_CANVAS; }
330 static Type get_type(const etl::handle<Canvas>&)
331 { return TYPE_CANVAS; }
332 static Type get_type(const etl::loose_handle<Canvas>&)
333 { return TYPE_CANVAS; }
334 static Type get_type(const list_type&) { return TYPE_LIST; }
335 template <class T> static Type get_type(const std::vector<T> &/*x*/)
336 { return TYPE_LIST; }
337 template <class T> static Type get_type(const std::list<T> &/*x*/)
338 { return TYPE_LIST; }
339 // ========================================================================
343 -- ** -- C A S T O P E R A T O R S -----------------------------------------
347 //! I wonder why are those casting operators disabled...
348 operator const list_type&()const { return get_list(); }
349 operator const Angle&()const { return get(Angle()); }
350 //operator const Color&()const { return get(Color()); }
351 operator const Real&()const { return get(Real()); }
352 //operator const Time&()const { return get(Time()); }
354 operator const Vector&()const { return get(Vector()); }
355 operator const BLinePoint&()const { return get(BLinePoint()); }
356 //operator const int&()const { return get(int()); }
357 //operator const String&()const { return get(String()); }
358 //operator const char *()const { return get(String()).c_str(); }
359 operator const Segment&()const { return get(Segment()); }
363 -- ** -- O T H E R -----------------------------------------------------------
369 half get(const half &)const { return get(Real()); }
370 void put(half*x)const { *x=get(Real()); }
371 void set(const half &x) { _set(Real(x)); }
372 static Type get_type(const half&) { return TYPE_REAL; }
373 operator half()const { return get(Real()); }
377 //! Cast operator template to obtain the standard list from the TYPE LIST
379 operator std::list<T>()const
381 assert(type==TYPE_LIST);
382 std::list<T> ret(get_list().begin(),get_list().end());
385 //! Cast operator template to obtain the standard vector from the TYPE LIST
387 operator std::vector<T>()const
389 assert(type==TYPE_LIST);
390 std::vector<T> ret(get_list().begin(),get_list().end());
396 //! Internal set template. Takes in consideration the reference counter
397 template <typename T> void
400 const Type newtype(get_type(x));
402 assert(newtype!=TYPE_NIL);
406 if(ref_count.unique())
408 *reinterpret_cast<T*>(data)=x;
420 }; // END of class ValueBase
424 ** \brief Template for all the valid Value Types
427 class Value : public ValueBase
430 Value(const T &x):ValueBase(x)
434 Value(const ValueBase &x):ValueBase(x)
436 if(!x.same_type_as(T()))
437 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
444 T get()const { return ValueBase::get(T()); }
446 void put(T* x)const { ValueBase::put(x); }
448 void set(const T& x) { ValueBase::operator=(x); }
450 Value<T>& operator=(const T& x) { set(x); return *this; }
452 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
454 Value<T>& operator=(const ValueBase& x)
456 if(!x.same_type_as(T()))
457 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
458 return ValueBase::operator=(x);
461 }; // END of class Value
463 }; // END of namespace synfig
465 /* === E N D =============================================================== */