25 #ifndef _TABLE_CASTVISITOR_H
26 #define _TABLE_CASTVISITOR_H
29 #include <boost/tokenizer.hpp>
30 #include <boost/variant/static_visitor.hpp>
33 #include <type_traits>
40 template <
typename To>
44 template <
typename From>
49 template <
typename From>
51 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to " <<
typeid(To).name();
56 class CastVisitor<std::string> :
public boost::static_visitor<std::string> {
59 template <
typename From>
68 class CastVisitor<double> :
public boost::static_visitor<double> {
70 template <
typename From>
71 static constexpr
bool generic() {
76 template <
typename From>
78 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to " <<
typeid(double).name();
81 template <
typename From>
87 char* endptr =
nullptr;
89 if (endptr == from.
c_str()) {
90 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to " <<
typeid(double).name();
92 if (value == HUGE_VAL || value == -HUGE_VAL) {
93 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to " <<
typeid(double).name();
102 template <
typename From>
103 static constexpr
bool generic() {
109 template <
typename From>
111 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to " <<
typeid(float).name();
114 template <
typename From>
120 char* endptr =
nullptr;
122 if (endptr == from.
c_str()) {
123 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to " <<
typeid(float).name();
125 if (value == HUGE_VALF || value == -HUGE_VALF) {
126 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to " <<
typeid(float).name();
133 class CastVisitor<int64_t> :
public boost::static_visitor<int64_t> {
135 template <
typename From>
136 static constexpr
bool generic() {
141 template <
typename From>
143 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to " <<
typeid(int64_t).name();
146 template <
typename From>
152 char* endptr =
nullptr;
154 if (endptr == from.
c_str()) {
155 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to " <<
typeid(int64_t).name();
162 class CastVisitor<int32_t> :
public boost::static_visitor<int32_t> {
164 template <
typename From>
165 static constexpr
bool generic() {
171 template <
typename From>
173 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to " <<
typeid(int32_t).name();
176 template <
typename From>
182 char* endptr =
nullptr;
184 if (endptr == from.
c_str()) {
185 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to " <<
typeid(int32_t).name();
187 if (value > INT32_MAX || value < INT32_MIN) {
188 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to " <<
typeid(int32_t).name();
190 return static_cast<int32_t
>(value);
194 template <
typename VectorType>
195 class CastVisitor<std::vector<VectorType>> :
public boost::static_visitor<std::vector<VectorType>> {
198 template <
typename From>
205 template <
typename From>
208 for (
auto v : from) {
216 boost::char_separator<char> sep{
","};
217 boost::tokenizer<boost::char_separator<char>> tok{from, sep};
218 for (
auto&
s : tok) {
double operator()(const From &, typename std::enable_if<!generic< From >()>::type *=0) const
To operator()(const From &from, typename std::enable_if< std::is_same< From, To >::value >::type *=0) const
float operator()(const std::string &from) const
int64_t operator()(const From &from, typename std::enable_if< generic< From >()>::type *=0) const
float operator()(const From &from, typename std::enable_if< generic< From >()>::type *=0) const
double operator()(const From &, typename std::enable_if<!generic< From >()>::type *=0) const
double operator()(const std::string &from) const
double operator()(const From &from, typename std::enable_if< generic< From >()>::type *=0) const
std::vector< VectorType > operator()(const std::vector< From > &from) const
const std::vector< VectorType > & operator()(const std::vector< VectorType > &from) const
int32_t operator()(const From &from, typename std::enable_if< generic< From >()>::type *=0) const
double operator()(const From &, typename std::enable_if<!generic< From >()>::type *=0) const
double operator()(const From &, typename std::enable_if<!generic< From >()>::type *=0) const
std::vector< VectorType > operator()(const std::string &from) const
std::string operator()(const From &from) const
int64_t operator()(const std::string &from) const
int32_t operator()(const std::string &from) const
std::vector< VectorType > operator()(const From &from) const
To operator()(const From &, typename std::enable_if<!std::is_same< From, To >::value >::type *=0) const