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