Rearrange conditions for export action and fix bug 2956710
[synfig.git] / synfig-core / src / synfig / value.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file value.h
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_VALUE_H
27 #define __SYNFIG_VALUE_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include "angle.h"
32 #include "segment.h"
33 #include "string.h"
34 #include <list>
35 #include <vector>
36 #include <ETL/trivial>
37 #include <ETL/handle>
38 #include "general.h"
39 #include "blinepoint.h"
40 #include "exception.h"
41
42 #ifdef USE_HALF_TYPE
43 #include <OpenEXR/half.h>
44 #endif
45
46
47 #include <ETL/ref_count>
48
49 /* === M A C R O S ========================================================= */
50
51 /* === T Y P E D E F S ===================================================== */
52
53 /* === C L A S S E S & S T R U C T S ======================================= */
54
55 namespace synfig {
56
57 class Canvas;
58 class Vector;
59 class Time;
60 class Segment;
61 class Gradient;
62 class BLinePoint;
63 class Color;
64
65 /*!     \class ValueBase
66 **      \brief Base class for the Values of Synfig
67 */
68 class ValueBase
69 {
70         /*
71  --     ** -- T Y P E S -----------------------------------------------------------
72         */
73
74 public:
75
76         //! This enum lists all the types of values
77         enum Type
78         {
79                 TYPE_NIL=0,                     //!< Represents an empty value
80
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)
84
85                 // All types after this point are larger than 32 bits
86
87                 TYPE_TIME,                      //!< Time value
88                 TYPE_REAL,                      //!< Real value (floating point number)
89
90                 // All types after this point are larger than 64 bits
91
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)
96
97                 // All types after this point require construction/destruction
98
99                 TYPE_LIST,                      //!< List of any of above
100                 TYPE_CANVAS,            //!< Canvas
101                 TYPE_STRING,            //!< String
102                 TYPE_GRADIENT,          //!< Color Gradient
103
104                 TYPE_END                        //!< Not a valid type, used for sanity checks
105         };
106
107 private:
108
109         typedef std::vector<ValueBase> list_type;
110
111         /*
112  --     ** -- D A T A -------------------------------------------------------------
113         */
114
115 protected:
116         //! The type of value
117         Type type;
118         //! Pointer to hold the data of the value
119         void *data;
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
125         bool loop_;
126
127         /*
128  --     ** -- C O N S T R U C T O R S -----------------------------------
129         */
130
131 public:
132
133         //! Default constructor
134         ValueBase();
135
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_)
140                 { set(x); }
141
142         //! Copy constructor. The data is not copied, just the type.
143         ValueBase(Type x);
144
145         //! Default destructor
146         ~ValueBase();
147
148         /*
149  --     ** -- O P E R A T O R S ---------------------------------------------------
150         */
151
152 public:
153
154         //! Template for the operator assignation operator for non ValueBase classes
155         //! \see set()
156         template <class T> ValueBase& operator=(const T& x)
157                 { set(x); return *this; }
158
159         //!Operator asignation for ValueBase classes. Does a exact copy of \x
160         ValueBase& operator=(const ValueBase& x);
161
162         //! Eqaul than operator. Segment, Gradient and Bline Points cannot be compared.
163         bool operator==(const ValueBase& rhs)const;
164
165         //! Not equal than operator.
166         bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
167
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]; }
171
172         /*
173  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
174         */
175
176 public:
177
178         //! Deletes the data only if the ref count is zero
179         void clear();
180
181         //! Gets the loop option.
182         bool get_loop()const { return loop_; }
183
184         //! Sets the loop option.
185         void set_loop(bool x) { loop_=x; }
186
187         //! True if the Value is not valid or is type LIST and is empty
188         bool empty()const;
189
190         //! Gets the contained type in the Value Base
191         Type get_contained_type()const;
192
193         //! Returns true if the contained value is defined and valid.
194         bool is_valid()const;
195
196         //!     Returns a string containing the name of the type. Used for sif files
197         String type_name()const { return type_name(type); }
198
199         //! Returns the type of the contained value
200         const Type & get_type()const { return type; }
201
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
206         {
207                 const Type testtype(get_type(x));
208
209                 return same_type_as(type, testtype);
210         }
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
213         {
214                 return same_type_as(type, testtype);
215         }
216
217         //! Compares two types.  Returns true if they are the same type.
218         static bool same_type_as(const Type type1, const Type type2)
219         {
220                 if (type1 == type2) return true;
221                 if ((type1 == TYPE_REAL || type1 == TYPE_TIME) &&
222                         (type2 == TYPE_REAL || type2 == TYPE_TIME))
223                         return true;
224                 return false;
225         }
226
227
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
232         {
233                 assert(is_valid() && same_type_as(x));
234                 return *static_cast<const T*>(data);
235         }
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()); }
248
249 #ifdef _DEBUG
250         String get_string() const;
251 #endif  // _DEBUG
252         // ========================================================================
253
254
255
256         // === PUT MEMBERS ========================================================
257         //! Put template for any class
258         template <typename T>
259         void put(T* x)const
260         {
261                 assert(same_type_as(*x));
262                 *x=*static_cast<const T*>(data);
263         }
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         // ========================================================================
269
270
271
272         // === SET MEMBERS ========================================================
273         //! Set template for any class
274         template <typename T> void set(const T& x) { _set(x); }
275         //! Set for float
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
282         void set(char* x);
283         //! Set for Canvas pointer
284         void set(Canvas*x);
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         // ========================================================================
296
297
298         /*
299  --     ** -- S T A T I C   F U N C T I O N S -------------------------------------
300         */
301
302 public:
303
304         //!     Returns a string containing the name of the given Type
305         static String type_name(Type id);
306
307         //!     Returns a string containing the translated name of the given Type
308         static String type_local_name(Type id);
309
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);
314
315
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         // ========================================================================
340
341
342         /*
343  --     ** -- C A S T   O P E R A T O R S -----------------------------------------
344         */
345
346 public:
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()); }
353
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()); }
360
361
362         /*
363  --     ** -- O T H E R -----------------------------------------------------------
364         */
365
366 public:
367
368 #ifdef USE_HALF_TYPE
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()); }
374 #endif
375
376
377         //! Cast operator template to obtain the standard list from the TYPE LIST
378         template <class T>
379         operator std::list<T>()const
380         {
381                 assert(type==TYPE_LIST);
382                 std::list<T> ret(get_list().begin(),get_list().end());
383                 return ret;
384         }
385         //! Cast operator template to obtain the standard vector from the TYPE LIST
386         template <class T>
387         operator std::vector<T>()const
388         {
389                 assert(type==TYPE_LIST);
390                 std::vector<T> ret(get_list().begin(),get_list().end());
391                 return ret;
392         }
393
394
395 private:
396         //! Internal set template. Takes in consideration the reference counter
397         template <typename T> void
398         _set(const T& x)
399         {
400                 const Type newtype(get_type(x));
401
402                 assert(newtype!=TYPE_NIL);
403
404                 if(newtype==type)
405                 {
406                         if(ref_count.unique())
407                         {
408                                 *reinterpret_cast<T*>(data)=x;
409                                 return;
410                         }
411                 }
412
413                 clear();
414
415                 type=newtype;
416                 ref_count.reset();
417                 data=new T(x);
418         }
419
420 }; // END of class ValueBase
421
422
423 /*!     \class Value
424 **      \brief Template for all the valid Value Types
425 */
426 template <class T>
427 class Value : public ValueBase
428 {
429 public:
430         Value(const T &x):ValueBase(x)
431         {
432         }
433
434         Value(const ValueBase &x):ValueBase(x)
435         {
436                 if(!x.same_type_as(T()))
437                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
438         }
439
440         Value()
441         {
442         }
443
444         T get()const { return ValueBase::get(T()); }
445
446         void put(T* x)const     { ValueBase::put(x); }
447
448         void set(const T& x) { ValueBase::operator=(x); }
449
450         Value<T>& operator=(const T& x) { set(x); return *this; }
451
452         Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
453
454         Value<T>& operator=(const ValueBase& x)
455         {
456                 if(!x.same_type_as(T()))
457                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
458                 return ValueBase::operator=(x);
459         }
460
461 }; // END of class Value
462
463 }; // END of namespace synfig
464
465 /* === E N D =============================================================== */
466
467 #endif