Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / 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 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_VALUE_H
26 #define __SYNFIG_VALUE_H
27
28 /* === H E A D E R S ======================================================= */
29
30 //#include "vector.h"
31 //#include "time.h"
32 #include "segment.h"
33 //#include "color.h"
34 #include "string.h"
35 #include <list>
36 #include <vector>
37 #include <ETL/trivial>
38 #include <ETL/handle>
39 #include "general.h"
40 //#include "gradient.h"
41 #include "blinepoint.h"
42 #include "exception.h"
43
44 #ifdef USE_HALF_TYPE
45 #include <OpenEXR/half.h>
46 #endif
47
48 #ifndef SYNFIG_NO_ANGLE
49 #include "angle.h"
50 #endif
51
52 #include <ETL/ref_count>
53
54 /* === M A C R O S ========================================================= */
55
56 /* === T Y P E D E F S ===================================================== */
57
58 /* === C L A S S E S & S T R U C T S ======================================= */
59
60 namespace synfig {
61
62 class Canvas;
63 class Vector;
64 class Time;
65 class Segment;
66 class Gradient;
67 class BLinePoint;
68 class Color;
69
70 /*!     \class ValueBase
71 **      \todo writeme
72 */
73 class ValueBase
74 {
75         /*
76  --     ** -- T Y P E S -----------------------------------------------------------
77         */
78
79 public:
80
81         //! \writeme
82         enum Type
83         {
84                 TYPE_NIL=0,                     //!< Represents an empty value
85
86                 TYPE_BOOL,
87                 TYPE_INTEGER,
88                 TYPE_ANGLE,                     //!< Angle
89
90                 // All types after this point are larger than 32 bits
91
92                 TYPE_TIME,                      //!< Time
93                 TYPE_REAL,                      //!< Real
94
95                 // All types after this point are larger than 64 bits
96
97                 TYPE_VECTOR,            //!< Vector
98                 TYPE_COLOR,                     //!< Color
99                 TYPE_SEGMENT,           //!< Segment
100                 TYPE_BLINEPOINT,        //!< BLinePoint
101
102                 // All types after this point require construction/destruction
103
104                 TYPE_LIST,                      //!< List
105                 TYPE_CANVAS,            //!< Canvas
106                 TYPE_STRING,            //!< String
107                 TYPE_GRADIENT,          //!< Color Gradient
108
109                 TYPE_END                        //!< Not a valid type, used for sanity checks
110         };
111
112 private:
113
114         typedef std::vector<ValueBase> list_type;
115
116         /*
117  --     ** -- D A T A -------------------------------------------------------------
118         */
119
120 protected:
121
122         Type type;
123         void *data;
124         etl::reference_counter ref_count;
125         bool loop_;
126
127         /*
128  --     ** -- C O N S T R U C T O R S -----------------------------------
129         */
130
131 public:
132
133         //! \writeme
134         ValueBase();
135
136         //! \writeme
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         //! \writeme
143         ValueBase(Type x);
144
145         //! \writeme
146         ~ValueBase();
147
148         /*
149  --     ** -- O P E R A T O R S ---------------------------------------------------
150         */
151
152 public:
153
154         //! \writeme
155         template <class T> ValueBase& operator=(const T& x)
156                 { set(x); return *this; }
157
158         //! \writeme
159         ValueBase& operator=(const ValueBase& x);
160
161         //! \writeme
162         bool operator==(const ValueBase& rhs)const;
163
164         //! \writeme
165         bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
166
167         //!     Constant index operator for when value is of type TYPE_LIST
168         const ValueBase &operator[](int index)const
169                 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
170
171         /*
172  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
173         */
174
175 public:
176
177         //! \writeme
178         void clear();
179
180         //! \writeme
181         bool get_loop()const { return loop_; }
182
183         //! \writeme
184         void set_loop(bool x) { loop_=x; }
185
186         //! \writeme
187         bool empty()const;
188
189         //! \writeme
190         Type get_contained_type()const;
191
192         //! Returns true if the contained value is defined and valid.
193         bool is_valid()const;
194
195         //!     Returns a string containing the name of the type
196         String type_name()const { return type_name(type); }
197
198         //! Returns the type of the contained value
199         const Type & get_type()const { return type; }
200
201         //! Checks the type of the parameter against itself. Returns true if they are of the same type.
202         template <class T> bool
203         same_as(const T &x)const
204         {
205                 const Type testtype(get_type(x));
206
207                 if(testtype==type)return true;
208                 if(     (type==TYPE_REAL || type==TYPE_TIME) &&
209                         (testtype==TYPE_REAL || testtype==TYPE_TIME) )
210                         return true;
211                 return false;
212         }
213
214
215         // === GET MEMBERS ========================================================
216         template <typename T>
217         const T &get(const T& x)const
218         {
219                 assert(is_valid() && same_as(x));
220                 return *static_cast<const T*>(data);
221         }
222         float get(const float &)const { return get(Real()); }
223         etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
224                 { return get(etl::loose_handle<Canvas>()); }
225         etl::loose_handle<Canvas> get(Canvas*)const
226                 { return get(etl::loose_handle<Canvas>()); }
227         const char* get(const char*)const;
228         const list_type& get_list()const { return get(list_type()); }
229         // ========================================================================
230
231
232
233         // === PUT MEMBERS ========================================================
234         template <typename T>
235         void put(T* x)const
236         {
237                 assert(same_as(*x));
238                 *x=*static_cast<const T*>(data);
239         }
240         void put(float* x)const { *x=get(Real()); }
241         void put(char** x)const;
242         // ========================================================================
243
244
245
246         // === SET MEMBERS ========================================================
247         template <typename T> void set(const T& x) { _set(x); }
248         void set(const float &x) { _set(Real(x)); }
249         void set(const list_type &x);
250         void set(const char* x);
251         void set(Canvas*x);
252         void set(etl::loose_handle<Canvas> x);
253         void set(etl::handle<Canvas> x);
254         template <class T> void set(const std::vector<T> &x)
255                 { _set(list_type(x.begin(),x.end())); }
256         template <class T> void set(const std::list<T> &x)
257                 { _set(list_type(x.begin(),x.end())); }
258         // ========================================================================
259
260
261         /*
262  --     ** -- S T A T I C   F U N C T I O N S -------------------------------------
263         */
264
265 public:
266
267         //!     Returns a string containing the name of the given Type
268         static String type_name(Type id);
269
270         //!     Returns a the corresponding Type of the described type
271         static Type ident_type(const String &str);
272
273
274         // === GET TYPE MEMBERS ===================================================
275         static const Type get_type(bool) { return TYPE_BOOL; }
276         static const Type get_type(int) { return TYPE_INTEGER; }
277         static const Type get_type(const Time&) { return TYPE_TIME; }
278         static const Type get_type(const Real&) { return TYPE_REAL; }
279         static const Type get_type(const float&) { return TYPE_REAL; }
280         static const Type get_type(const Vector&) { return TYPE_VECTOR; }
281         static const Type get_type(const Color&) { return TYPE_COLOR; }
282         static const Type get_type(const Segment&) { return TYPE_SEGMENT; }
283         static const Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
284         static const Type get_type(const String&) { return TYPE_STRING; }
285         static const Type get_type(const Gradient&) { return TYPE_GRADIENT; }
286         static const Type get_type(Canvas*) { return TYPE_CANVAS; }
287         static const Type get_type(const etl::handle<Canvas>&)
288                 { return TYPE_CANVAS; }
289         static const Type get_type(const etl::loose_handle<Canvas>&)
290                 { return TYPE_CANVAS; }
291         static const Type get_type(const list_type&) { return TYPE_LIST; }
292         template <class T> static const Type get_type(const std::vector<T> &x)
293                 { return TYPE_LIST; }
294         template <class T> static const Type get_type(const std::list<T> &x)
295                 { return TYPE_LIST; }
296         // ========================================================================
297
298
299         /*
300  --     ** -- C A S T   O P E R A T O R S -----------------------------------------
301         */
302
303 public:
304
305         operator const list_type&()const { return get_list(); }
306         //operator const Color&()const { return get(Color()); }
307         //operator const Real&()const { return get(Real()); }
308         //operator const Time&()const { return get(Time()); }
309
310         operator const Vector&()const {  return get(Vector()); }
311         operator const BLinePoint&()const {  return get(BLinePoint()); }
312         //operator const int&()const {  return get(int()); }
313         //operator const String&()const {  return get(String()); }
314         //operator const char *()const {  return get(String()).c_str(); }
315         operator const Segment&()const { return get(Segment()); }
316
317
318         /*
319  --     ** -- O T H E R -----------------------------------------------------------
320         */
321
322 public:
323
324 #ifdef USE_HALF_TYPE
325         half get(const half &)const { return get(Real()); }
326         void put(half*x)const { *x=get(Real()); }
327         void set(const half &x) { _set(Real(x)); }
328         static const Type get_type(const half&) { return TYPE_REAL; }
329         operator half()const { return get(Real()); }
330 #endif
331
332 #ifndef SYNFIG_NO_ANGLE
333         operator const Angle&()const { return get(Angle()); }
334         static const Type get_type(const Angle&) { return TYPE_ANGLE; }
335 #endif
336
337         template <class T>
338         operator std::list<T>()const
339         {
340                 assert(type==TYPE_LIST);
341                 std::list<T> ret(get_list().begin(),get_list().end());
342                 return ret;
343         }
344         template <class T>
345         operator std::vector<T>()const
346         {
347                 assert(type==TYPE_LIST);
348                 std::vector<T> ret(get_list().begin(),get_list().end());
349                 return ret;
350         }
351
352
353 private:
354
355         template <typename T> void
356         _set(const T& x)
357         {
358                 const Type newtype(get_type(x));
359
360                 assert(newtype!=TYPE_NIL);
361
362                 if(newtype==type)
363                 {
364                         if(ref_count.unique())
365                         {
366                                 *reinterpret_cast<T*>(data)=x;
367                                 return;
368                         }
369                 }
370
371                 clear();
372
373                 type=newtype;
374                 ref_count.reset();
375                 data=new T(x);
376         }
377 }; // END of class ValueBase
378
379
380 /*!     \class Value
381 **      \todo writeme
382 */
383 template <class T>
384 class Value : public ValueBase
385 {
386 public:
387         Value(const T &x):ValueBase(x)
388         {
389         }
390
391         Value(const ValueBase &x):ValueBase(x)
392         {
393                 if(!x.same_as(T()))
394                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
395         }
396
397         Value()
398         {
399         }
400
401         T get()const { return ValueBase::get(T()); }
402
403         void put(T* x)const     { ValueBase::put(x); }
404
405         void set(const T& x) { ValueBase::operator=(x); }
406
407         Value<T>& operator=(const T& x) { set(x); return *this; }
408
409         Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
410
411         Value<T>& operator=(const ValueBase& x)
412         {
413                 if(!x.same_as(T()))
414                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
415                 return ValueBase::operator=(x);
416         }
417
418 }; // END of class Value
419
420 /*
421 template <>
422 class Value< std::list<CT> > : public ValueBase
423 {
424 public:
425         Value(const T &x):ValueBase(x)
426         {
427         }
428         Value(const ValueBase &x):ValueBase(x)
429         {
430                 if(!x.same_as(T()))
431                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
432         }
433         Value()
434         {
435         }
436
437         T get()const { return ValueBase::get(T()); }
438
439         void put(T* x)const     { ValueBase::put(x); }
440
441         void set(const T& x) { ValueBase::operator=(x); }
442
443         Value<T>& operator=(const T& x) { set(x); return *this; }
444
445         Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
446
447         Value<T>& operator=(const ValueBase& x)
448         {
449                 if(!x.same_as(T()))
450                         throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
451                 return ValueBase::operator=(x);
452         }
453
454 }; // END of class Value
455 */
456
457 }; // END of namespace synfig
458
459 /* === E N D =============================================================== */
460
461 #endif