34 #ifndef _Wolframe_SERIALIZE_STRUCT_PARSE_VALUE_HPP_INCLUDED
35 #define _Wolframe_SERIALIZE_STRUCT_PARSE_VALUE_HPP_INCLUDED
41 #include <boost/lexical_cast.hpp>
42 #include <boost/numeric/conversion/cast.hpp>
43 #include <boost/utility/enable_if.hpp>
44 #include <boost/type_traits.hpp>
62 static typename boost::enable_if_c<
63 boost::is_same<T,std::string>::value
64 ,
const String&>::type
get(
const T&) {
static String rt;
return rt;}
69 static typename boost::enable_if_c<
70 boost::is_same<T,bool>::value
71 ,
const Bool&>::type
get(
const T&) {
static Bool rt;
return rt;}
76 static typename boost::enable_if_c<
77 (boost::is_arithmetic<T>::value && !boost::is_same<T,bool>::value && !boost::is_same<T,std::string>::value)
78 ,
const Arithmetic&>::type
get(
const T&) {
static Arithmetic rt;
return rt;}
83 static typename boost::enable_if_c<
84 boost::is_same<T,_Wolframe::types::DateTime>::value
85 ,
const DateTime&>::type
get(
const T&) {
static DateTime rt;
return rt;}
90 static typename boost::enable_if_c<
91 boost::is_same<T,_Wolframe::types::BigNumber>::value
92 ,
const BigNumber&>::type
get(
const T&) {
static BigNumber rt;
return rt;}
97 static bool getBool(
bool& val,
int boolnum)
104 else if (boolnum == 1)
112 template <
typename ValueType>
115 using namespace _Wolframe;
122 catch (
const boost::bad_lexical_cast&){}
123 catch (
const std::runtime_error&){}
127 template <
typename ValueType>
130 using namespace _Wolframe;
134 switch (element.
type())
136 case types::Variant::Null:
139 case types::Variant::Bool:
143 case types::Variant::Double:
144 return getBool( val, boost::numeric_cast<int>( element.
todouble()));
146 case types::Variant::Int:
147 return getBool( val, boost::numeric_cast<ValueType>( element.
toint()));
149 case types::Variant::UInt:
150 return getBool( val, boost::numeric_cast<ValueType>( element.
touint()));
152 case types::Variant::String:
154 const char* cc = element.
charptr();
155 std::size_t ccsize = element.
charsize();
157 if (ccsize == 4 && boost::algorithm::iequals( cc,
"true"))
162 if (ccsize == 5 && boost::algorithm::iequals( cc,
"false"))
167 if (ccsize == 3 && boost::algorithm::iequals( cc,
"yes"))
172 if (ccsize == 2 && boost::algorithm::iequals( cc,
"no"))
179 if ((cc[0]|32) ==
't' || (cc[0]|32) ==
'y')
184 if ((cc[0]|32) ==
'f' || (cc[0]|32) ==
'n')
192 case types::Variant::BigNumber:
193 throw std::runtime_error(
"cannot convert big number type to boolean value");
195 throw std::runtime_error(
"cannot convert timestamp type to boolean value");
196 case types::Variant::Custom:
202 && baseval.
type() != types::Variant::Custom)
207 catch (
const std::runtime_error& e)
209 throw std::runtime_error( std::string(
"cannot convert custom data type to boolean value: ") + e.what());
211 throw std::runtime_error(
"cannot convert custom data type to boolean value");
215 catch (
const boost::bad_lexical_cast&){}
219 template <
typename ValueType>
222 using namespace _Wolframe;
226 switch (element.
type())
228 case types::Variant::Null:
231 case types::Variant::Bool:
232 val = boost::numeric_cast<ValueType>( element.
tobool());
235 case types::Variant::Double:
236 val = boost::numeric_cast<ValueType>( element.
todouble());
239 case types::Variant::Int:
240 val = boost::numeric_cast<ValueType>( element.
toint());
243 case types::Variant::UInt:
244 val = boost::numeric_cast<ValueType>( element.
touint());
248 throw std::runtime_error(
"cannot convert timestamp to arithmetic type");
250 case types::Variant::BigNumber:
251 val = boost::lexical_cast<ValueType>( element.
tostring());
254 case types::Variant::String:
255 val = boost::lexical_cast<ValueType>( element.
tostring());
258 case types::Variant::Custom:
264 && baseval.
type() != types::Variant::Custom)
268 val = boost::lexical_cast<ValueType>( element.
tostring());
271 catch (
const std::runtime_error& e)
273 throw std::runtime_error( std::string(
"cannot convert custom data type to arithmetic value: ") + e.what());
278 catch (
const boost::bad_lexical_cast&){}
279 catch (
const boost::bad_numeric_cast&){}
283 template <
typename ValueType>
286 using namespace _Wolframe;
290 switch (element.
type())
292 case types::Variant::Null:
295 case types::Variant::Bool:
299 case types::Variant::Double:
303 case types::Variant::Int:
307 case types::Variant::UInt:
312 throw std::runtime_error(
"cannot convert timestamp to big number");
314 case types::Variant::BigNumber:
318 case types::Variant::String:
322 case types::Variant::Custom:
328 && baseval.
type() != types::Variant::Custom)
335 catch (
const std::runtime_error& e)
337 throw std::runtime_error( std::string(
"cannot convert custom data type to bignumber value: ") + e.what());
342 catch (
const boost::bad_lexical_cast&){}
343 catch (
const boost::bad_numeric_cast&){}
347 template <
typename ValueType>
350 using namespace _Wolframe;
354 switch (element.
type())
356 case types::Variant::Null:
357 case types::Variant::Bool:
358 case types::Variant::Double:
359 case types::Variant::Int:
360 case types::Variant::UInt:
367 case types::Variant::BigNumber:
370 case types::Variant::String:
374 case types::Variant::Custom:
380 && baseval.
type() != types::Variant::Custom)
387 catch (
const std::runtime_error& e)
389 throw std::runtime_error( std::string(
"cannot convert custom data type to datetime value: ") + e.what());
394 catch (
const boost::bad_lexical_cast&){}
395 catch (
const boost::bad_numeric_cast&){}
401 namespace _Wolframe {
402 namespace serialize {
404 template <
typename ValueType>
boost::uint64_t Timestamp
Timestamp equivalent to a date time value for variant type.
Definition: datetime.hpp:47
Application wide definitions of integer number value types.
Type for representing arbitrary precision fixed point numbers and big integers as binary coded decima...
virtual bool getBaseTypeValue(Variant &) const
Definition: customDataType.hpp:81
Custom data type interface for variant.
Type for representing big numbers as binary coded decimal (BCD) numbers.
Definition: bignumber.hpp:49
Data::UInt touint() const
Data type for normalized date time (absolute time without time zone info)
Definition: datetime.hpp:55
#define _WOLFRAME_UINTEGER
Definition: integer.hpp:42
Forward declaration.
Definition: variant.hpp:65
Variant value type that represents a variant copy without content ownership.
Definition: variant.hpp:286
Conversions of atomic types.
Type type() const
Get the type of this.
Definition: variant.hpp:191
std::string tostring() const
Getter with value conversion.
const CustomDataValue * customref() const
Get the pointer to the custom data object (throws for non custom data type)
Definition: variant.hpp:201
Date and datetime value type.
Data::Timestamp totimestamp() const
char * charptr() const
Get the pointer to the C representation of a string (throws for non string)
Definition: variant.hpp:197
std::size_t charsize() const
Get the size of a string (throws for non string)
Definition: variant.hpp:199
bool parseValue(ValueType &val, const types::VariantConst &element)
Definition: parseValue.hpp:405
const types::BigNumber * bignumref() const
Get the pointer to the big number object (throws for non big number data type)
Definition: variant.hpp:205