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
126 //! For Values of Constant Value Nodes
130 -- ** -- C O N S T R U C T O R S -----------------------------------
135 //! Default constructor
138 //! Template constructor for any type
139 template <typename T>
140 ValueBase(const T &x, bool loop_=false, bool static_=false):
141 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_), static_(static_)
144 //! Copy constructor. The data is not copied, just the type.
147 //! Default destructor
151 -- ** -- O P E R A T O R S ---------------------------------------------------
156 //! Template for the operator assignation operator for non ValueBase classes
158 template <class T> ValueBase& operator=(const T& x)
159 { set(x); return *this; }
161 //!Operator asignation for ValueBase classes. Does a exact copy of \x
162 ValueBase& operator=(const ValueBase& x);
164 //! Eqaul than operator. Segment, Gradient and Bline Points cannot be compared.
165 bool operator==(const ValueBase& rhs)const;
167 //! Not equal than operator.
168 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
170 //! Constant index operator for when value is of type TYPE_LIST
171 const ValueBase &operator[](int index)const
172 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
175 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
180 //! Deletes the data only if the ref count is zero
183 //! Gets the loop option.
184 bool get_loop()const { return loop_; }
186 //! Sets the loop option.
187 void set_loop(bool x) { loop_=x; }
189 //! Gets the static option.
190 bool get_static()const { return static_; }
192 //! Sets the static option.
193 void set_static(bool x) { static_=x; }
195 //! True if the Value is not valid or is type LIST and is empty
198 //! Gets the contained type in the Value Base
199 Type get_contained_type()const;
201 //! Returns true if the contained value is defined and valid.
202 bool is_valid()const;
204 //! Returns a string containing the name of the type. Used for sif files
205 String type_name()const { return type_name(type); }
207 //! Returns the type of the contained value
208 const Type & get_type()const { return type; }
210 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
211 //! Template for any class
212 template <class T> bool
213 same_type_as(const T &x)const
215 const Type testtype(get_type(x));
217 return same_type_as(type, testtype);
219 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
220 bool same_type_as(const Type testtype)const
222 return same_type_as(type, testtype);
225 //! Compares two types. Returns true if they are the same type.
226 static bool same_type_as(const Type type1, const Type type2)
228 if (type1 == type2) return true;
229 if ((type1 == TYPE_REAL || type1 == TYPE_TIME) &&
230 (type2 == TYPE_REAL || type2 == TYPE_TIME))
236 // === GET MEMBERS ========================================================
237 //! Template to get the ValueBase class data by casting the type
238 template <typename T>
239 const T &get(const T& x __attribute__ ((unused)))const
241 assert(is_valid() && same_type_as(x));
242 return *static_cast<const T*>(data);
244 //! Gets the Real part of the data
245 float get(const float &)const { return get(Real()); }
246 //! Gets the Canvas Handle part of the data based on Canvas Handle type
247 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
248 { return get(etl::loose_handle<Canvas>()); }
249 //! Gets the Canvas Handle part of the data based on Canvas pointer type
250 etl::loose_handle<Canvas> get(Canvas*)const
251 { return get(etl::loose_handle<Canvas>()); }
252 //! Gets the data as char pointer based on char pointer
253 const char* get(const char*)const;
254 //! Gets the data as List Type
255 const list_type& get_list()const { return get(list_type()); }
258 String get_string() const;
260 // ========================================================================
264 // === PUT MEMBERS ========================================================
265 //! Put template for any class
266 template <typename T>
269 assert(same_type_as(*x));
270 *x=*static_cast<const T*>(data);
272 //! Put for float values
273 void put(float* x)const { *x=get(Real()); }
274 //! Put for char values (Not defined??)
275 void put(char** x)const;
276 // ========================================================================
280 // === SET MEMBERS ========================================================
281 //! Set template for any class
282 template <typename T> void set(const T& x) { _set(x); }
284 void set(const float &x) { _set(Real(x)); }
285 //! Set for List Type
286 void set(const list_type &x);
287 //! Set for char string
288 void set(const char* x);
289 //! Set for char string
291 //! Set for Canvas pointer
293 //! Set for Canvas handle
294 void set(etl::loose_handle<Canvas> x);
295 //! Set for Canvas handle
296 void set(etl::handle<Canvas> x);
297 //! Set template for standar vector
298 template <class T> void set(const std::vector<T> &x)
299 { _set(list_type(x.begin(),x.end())); }
300 //! Set template for standar list
301 template <class T> void set(const std::list<T> &x)
302 { _set(list_type(x.begin(),x.end())); }
303 // ========================================================================
307 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
312 //! Returns a string containing the name of the given Type
313 static String type_name(Type id);
315 //! Returns a string containing the translated name of the given Type
316 static String type_local_name(Type id);
318 //! Returns a the corresponding Type of the described type.
319 //! Notice that this is used in the loadcanvas. It should keep all
320 //! all type names used in previous sif files
321 static Type ident_type(const String &str);
324 // === GET TYPE MEMBERS ===================================================
325 static Type get_type(bool) { return TYPE_BOOL; }
326 static Type get_type(int) { return TYPE_INTEGER; }
327 static Type get_type(const Angle&) { return TYPE_ANGLE; }
328 static Type get_type(const Time&) { return TYPE_TIME; }
329 static Type get_type(const Real&) { return TYPE_REAL; }
330 static Type get_type(const float&) { return TYPE_REAL; }
331 static Type get_type(const Vector&) { return TYPE_VECTOR; }
332 static Type get_type(const Color&) { return TYPE_COLOR; }
333 static Type get_type(const Segment&) { return TYPE_SEGMENT; }
334 static Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
335 static Type get_type(const String&) { return TYPE_STRING; }
336 static Type get_type(const Gradient&) { return TYPE_GRADIENT; }
337 static Type get_type(Canvas*) { return TYPE_CANVAS; }
338 static Type get_type(const etl::handle<Canvas>&)
339 { return TYPE_CANVAS; }
340 static Type get_type(const etl::loose_handle<Canvas>&)
341 { return TYPE_CANVAS; }
342 static Type get_type(const list_type&) { return TYPE_LIST; }
343 template <class T> static Type get_type(const std::vector<T> &/*x*/)
344 { return TYPE_LIST; }
345 template <class T> static Type get_type(const std::list<T> &/*x*/)
346 { return TYPE_LIST; }
347 // ========================================================================
351 -- ** -- C A S T O P E R A T O R S -----------------------------------------
355 //! I wonder why are those casting operators disabled...
356 operator const list_type&()const { return get_list(); }
357 operator const Angle&()const { return get(Angle()); }
358 //operator const Color&()const { return get(Color()); }
359 operator const Real&()const { return get(Real()); }
360 //operator const Time&()const { return get(Time()); }
362 operator const Vector&()const { return get(Vector()); }
363 operator const BLinePoint&()const { return get(BLinePoint()); }
364 //operator const int&()const { return get(int()); }
365 //operator const String&()const { return get(String()); }
366 //operator const char *()const { return get(String()).c_str(); }
367 operator const Segment&()const { return get(Segment()); }
371 -- ** -- O T H E R -----------------------------------------------------------
377 half get(const half &)const { return get(Real()); }
378 void put(half*x)const { *x=get(Real()); }
379 void set(const half &x) { _set(Real(x)); }
380 static Type get_type(const half&) { return TYPE_REAL; }
381 operator half()const { return get(Real()); }
385 //! Cast operator template to obtain the standard list from the TYPE LIST
387 operator std::list<T>()const
389 assert(type==TYPE_LIST);
390 std::list<T> ret(get_list().begin(),get_list().end());
393 //! Cast operator template to obtain the standard vector from the TYPE LIST
395 operator std::vector<T>()const
397 assert(type==TYPE_LIST);
398 std::vector<T> ret(get_list().begin(),get_list().end());
404 //! Internal set template. Takes in consideration the reference counter
405 template <typename T> void
408 const Type newtype(get_type(x));
410 assert(newtype!=TYPE_NIL);
414 if(ref_count.unique())
416 *reinterpret_cast<T*>(data)=x;
428 }; // END of class ValueBase
432 ** \brief Template for all the valid Value Types
435 class Value : public ValueBase
438 Value(const T &x):ValueBase(x)
442 Value(const ValueBase &x):ValueBase(x)
444 if(!x.same_type_as(T()))
445 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
452 T get()const { return ValueBase::get(T()); }
454 void put(T* x)const { ValueBase::put(x); }
456 void set(const T& x) { ValueBase::operator=(x); }
458 Value<T>& operator=(const T& x) { set(x); return *this; }
460 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
462 Value<T>& operator=(const ValueBase& x)
464 if(!x.same_type_as(T()))
465 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
466 return ValueBase::operator=(x);
469 }; // END of class Value
471 }; // END of namespace synfig
473 /* === E N D =============================================================== */