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 "
57 class CastVisitor<std::string> :
public boost::static_visitor<std::string> {
60 template <
typename From>
69 class CastVisitor<double> :
public boost::static_visitor<double> {
71 template <
typename From>
72 static constexpr
bool generic() {
77 template <
typename From>
79 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to "
80 <<
typeid(double).name();
83 template <
typename From>
89 char* endptr =
nullptr;
91 if (endptr == from.
c_str()) {
93 <<
typeid(double).name();
95 if (value == HUGE_VAL || value == -HUGE_VAL) {
96 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to "
97 <<
typeid(double).name();
106 template <
typename From>
107 static constexpr
bool generic() {
113 template <
typename From>
115 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to "
116 <<
typeid(float).name();
119 template <
typename From>
125 char* endptr =
nullptr;
127 if (endptr == from.
c_str()) {
128 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to "
129 <<
typeid(float).name();
131 if (value == HUGE_VALF || value == -HUGE_VALF) {
132 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to "
133 <<
typeid(float).name();
140 class CastVisitor<int64_t> :
public boost::static_visitor<int64_t> {
142 template <
typename From>
143 static constexpr
bool generic() {
148 template <
typename From>
150 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to "
151 <<
typeid(int64_t).name();
154 template <
typename From>
160 char* endptr =
nullptr;
162 if (endptr == from.
c_str()) {
163 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to "
164 <<
typeid(int64_t).name();
171 class CastVisitor<int32_t> :
public boost::static_visitor<int32_t> {
173 template <
typename From>
174 static constexpr
bool generic() {
180 template <
typename From>
182 throw Elements::Exception() <<
"CastVisitor cannot convert " <<
typeid(From).name() <<
" type to "
183 <<
typeid(int32_t).name();
186 template <
typename From>
192 char* endptr =
nullptr;
194 if (endptr == from.
c_str()) {
195 throw Elements::Exception() <<
"CastVisitor cannot convert the string '" << from <<
"' to "
196 <<
typeid(int32_t).name();
198 if (value > INT32_MAX || value < INT32_MIN) {
199 throw Elements::Exception() <<
"CastVisitor overflows converting the string '" << from <<
"' to "
200 <<
typeid(int32_t).name();
202 return static_cast<int32_t
>(value);
206 template <
typename VectorType>
207 class CastVisitor<std::vector<VectorType>> :
public boost::static_visitor<std::vector<VectorType>> {
210 template <
typename From>
217 template <
typename From>
220 for (
auto v : from) {
228 boost::char_separator<char> sep{
","};
229 boost::tokenizer<boost::char_separator<char>> tok{from, sep};
230 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