28#ifndef OST_INFO_TYPE_CAST_HH
29#define OST_INFO_TYPE_CAST_HH
31#include <boost/regex.hpp>
32#include <boost/lexical_cast.hpp>
34namespace ost {
namespace info {
namespace detail {
41template <
typename IN,
typename OUT>
42OUT do_cast(
const IN& in) {
44 return boost::lexical_cast<OUT>(in);
45 }
catch (boost::bad_lexical_cast &) {
46 throw ItemCastError();
53template <
int INTYPE,
int OUTTYPE>
54void set_new_type(EleImpl& item);
56template <>
void set_new_type<IT_STRING,IT_STRING>(EleImpl& item)
61template <>
void set_new_type<IT_STRING,IT_INT>(EleImpl& item)
64 item.SetIntRepr(do_cast<String, int>(item.GetStringRepr()));
65 }
catch (ItemCastError&) {
70template <>
void set_new_type<IT_STRING,IT_FLOAT>(EleImpl& item)
73 item.SetFloatRepr(do_cast<String, float>(item.GetStringRepr()));
74 }
catch (ItemCastError&) {
75 item.SetFloatRepr(0.0);
79template <>
void set_new_type<IT_STRING,IT_BOOL>(EleImpl& item)
81 const int Strings = 3;
82 String trueStrings[] = {
"true",
"1",
"yes"};
83 String falseStrings[] = {
"false",
"0",
"no"};
85 String value = item.GetStringRepr();
87 for(
int i=0;i<Strings;++i) {
88 if(value == trueStrings[i]) {
89 item.SetBoolRepr(
true);
91 }
else if(value == falseStrings[i]) {
92 item.SetBoolRepr(
false);
99template <>
void set_new_type<IT_STRING,IT_VECTOR>(EleImpl& item)
101 static String num(
"[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
102 boost::regex expression(
"\\[ *("+num+
") *, *("+num+
") *, *("+num+
") *\\]");
105 if(boost::regex_match(item.GetStringRepr().c_str(), what, expression)) {
106 item.SetVecRepr(
geom::Vec3(do_cast<String, Real>(what[1]),
107 do_cast<String, Real>(what[3]),
108 do_cast<String, Real>(what[5])));
114template <>
void set_new_type<IT_INT,IT_STRING>(EleImpl& item)
117 item.SetStringRepr(do_cast<int, String>(item.GetIntRepr()),
true);
118 }
catch (ItemCastError&) {
119 item.SetStringRepr(
"",
true);
123template <>
void set_new_type<IT_INT,IT_INT>(EleImpl& item)
128template <>
void set_new_type<IT_INT,IT_FLOAT>(EleImpl& item)
131 item.SetFloatRepr(do_cast<int, float>(item.GetIntRepr()));
132 }
catch (ItemCastError&) {
133 item.SetFloatRepr(0.0);
137template <>
void set_new_type<IT_INT,IT_BOOL>(EleImpl& item)
139 if(item.GetIntRepr() == 0) {
140 item.SetBoolRepr(
false);
142 item.SetBoolRepr(
true);
146template <>
void set_new_type<IT_INT,IT_VECTOR>(EleImpl& item)
149 item.SetVecRepr(
geom::Vec3(do_cast<int, Real>(item.GetIntRepr()), 0.0, 0.0));
150 }
catch (ItemCastError&) {
155template <>
void set_new_type<IT_FLOAT,IT_STRING>(EleImpl& item)
158 item.SetStringRepr(do_cast<float, String>(item.GetFloatRepr()),
true);
159 }
catch (ItemCastError&) {
160 item.SetStringRepr(
"",
true);
164template <>
void set_new_type<IT_FLOAT,IT_INT>(EleImpl& item)
166 item.SetIntRepr(
static_cast<int>(round(item.GetFloatRepr())));
169template <>
void set_new_type<IT_FLOAT,IT_FLOAT>(EleImpl& item)
174template <>
void set_new_type<IT_FLOAT,IT_BOOL>(EleImpl& item)
176 if(item.GetFloatRepr() == 0.0) {
177 item.SetBoolRepr(
false);
179 item.SetBoolRepr(
true);
183template <>
void set_new_type<IT_FLOAT,IT_VECTOR>(EleImpl& item)
185 item.SetVecRepr(
geom::Vec3(item.GetFloatRepr(), 0.0, 0.0));
188template <>
void set_new_type<IT_BOOL,IT_STRING>(EleImpl& item)
191 item.SetStringRepr(do_cast<bool, String>(item.GetBoolRepr()),
true);
192 }
catch (ItemCastError&) {
193 item.SetStringRepr(
"",
true);
197template <>
void set_new_type<IT_BOOL,IT_INT>(EleImpl& item)
200 item.SetIntRepr(do_cast<bool, int>(item.GetBoolRepr()));
201 }
catch (ItemCastError&) {
206template <>
void set_new_type<IT_BOOL,IT_FLOAT>(EleImpl& item)
209 item.SetFloatRepr(do_cast<bool, float>(item.GetBoolRepr()));
210 }
catch (ItemCastError&) {
211 item.SetFloatRepr(0.0);
215template <>
void set_new_type<IT_BOOL,IT_BOOL>(EleImpl& item)
220template <>
void set_new_type<IT_BOOL,IT_VECTOR>(EleImpl& item)
223 item.SetVecRepr(
geom::Vec3(do_cast<bool, Real>(item.GetBoolRepr()), 0.0, 0.0));
224 }
catch (ItemCastError&) {
229template <>
void set_new_type<IT_VECTOR,IT_STRING>(EleImpl& item)
231 std::ostringstream str;
232 str << item.GetVecRepr();
233 item.SetStringRepr(str.str(),
true);
236template <>
void set_new_type<IT_VECTOR,IT_INT>(EleImpl& item)
239 item.SetIntRepr(do_cast<Real, int>(item.GetVecRepr()[0]));
240 }
catch (ItemCastError&) {
245template <>
void set_new_type<IT_VECTOR,IT_FLOAT>(EleImpl& item)
247 item.SetFloatRepr(item.GetVecRepr()[0]);
250template <>
void set_new_type<IT_VECTOR,IT_BOOL>(EleImpl& item)
252 if(item.GetVecRepr()[0] == 0.0) {
253 item.SetBoolRepr(
false);
255 item.SetBoolRepr(
true);
259template <>
void set_new_type<IT_VECTOR,IT_VECTOR>(EleImpl& item)
266#define ITEM_TYPE_CAST_EVAL(INTYPE) \
267 if(new_type==IT_STRING) { \
268 set_new_type< INTYPE ,IT_STRING>(item); \
269 } else if(new_type==IT_INT) { \
270 set_new_type< INTYPE ,IT_INT>(item); \
271 } else if(new_type==IT_FLOAT) { \
272 set_new_type< INTYPE ,IT_FLOAT>(item); \
273 } else if(new_type==IT_BOOL) { \
274 set_new_type< INTYPE ,IT_BOOL>(item); \
275 } else if(new_type==IT_VECTOR) { \
276 set_new_type< INTYPE ,IT_VECTOR>(item); \
285 }
else if(in_type==
IT_INT) {
301 }
else if(in_type==
IT_INT) {
Three dimensional vector class, using Real precision.
#define ITEM_TYPE_CAST_EVAL(INTYPE)
void SetInfoItemNewType(EleImpl &item, int new_type)
this is the public interface