1 /* === S I N F G =========================================================== */
3 ** \brief Template Header
5 ** $Id: value.h,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __SINFG_VALUE_H
25 #define __SINFG_VALUE_H
27 /* === H E A D E R S ======================================================= */
36 #include <ETL/trivial>
39 //#include "gradient.h"
40 #include "blinepoint.h"
41 #include "exception.h"
43 #ifndef SINFG_NO_ANGLE
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 ======================================= */
71 -- ** -- T Y P E S -----------------------------------------------------------
79 TYPE_NIL=0, //!< Represents an empty value
83 TYPE_ANGLE, //!< Angle
85 // All types after this point are larger than 32 bits
90 // All types after this point are larger than 64 bits
92 TYPE_VECTOR, //!< Vector
93 TYPE_COLOR, //!< Color
94 TYPE_SEGMENT, //!< Segment
95 TYPE_BLINEPOINT, //!< BLinePoint
97 // All types after this point require construction/destruction
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 -------------------------------------------------------------
119 etl::reference_counter ref_count;
123 -- ** -- C O N S T R U C T O R S -----------------------------------
132 template <typename T>
133 ValueBase(const T &x, bool loop_=false):
134 type(TYPE_NIL),data(0),ref_count(0),loop_(loop_)
144 -- ** -- O P E R A T O R S ---------------------------------------------------
150 template <class T> ValueBase& operator=(const T& x)
151 { set(x); return *this; }
154 ValueBase& operator=(const ValueBase& x);
157 bool operator==(const ValueBase& rhs)const;
160 bool operator!=(const ValueBase& rhs)const { return !operator==(rhs); }
162 //! Constant index operator for when value is of type TYPE_LIST
163 const ValueBase &operator[](int index)const
164 { assert(type==TYPE_LIST); assert(index>0); return get_list()[index]; }
167 -- ** -- M E M B E R F U N C T I O N S -------------------------------------
176 bool get_loop()const { return loop_; }
179 void set_loop(bool x) { loop_=x; }
185 Type get_contained_type()const;
187 //! Returns true if the contained value is defined and valid.
188 bool is_valid()const;
190 //! Returns a string containing the name of the type
191 String type_name()const { return type_name(type); }
193 //! Returns the type of the contained value
194 const Type & get_type()const { return type; }
196 //! Checks the type of the parameter against itself. Returns true if they are of the same type.
197 template <class T> bool
198 same_as(const T &x)const
200 const Type testtype(get_type(x));
202 if(testtype==type)return true;
203 if( (type==TYPE_REAL || type==TYPE_TIME) &&
204 (testtype==TYPE_REAL || testtype==TYPE_TIME) )
210 // === GET MEMBERS ========================================================
211 template <typename T>
212 const T &get(const T& x)const
214 assert(is_valid() && same_as(x));
215 return *static_cast<const T*>(data);
217 float get(const float &)const { return get(Real()); }
218 etl::loose_handle<Canvas> get(const etl::handle<Canvas>&)const
219 { return get(etl::loose_handle<Canvas>()); }
220 etl::loose_handle<Canvas> get(Canvas*)const
221 { return get(etl::loose_handle<Canvas>()); }
222 const char* get(const char*)const;
223 const list_type& get_list()const { return get(list_type()); }
224 // ========================================================================
228 // === PUT MEMBERS ========================================================
229 template <typename T>
233 *x=*static_cast<const T*>(data);
235 void put(float* x)const { *x=get(Real()); }
236 void put(char** x)const;
237 // ========================================================================
241 // === SET MEMBERS ========================================================
242 template <typename T> void set(const T& x) { _set(x); }
243 void set(const float &x) { _set(Real(x)); }
244 void set(const list_type &x);
245 void set(const char* x);
247 void set(etl::loose_handle<Canvas> x);
248 void set(etl::handle<Canvas> x);
249 template <class T> void set(const std::vector<T> &x)
250 { _set(list_type(x.begin(),x.end())); }
251 template <class T> void set(const std::list<T> &x)
252 { _set(list_type(x.begin(),x.end())); }
253 // ========================================================================
257 -- ** -- S T A T I C F U N C T I O N S -------------------------------------
262 //! Returns a string containing the name of the given Type
263 static String type_name(Type id);
265 //! Returns a the corresponding Type of the described type
266 static Type ident_type(const String &str);
269 // === GET TYPE MEMBERS ===================================================
270 static const Type get_type(bool) { return TYPE_BOOL; }
271 static const Type get_type(int) { return TYPE_INTEGER; }
272 static const Type get_type(const Time&) { return TYPE_TIME; }
273 static const Type get_type(const Real&) { return TYPE_REAL; }
274 static const Type get_type(const float&) { return TYPE_REAL; }
275 static const Type get_type(const Vector&) { return TYPE_VECTOR; }
276 static const Type get_type(const Color&) { return TYPE_COLOR; }
277 static const Type get_type(const Segment&) { return TYPE_SEGMENT; }
278 static const Type get_type(const BLinePoint&) { return TYPE_BLINEPOINT; }
279 static const Type get_type(const String&) { return TYPE_STRING; }
280 static const Type get_type(const Gradient&) { return TYPE_GRADIENT; }
281 static const Type get_type(Canvas*) { return TYPE_CANVAS; }
282 static const Type get_type(const etl::handle<Canvas>&)
283 { return TYPE_CANVAS; }
284 static const Type get_type(const etl::loose_handle<Canvas>&)
285 { return TYPE_CANVAS; }
286 static const Type get_type(const list_type&) { return TYPE_LIST; }
287 template <class T> static const Type get_type(const std::vector<T> &x)
288 { return TYPE_LIST; }
289 template <class T> static const Type get_type(const std::list<T> &x)
290 { return TYPE_LIST; }
291 // ========================================================================
295 -- ** -- C A S T O P E R A T O R S -----------------------------------------
301 operator std::list<T>()const
303 assert(type==TYPE_LIST);
304 std::list<T> ret(get_list().begin(),get_list().end());
308 operator std::vector<T>()const
310 assert(type==TYPE_LIST);
311 std::vector<T> ret(get_list().begin(),get_list().end());
314 operator const list_type&()const { return get_list(); }
315 //operator const Color&()const { return get(Color()); }
316 //operator const Real&()const { return get(Real()); }
317 //operator const Time&()const { return get(Time()); }
319 operator const Vector&()const { return get(Vector()); }
320 operator const BLinePoint&()const { return get(BLinePoint()); }
321 //operator const int&()const { return get(int()); }
322 //operator const String&()const { return get(String()); }
323 //operator const char *()const { return get(String()).c_str(); }
324 operator const Segment&()const { return get(Segment()); }
327 -- ** -- O T H E R -----------------------------------------------------------
333 half get(const half &)const { return get(Real()); }
334 void put(half*x)const { *x=get(Real()); }
335 void set(const half &x) { _set(Real(x)); }
336 static const Type get_type(const half&) { return TYPE_REAL; }
337 operator half()const { return get(Real()); }
340 #ifndef SINFG_NO_ANGLE
341 operator const Angle&()const { return get(Angle()); }
342 static const Type get_type(const Angle&) { return TYPE_ANGLE; }
348 template <typename T> void
351 const Type newtype(get_type(x));
353 assert(newtype!=TYPE_NIL);
357 if(ref_count.unique())
359 *reinterpret_cast<T*>(data)=x;
370 }; // END of class ValueBase
377 class Value : public ValueBase
380 Value(const T &x):ValueBase(x)
384 Value(const ValueBase &x):ValueBase(x)
387 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
394 T get()const { return ValueBase::get(T()); }
396 void put(T* x)const { ValueBase::put(x); }
398 void set(const T& x) { ValueBase::operator=(x); }
400 Value<T>& operator=(const T& x) { set(x); return *this; }
402 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
404 Value<T>& operator=(const ValueBase& x)
407 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
408 return ValueBase::operator=(x);
411 }; // END of class Value
415 class Value< std::list<CT> > : public ValueBase
418 Value(const T &x):ValueBase(x)
421 Value(const ValueBase &x):ValueBase(x)
424 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
430 T get()const { return ValueBase::get(T()); }
432 void put(T* x)const { ValueBase::put(x); }
434 void set(const T& x) { ValueBase::operator=(x); }
436 Value<T>& operator=(const T& x) { set(x); return *this; }
438 Value<T>& operator=(const Value<T>& x) { return ValueBase::operator=(x); }
440 Value<T>& operator=(const ValueBase& x)
443 throw Exception::BadType("Value<T>(ValueBase): Type Mismatch");
444 return ValueBase::operator=(x);
447 }; // END of class Value
450 }; // END of namespace sinfg
452 /* === E N D =============================================================== */