cgv
variant.h
1 #pragma once
2 
3 #include <cgv/utils/convert.h>
4 #include <stdlib.h>
5 #include <cgv/type/standard_types.h>
6 #include <cgv/type/info/type_id.h>
7 
8 #include "lib_begin.h"
9 
10 namespace cgv {
11  namespace type {
12 
19 template <typename T>
20 struct variant
21 {
23  static T get(const std::string& value_type, const void* value_ptr)
24  {
25  if (value_type == info::get_type_name(info::TI_BOOL))
26  return (T) *static_cast<const bool*>(value_ptr);
27  if (value_type == info::get_type_name(info::TI_INT8))
28  return (T) *static_cast<const int8_type*>(value_ptr);
29  if (value_type == info::get_type_name(info::TI_INT16))
30  return (T) *static_cast<const int16_type*>(value_ptr);
31  if (value_type == info::get_type_name(info::TI_INT32))
32  return (T) *static_cast<const int32_type*>(value_ptr);
33  if (value_type == info::get_type_name(info::TI_INT64))
34  return (T) *static_cast<const int64_type*>(value_ptr);
35  if (value_type == info::get_type_name(info::TI_UINT8))
36  return (T) *static_cast<const uint8_type*>(value_ptr);
37  if (value_type == info::get_type_name(info::TI_UINT16))
38  return (T) *static_cast<const uint16_type*>(value_ptr);
39  if (value_type == info::get_type_name(info::TI_UINT32))
40  return (T) *static_cast<const uint32_type*>(value_ptr);
41  if (value_type == info::get_type_name(info::TI_UINT64))
42  return (T) *static_cast<const uint64_type*>(value_ptr);
43  if (value_type == info::get_type_name(info::TI_FLT32))
44  return (T) *static_cast<const flt32_type*>(value_ptr);
45  if (value_type == info::get_type_name(info::TI_FLT64))
46  return (T) *static_cast<const flt64_type*>(value_ptr);
47  if (value_type == info::get_type_name(info::TI_STRING))
48  return (T) atof(static_cast<const std::string*>(value_ptr)->c_str());
49  if (value_type == info::get_type_name(info::TI_WCHAR))
50  return (T) *static_cast<const short*>(value_ptr);
51  if (value_type == info::get_type_name(info::TI_WSTRING))
52  return (T) atof(cgv::utils::wstr2str(*static_cast<const std::wstring*>(value_ptr)).c_str());
53  return T();
54  }
56  static void set(const T& value, const std::string& value_type, void* value_ptr)
57  {
58  if (value_type == info::get_type_name(info::TI_BOOL))
59  *static_cast<bool*>(value_ptr) = value != T();
60  else if (value_type == info::get_type_name(info::TI_INT8))
61  *static_cast<int8_type*>(value_ptr) = (int8_type) value;
62  else if (value_type == info::get_type_name(info::TI_INT16))
63  *static_cast<int16_type*>(value_ptr) = (int16_type) value;
64  else if (value_type == info::get_type_name(info::TI_INT32))
65  *static_cast<int32_type*>(value_ptr) = (int32_type) value;
66  else if (value_type == info::get_type_name(info::TI_INT64))
67  *static_cast<int64_type*>(value_ptr) = (int64_type) value;
68  else if (value_type == info::get_type_name(info::TI_UINT8))
69  *static_cast<uint8_type*>(value_ptr) = (uint8_type) value;
70  else if (value_type == info::get_type_name(info::TI_UINT16))
71  *static_cast<uint16_type*>(value_ptr) = (uint16_type) value;
72  else if (value_type == info::get_type_name(info::TI_UINT32))
73  *static_cast<uint32_type*>(value_ptr) = (uint32_type) value;
74  else if (value_type == info::get_type_name(info::TI_UINT64))
75  *static_cast<uint64_type*>(value_ptr) = (uint64_type) value;
76  else if (value_type == info::get_type_name(info::TI_FLT32))
77  *static_cast<flt32_type*>(value_ptr) = (flt32_type) value;
78  else if (value_type == info::get_type_name(info::TI_FLT64))
79  *static_cast<flt64_type*>(value_ptr) = (flt64_type) value;
80  else if (value_type == info::get_type_name(info::TI_WCHAR))
81  *static_cast<int16_type*>(value_ptr) = (int16_type) value;
82  else if (value_type == info::get_type_name(info::TI_STRING))
83  *static_cast<std::string*>(value_ptr) = cgv::utils::to_string(value);
84  else if (value_type == info::get_type_name(info::TI_WSTRING))
85  *static_cast<std::wstring*>(value_ptr) = cgv::utils::str2wstr(cgv::utils::to_string(value));
86  }
87 };
88 
89 template <>
90 struct variant<bool>
91 {
92  static bool get(const std::string& value_type, const void* value_ptr)
93  {
94  if (value_type == info::get_type_name(info::TI_BOOL))
95  return *static_cast<const bool*>(value_ptr);
96  if (value_type == info::get_type_name(info::TI_INT8))
97  return *static_cast<const int8_type*>(value_ptr) != 0;
98  if (value_type == info::get_type_name(info::TI_INT16))
99  return *static_cast<const int16_type*>(value_ptr) != 0;
100  if (value_type == info::get_type_name(info::TI_INT32))
101  return *static_cast<const int32_type*>(value_ptr) != 0;
102  if (value_type == info::get_type_name(info::TI_INT64))
103  return *static_cast<const int64_type*>(value_ptr) != 0;
104  if (value_type == info::get_type_name(info::TI_UINT8))
105  return *static_cast<const uint8_type*>(value_ptr) != 0;
106  if (value_type == info::get_type_name(info::TI_UINT16))
107  return *static_cast<const uint16_type*>(value_ptr) != 0;
108  if (value_type == info::get_type_name(info::TI_UINT32))
109  return *static_cast<const uint32_type*>(value_ptr) != 0;
110  if (value_type == info::get_type_name(info::TI_UINT64))
111  return *static_cast<const uint64_type*>(value_ptr) != 0;
112  if (value_type == info::get_type_name(info::TI_FLT32))
113  return *static_cast<const flt32_type*>(value_ptr) != 0;
114  if (value_type == info::get_type_name(info::TI_FLT64))
115  return *static_cast<const flt64_type*>(value_ptr) != 0;
116  if (value_type == info::get_type_name(info::TI_WCHAR))
117  return *static_cast<const wchar_type*>(value_ptr) != 0;
118  if (value_type == info::get_type_name(info::TI_STRING))
119  return *static_cast<const std::string*>(value_ptr) == "true";
120  if (value_type == info::get_type_name(info::TI_WSTRING))
121  return *static_cast<const std::wstring*>(value_ptr) == L"true";
122  return false;
123  }
124  static void set(const bool& value, const std::string& value_type, void* value_ptr)
125  {
126  if (value_type == info::get_type_name(info::TI_BOOL))
127  *static_cast<bool*>(value_ptr) = value;
128  else if (value_type == info::get_type_name(info::TI_INT8))
129  *static_cast<int8_type*>(value_ptr) = value?1:0;
130  else if (value_type == info::get_type_name(info::TI_INT16))
131  *static_cast<int16_type*>(value_ptr) = value?1:0;
132  else if (value_type == info::get_type_name(info::TI_INT32))
133  *static_cast<int32_type*>(value_ptr) = value?1:0;
134  else if (value_type == info::get_type_name(info::TI_INT64))
135  *static_cast<int64_type*>(value_ptr) = value?1:0;
136  else if (value_type == info::get_type_name(info::TI_UINT8))
137  *static_cast<uint8_type*>(value_ptr) = value?1:0;
138  else if (value_type == info::get_type_name(info::TI_UINT16))
139  *static_cast<uint16_type*>(value_ptr) = value?1:0;
140  else if (value_type == info::get_type_name(info::TI_UINT32))
141  *static_cast<uint32_type*>(value_ptr) = value?1:0;
142  else if (value_type == info::get_type_name(info::TI_UINT64))
143  *static_cast<uint64_type*>(value_ptr) = value?1:0;
144  else if (value_type == info::get_type_name(info::TI_FLT32))
145  *static_cast<flt32_type*>(value_ptr) = value?1.0f:0.0f;
146  else if (value_type == info::get_type_name(info::TI_FLT64))
147  *static_cast<flt64_type*>(value_ptr) = value?1:0;
148  else if (value_type == info::get_type_name(info::TI_WCHAR))
149  *static_cast<wchar_type*>(value_ptr) = value?1:0;
150  else if (value_type == info::get_type_name(info::TI_STRING))
151  *static_cast<std::string*>(value_ptr) = value?"true":"false";
152  else if (value_type == info::get_type_name(info::TI_WSTRING))
153  *static_cast<std::wstring*>(value_ptr) = value?L"true":L"false";
154  }
155 };
156 
157 
158 template <>
159 struct variant<std::string>
160 {
161  static std::string get(const std::string& value_type, const void* value_ptr)
162  {
163  if (value_type == info::get_type_name(info::TI_BOOL))
164  return *static_cast<const bool*>(value_ptr)?"true":"false";
165  if (value_type == info::get_type_name(info::TI_INT8))
166  return cgv::utils::to_string((int)*static_cast<const int8_type*>(value_ptr));
167  if (value_type == info::get_type_name(info::TI_INT16))
168  return cgv::utils::to_string(*static_cast<const int16_type*>(value_ptr));
169  if (value_type == info::get_type_name(info::TI_INT32))
170  return cgv::utils::to_string(*static_cast<const int32_type*>(value_ptr));
171  if (value_type == info::get_type_name(info::TI_INT64))
172  return cgv::utils::to_string(*static_cast<const int64_type*>(value_ptr));
173  if (value_type == info::get_type_name(info::TI_UINT8))
174  return cgv::utils::to_string((int)*static_cast<const uint8_type*>(value_ptr));
175  if (value_type == info::get_type_name(info::TI_UINT16))
176  return cgv::utils::to_string(*static_cast<const uint16_type*>(value_ptr));
177  if (value_type == info::get_type_name(info::TI_UINT32))
178  return cgv::utils::to_string(*static_cast<const uint32_type*>(value_ptr));
179  if (value_type == info::get_type_name(info::TI_UINT64))
180  return cgv::utils::to_string(*static_cast<const uint64_type*>(value_ptr));
181  if (value_type == info::get_type_name(info::TI_FLT32))
182  return cgv::utils::to_string(*static_cast<const flt32_type*>(value_ptr));
183  if (value_type == info::get_type_name(info::TI_FLT64))
184  return cgv::utils::to_string(*static_cast<const flt64_type*>(value_ptr));
185  if (value_type == info::get_type_name(info::TI_WCHAR))
186  return cgv::utils::wstr2str(std::wstring(*static_cast<const wchar_type*>(value_ptr), 1));
187  if (value_type == info::get_type_name(info::TI_STRING))
188  return *static_cast<const std::string*>(value_ptr);
189  if (value_type == info::get_type_name(info::TI_WSTRING))
190  return cgv::utils::wstr2str(*static_cast<const std::wstring*>(value_ptr));
191  return "";
192  }
193  static void set(const std::string& value, const std::string& value_type, void* value_ptr)
194  {
195  if (value_type == info::get_type_name(info::TI_BOOL))
196  *static_cast<bool*>(value_ptr) = value=="true"?true:false;
197  else if (value_type == info::get_type_name(info::TI_INT8))
198  *static_cast<int8_type*>(value_ptr) = atoi(value.c_str());
199  else if (value_type == info::get_type_name(info::TI_INT16))
200  *static_cast<int16_type*>(value_ptr) = (int16_type) atoi(value.c_str());
201  else if (value_type == info::get_type_name(info::TI_INT32))
202  *static_cast<int32_type*>(value_ptr) = (int32_type) atoi(value.c_str());
203  else if (value_type == info::get_type_name(info::TI_INT64))
204  *static_cast<int64_type*>(value_ptr) = (int64_type) atoi(value.c_str());
205  else if (value_type == info::get_type_name(info::TI_UINT8))
206  *static_cast<uint8_type*>(value_ptr) = (uint8_type) atoi(value.c_str());
207  else if (value_type == info::get_type_name(info::TI_UINT16))
208  *static_cast<uint16_type*>(value_ptr) = (uint16_type) atoi(value.c_str());
209  else if (value_type == info::get_type_name(info::TI_UINT32))
210  *static_cast<uint32_type*>(value_ptr) = (uint32_type) atoi(value.c_str());
211  else if (value_type == info::get_type_name(info::TI_UINT64))
212  *static_cast<uint64_type*>(value_ptr) = (uint64_type) atoi(value.c_str());
213  else if (value_type == info::get_type_name(info::TI_FLT32))
214  *static_cast<flt32_type*>(value_ptr) = (flt32_type) atof(value.c_str());
215  else if (value_type == info::get_type_name(info::TI_FLT64))
216  *static_cast<flt64_type*>(value_ptr) = (flt64_type) atof(value.c_str());
217  else if (value_type == info::get_type_name(info::TI_STRING))
218  *static_cast<std::string*>(value_ptr) = value;
219  else if (value_type == info::get_type_name(info::TI_WCHAR))
220  *static_cast<wchar_type*>(value_ptr) = value.empty() ? 0 : value[0];
221  else if (value_type == info::get_type_name(info::TI_WSTRING))
222  *static_cast<std::wstring*>(value_ptr) = cgv::utils::str2wstr(value);
223  }
224 };
225 
226 template <>
227 struct variant<std::wstring>
228 {
229  static std::wstring get(const std::string& value_type, const void* value_ptr)
230  {
231  if (value_type == info::get_type_name(info::TI_WSTRING))
232  return *static_cast<const std::wstring*>(value_ptr);
233  return cgv::utils::str2wstr(variant<std::string>::get(value_type, value_ptr));
234  }
235  static void set(const std::wstring& value, const std::string& value_type, void* value_ptr)
236  {
237  if (value_type == info::get_type_name(info::TI_WSTRING))
238  *static_cast<std::wstring*>(value_ptr) = value;
239  else {
240  std::string v = cgv::utils::wstr2str(value);
241  variant<std::string>::set(v, value_type, value_ptr);
242  }
243  }
244 };
245 
246 
247 template <>
248 struct variant<const char*>
249 {
250  static void set(const char* value, const std::string& value_type, void* value_ptr)
251  {
252  variant<std::string>::set(value?value:"", value_type, value_ptr);
253  }
254 };
255 
256 template <typename T>
257 void set_variant(const T& value, const std::string& value_type, void* value_ptr)
258 {
259  variant<T>::set(value,value_type,value_ptr);
260 }
261 
262 template <typename T>
263 void get_variant(T& value, const std::string& value_type, const void* value_ptr)
264 {
265  value = variant<T>::get(value_type,value_ptr);
266 }
267 
268 extern CGV_API void assign_variant(const std::string& dst_value_type, void* dst_value_ptr,
269  const std::string& src_value_type, const void* src_value_ptr);
270 
271  }
272 }
273 
274 #include <cgv/config/lib_end.h>
cgv::type::int32_type
int int32_type
this type provides an 32 bit signed integer type
Definition: standard_types.h:12
cgv::type::info::TI_WCHAR
@ TI_WCHAR
floating point type stored in 64 bits
Definition: type_id.h:30
cgv::type::flt64_type
double flt64_type
this type provides a 64 bit floating point type
Definition: standard_types.h:26
cgv::type::info::TI_INT32
@ TI_INT32
signed integer stored in 16 bits
Definition: type_id.h:21
cgv::type::uint8_type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
Definition: standard_types.h:16
cgv::type::info::TI_UINT64
@ TI_UINT64
unsigned integer stored in 32 bits
Definition: type_id.h:26
cgv::type::info::TI_STRING
@ TI_STRING
wide character type
Definition: type_id.h:31
cgv::utils::str2wstr
std::wstring str2wstr(const std::string &s)
convert a 8-bit string to a 16-bit string
Definition: convert.cxx:11
cgv::type::uint16_type
unsigned short uint16_type
this type provides an 16 bit unsigned integer type
Definition: standard_types.h:18
cgv::type::int16_type
short int16_type
this type provides an 16 bit signed integer type
Definition: standard_types.h:10
cgv::type::info::TI_FLT64
@ TI_FLT64
floating point type stored in 32 bits
Definition: type_id.h:29
cgv::type::info::TI_INT16
@ TI_INT16
signed integer stored in 8 bits
Definition: type_id.h:20
cgv::type::variant::set
static void set(const T &value, const std::string &value_type, void *value_ptr)
convert the first parameter of type T into value_type and store the value at the location pointed to ...
Definition: variant.h:56
cgv::type::info::TI_UINT32
@ TI_UINT32
unsigned integer stored in 16 bits
Definition: type_id.h:25
cgv::type::info::TI_UINT16
@ TI_UINT16
unsigned integer stored in 8 bits
Definition: type_id.h:24
cgv::utils::wstr2str
std::string wstr2str(const std::wstring &ws)
convert a 16-bit string to a 8-bit string
Definition: convert.cxx:26
cgv::type::info::TI_INT8
@ TI_INT8
boolean
Definition: type_id.h:19
cgv::type::info::TI_FLT32
@ TI_FLT32
floating point type stored in 16 bits
Definition: type_id.h:28
cgv::type::variant::get
static T get(const std::string &value_type, const void *value_ptr)
convert the value pointed to by value_ptr of type value_type to type T and return it
Definition: variant.h:23
cgv::type::int64_type
long long int64_type
this type provides an 64 bit signed integer type
Definition: standard_types.h:14
cgv::type::info::TI_INT64
@ TI_INT64
signed integer stored in 32 bits
Definition: type_id.h:22
cgv::type::info::TI_WSTRING
@ TI_WSTRING
string type
Definition: type_id.h:32
cgv::type::uint32_type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
Definition: standard_types.h:20
cgv::type::flt32_type
float flt32_type
this type provides a 32 bit floating point type
Definition: standard_types.h:24
cgv::type::info::TI_BOOL
@ TI_BOOL
void
Definition: type_id.h:18
cgv::type::info::TI_UINT8
@ TI_UINT8
signed integer stored in 64 bits
Definition: type_id.h:23
cgv::type::info::get_type_name
const char * get_type_name(TypeId tid)
function that returns the name of a type specified through TypeId
Definition: type_id.cxx:117
cgv::type::wchar_type
wchar_t wchar_type
wide character type
Definition: standard_types.h:28
cgv::utils::to_string
std::string to_string(const std::string &v, unsigned int w, unsigned int p)
specialization of conversion from string to strings
Definition: convert_string.cxx:7
cgv::type::variant
Definition: variant.h:21
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::type::uint64_type
unsigned long long uint64_type
this type provides an 64 bit unsigned integer type
Definition: standard_types.h:22
cgv::type::int8_type
char int8_type
this type provides an 8 bit signed integer type
Definition: standard_types.h:8