Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Variant.h
1 /*
2  * Variant class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2005-17, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_VARIANT_H_
22 #define ELM_UTIL_VARIANT_H_
23 
24 #include <elm/types.h>
25 
26 namespace elm {
27 
28 namespace variant {
29 
30  typedef union {
31  bool b;
40  float f;
41  double d;
42  const char *s;
43  void *p;
44  const void *cp;
45  } data_t;
46 
47  template <typename T>
48  struct access_t {
49  typedef const T& rt;
50  static const T& get(const data_t& d) { return *static_cast<const T *>(d.cp); }
51  static void set(data_t& d, const T& x) { d.cp = &x; }
52  };
53 
54  template <typename T>
55  struct access_t<T &> {
56  typedef T& rt;
57  static T& get(const data_t& d) { return *static_cast<T *>(d.p); }
58  static void set(data_t& d, T& x) { d.p = &x; }
59  };
60 
61  template <> struct access_t<bool> { typedef bool rt; static bool get(const data_t& d) { return d.b; } static void set(data_t& d, bool x) { d.b = x; } };
62  template <> struct access_t<t::int8 > { typedef t::int8 rt; static t::int8 get(const data_t& d) { return d.i8 ; } static void set(data_t& d, t::int8 x) { d.i8 = x; } };
63  template <> struct access_t<t::int16> { typedef t::int16 rt; static t::int16 get(const data_t& d) { return d.i16; } static void set(data_t& d, t::int16 x) { d.i16 = x; } };
64  template <> struct access_t<t::int32> { typedef t::int32 rt; static t::int32 get(const data_t& d) { return d.i32; } static void set(data_t& d, t::int32 x) { d.i32 = x; } };
65  template <> struct access_t<t::int64> { typedef t::int64 rt; static t::int64 get(const data_t& d) { return d.i64; } static void set(data_t& d, t::int64 x) { d.i64 = x; } };
66  template <> struct access_t<t::uint8 > { typedef t::uint8 rt; static t::uint8 get(const data_t& d) { return d.u8 ; } static void set(data_t& d, t::uint8 x) { d.u8 = x; } };
67  template <> struct access_t<t::uint16> { typedef t::uint16 rt; static t::uint16 get(const data_t& d) { return d.u16; } static void set(data_t& d, t::uint16 x) { d.u16 = x; } };
68  template <> struct access_t<t::uint32> { typedef t::uint32 rt; static t::uint32 get(const data_t& d) { return d.u32; } static void set(data_t& d, t::uint32 x) { d.u32 = x; } };
69  template <> struct access_t<t::uint64> { typedef t::uint64 rt; static t::uint64 get(const data_t& d) { return d.u64; } static void set(data_t& d, t::uint64 x) { d.u64 = x; } };
70  template <> struct access_t<float> { typedef float rt; static float get(const data_t& d) { return d.f; } static void set(data_t& d, float x) { d.f = x; } };
71  template <> struct access_t<double> { typedef double rt; static double get(const data_t& d) { return d.d; } static void set(data_t& d, double x) { d.d = x; } };
72 
73  template <> struct access_t<cstring> { typedef cstring rt; static cstring get(const data_t& d) { return static_cast<const char *>(d.cp); } static void set(data_t& d, cstring x) { d.cp = x.chars(); } };
74  template <> struct access_t<string> { typedef string rt; static string get(const data_t& d) { return static_cast<const char *>(d.cp); } static void set(data_t& d, string x) { d.cp = x.toCString().chars(); } };
75  template <> struct access_t<const cstring&> { typedef cstring rt; static cstring get(const data_t& d) { return static_cast<const char *>(d.cp); } static void set(data_t& d, cstring x) { d.cp = x.chars(); } };
76  template <> struct access_t<const string&> { typedef string rt; static string get(const data_t& d) { return static_cast<const char *>(d.cp); } static void set(data_t& d, string x) { d.cp = x.toCString().chars(); } };
77 
78  template <class T> struct access_t<T *> {
79  typedef T *rt;
80  static T *get(const data_t& d) { return static_cast<T *>(d.p); }
81  static void set(data_t& d, T *x) { d.p = x; }
82  };
83  template <class T> struct access_t<const T *> {
84  typedef const T *rt;
85  static const T *get(const data_t& d) { return static_cast<const T *>(d.cp); }
86  static void set(data_t& d, const T *x) { d.cp = x; }
87  };
88 
89 } // variant
90 
91 
92 class Variant {
93 public:
94  inline Variant() { }
95  template <class T> Variant(const T& x) { variant::access_t<T>::set(_data, x); }
96  inline void *asPointer(void) const { return _data.p; }
97  template <class T> typename variant::access_t<T>::rt as() const { return variant::access_t<T>::get(_data); }
98 private:
99  variant::data_t _data;
100 };
101 
102 } // elm
103 
104 #endif /* ELM_UTIL_VARIANT_H_ */
elm::variant::access_t< T * >::set
static void set(data_t &d, T *x)
Definition: Variant.h:81
elm::variant::access_t< T * >::rt
T * rt
Definition: Variant.h:79
elm::Variant
Definition: Variant.h:92
elm::variant::data_t::i8
t::int8 i8
Definition: Variant.h:32
elm::variant::access_t< t::int64 >::set
static void set(data_t &d, t::int64 x)
Definition: Variant.h:65
elm::variant::access_t< T & >::set
static void set(data_t &d, T &x)
Definition: Variant.h:58
elm::Variant::Variant
Variant()
Definition: Variant.h:94
elm::variant::access_t< const string & >::set
static void set(data_t &d, string x)
Definition: Variant.h:76
elm::variant::access_t< t::int32 >::set
static void set(data_t &d, t::int32 x)
Definition: Variant.h:64
elm::variant::access_t< t::uint32 >::set
static void set(data_t &d, t::uint32 x)
Definition: Variant.h:68
elm::variant::access_t< const cstring & >::set
static void set(data_t &d, cstring x)
Definition: Variant.h:75
elm::t::int32
int int32
Definition: arch.h:30
elm::t::uint16
unsigned short uint16
Definition: arch.h:29
elm::variant::access_t< const T * >::get
static const T * get(const data_t &d)
Definition: Variant.h:85
elm::variant::access_t::rt
const typedef T & rt
Definition: Variant.h:49
elm::variant::access_t::set
static void set(data_t &d, const T &x)
Definition: Variant.h:51
elm::variant::access_t< t::int16 >::set
static void set(data_t &d, t::int16 x)
Definition: Variant.h:63
elm::t::int64
long int64
Definition: arch.h:32
elm::variant::access_t< t::uint8 >::set
static void set(data_t &d, t::uint8 x)
Definition: Variant.h:66
elm::variant::data_t::d
double d
Definition: Variant.h:41
elm::variant::data_t::i64
t::int64 i64
Definition: Variant.h:35
bool
elm::CString
Definition: CString.h:17
elm::variant::data_t::s
const char * s
Definition: Variant.h:42
elm::variant::access_t< t::int8 >::set
static void set(data_t &d, t::int8 x)
Definition: Variant.h:62
elm::t::int16
short int16
Definition: arch.h:28
elm::variant::data_t::p
void * p
Definition: Variant.h:43
elm::variant::access_t< cstring >::set
static void set(data_t &d, cstring x)
Definition: Variant.h:73
elm::variant::access_t< T & >::rt
T & rt
Definition: Variant.h:56
elm::variant::data_t::i32
t::int32 i32
Definition: Variant.h:34
elm
Definition: adapter.h:26
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::variant::data_t::u16
t::uint16 u16
Definition: Variant.h:37
elm::variant::access_t< const T * >::rt
const typedef T * rt
Definition: Variant.h:84
elm::t::uint64
unsigned long uint64
Definition: arch.h:33
elm::variant::data_t::b
bool b
Definition: Variant.h:31
elm::variant::access_t< T & >::get
static T & get(const data_t &d)
Definition: Variant.h:57
elm::t::uint8
unsigned char uint8
Definition: arch.h:27
elm::variant::data_t::u32
t::uint32 u32
Definition: Variant.h:38
elm::variant::access_t< const T * >::set
static void set(data_t &d, const T *x)
Definition: Variant.h:86
elm::variant::data_t::f
float f
Definition: Variant.h:40
elm::variant::access_t< T * >::get
static T * get(const data_t &d)
Definition: Variant.h:80
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::variant::access_t< string >::set
static void set(data_t &d, string x)
Definition: Variant.h:74
elm::variant::data_t::u64
t::uint64 u64
Definition: Variant.h:39
elm::variant::data_t::u8
t::uint8 u8
Definition: Variant.h:36
elm::t::get
ret< T > get(const var< T > &v)
Definition: type_info.h:288
elm::variant::access_t< t::uint16 >::set
static void set(data_t &d, t::uint16 x)
Definition: Variant.h:67
elm::variant::data_t::i16
t::int16 i16
Definition: Variant.h:33
elm::String
Definition: String.h:30
elm::variant::data_t::cp
const void * cp
Definition: Variant.h:44
elm::variant::access_t< bool >::set
static void set(data_t &d, bool x)
Definition: Variant.h:61
elm::variant::access_t
Definition: Variant.h:48
elm::variant::access_t< float >::set
static void set(data_t &d, float x)
Definition: Variant.h:70
elm::variant::access_t< t::uint64 >::set
static void set(data_t &d, t::uint64 x)
Definition: Variant.h:69
elm::Variant::Variant
Variant(const T &x)
Definition: Variant.h:95
elm::String::toCString
CString toCString(void) const
Definition: String.h:90
elm::Variant::asPointer
void * asPointer(void) const
Definition: Variant.h:96
elm::variant::data_t
Definition: Variant.h:30
elm::variant::access_t< double >::set
static void set(data_t &d, double x)
Definition: Variant.h:71
elm::variant::access_t::get
static const T & get(const data_t &d)
Definition: Variant.h:50
elm::t::int8
signed char int8
Definition: arch.h:26
elm::Variant::as
variant::access_t< T >::rt as() const
Definition: Variant.h:97