00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OST_INFO_TYPE_CAST_HH
00029 #define OST_INFO_TYPE_CAST_HH
00030
00031 #include <boost/regex.hpp>
00032 #include <boost/lexical_cast.hpp>
00033
00034 namespace ost { namespace info { namespace detail {
00035
00036 namespace {
00037
00038 struct ItemCastError
00039 {};
00040
00041 template <typename IN, typename OUT>
00042 OUT do_cast(const IN& in) {
00043 try {
00044 return boost::lexical_cast<OUT>(in);
00045 } catch (boost::bad_lexical_cast &) {
00046 throw ItemCastError();
00047 }
00048 return OUT();
00049 }
00050
00051
00052
00053 template <int INTYPE, int OUTTYPE>
00054 void set_new_type(EleImpl& item);
00055
00056 template <> void set_new_type<IT_STRING,IT_STRING>(EleImpl& item)
00057 {
00058
00059 }
00060
00061 template <> void set_new_type<IT_STRING,IT_INT>(EleImpl& item)
00062 {
00063 try {
00064 item.SetIntRepr(do_cast<String, int>(item.GetStringRepr()));
00065 } catch (ItemCastError&) {
00066 item.SetIntRepr(0);
00067 }
00068 }
00069
00070 template <> void set_new_type<IT_STRING,IT_FLOAT>(EleImpl& item)
00071 {
00072 try {
00073 item.SetFloatRepr(do_cast<String, float>(item.GetStringRepr()));
00074 } catch (ItemCastError&) {
00075 item.SetFloatRepr(0.0);
00076 }
00077 }
00078
00079 template <> void set_new_type<IT_STRING,IT_BOOL>(EleImpl& item)
00080 {
00081 const int Strings = 3;
00082 String trueStrings[] = {"true", "1", "yes"};
00083 String falseStrings[] = {"false", "0", "no"};
00084
00085 String value = item.GetStringRepr();
00086
00087 for(int i=0;i<Strings;++i) {
00088 if(value == trueStrings[i]) {
00089 item.SetBoolRepr(true);
00090 i = Strings;
00091 } else if(value == falseStrings[i]) {
00092 item.SetBoolRepr(false);
00093 i = Strings;
00094 } else {
00095 }
00096 }
00097 }
00098
00099 template <> void set_new_type<IT_STRING,IT_VECTOR>(EleImpl& item)
00100 {
00101 static String num("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
00102 boost::regex expression("\\[ *("+num+") *, *("+num+") *, *("+num+") *\\]");
00103 boost::cmatch what;
00104
00105 if(boost::regex_match(item.GetStringRepr().c_str(), what, expression)) {
00106 item.SetVecRepr(geom::Vec3(do_cast<String, Real>(what[1]),
00107 do_cast<String, Real>(what[3]),
00108 do_cast<String, Real>(what[5])));
00109 } else {
00110 item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
00111 }
00112 }
00113
00114 template <> void set_new_type<IT_INT,IT_STRING>(EleImpl& item)
00115 {
00116 try {
00117 item.SetStringRepr(do_cast<int, String>(item.GetIntRepr()),true);
00118 } catch (ItemCastError&) {
00119 item.SetStringRepr("",true);
00120 }
00121 }
00122
00123 template <> void set_new_type<IT_INT,IT_INT>(EleImpl& item)
00124 {
00125
00126 }
00127
00128 template <> void set_new_type<IT_INT,IT_FLOAT>(EleImpl& item)
00129 {
00130 try {
00131 item.SetFloatRepr(do_cast<int, float>(item.GetIntRepr()));
00132 } catch (ItemCastError&) {
00133 item.SetFloatRepr(0.0);
00134 }
00135 }
00136
00137 template <> void set_new_type<IT_INT,IT_BOOL>(EleImpl& item)
00138 {
00139 if(item.GetIntRepr() == 0) {
00140 item.SetBoolRepr(false);
00141 } else {
00142 item.SetBoolRepr(true);
00143 }
00144 }
00145
00146 template <> void set_new_type<IT_INT,IT_VECTOR>(EleImpl& item)
00147 {
00148 try {
00149 item.SetVecRepr(geom::Vec3(do_cast<int, Real>(item.GetIntRepr()), 0.0, 0.0));
00150 } catch (ItemCastError&) {
00151 item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
00152 }
00153 }
00154
00155 template <> void set_new_type<IT_FLOAT,IT_STRING>(EleImpl& item)
00156 {
00157 try {
00158 item.SetStringRepr(do_cast<float, String>(item.GetFloatRepr()),true);
00159 } catch (ItemCastError&) {
00160 item.SetStringRepr("",true);
00161 }
00162 }
00163
00164 template <> void set_new_type<IT_FLOAT,IT_INT>(EleImpl& item)
00165 {
00166 item.SetIntRepr(static_cast<int>(round(item.GetFloatRepr())));
00167 }
00168
00169 template <> void set_new_type<IT_FLOAT,IT_FLOAT>(EleImpl& item)
00170 {
00171
00172 }
00173
00174 template <> void set_new_type<IT_FLOAT,IT_BOOL>(EleImpl& item)
00175 {
00176 if(item.GetFloatRepr() == 0.0) {
00177 item.SetBoolRepr(false);
00178 } else {
00179 item.SetBoolRepr(true);
00180 }
00181 }
00182
00183 template <> void set_new_type<IT_FLOAT,IT_VECTOR>(EleImpl& item)
00184 {
00185 item.SetVecRepr(geom::Vec3(item.GetFloatRepr(), 0.0, 0.0));
00186 }
00187
00188 template <> void set_new_type<IT_BOOL,IT_STRING>(EleImpl& item)
00189 {
00190 try {
00191 item.SetStringRepr(do_cast<bool, String>(item.GetBoolRepr()),true);
00192 } catch (ItemCastError&) {
00193 item.SetStringRepr("",true);
00194 }
00195 }
00196
00197 template <> void set_new_type<IT_BOOL,IT_INT>(EleImpl& item)
00198 {
00199 try {
00200 item.SetIntRepr(do_cast<bool, int>(item.GetBoolRepr()));
00201 } catch (ItemCastError&) {
00202 item.SetIntRepr(0);
00203 }
00204 }
00205
00206 template <> void set_new_type<IT_BOOL,IT_FLOAT>(EleImpl& item)
00207 {
00208 try {
00209 item.SetFloatRepr(do_cast<bool, float>(item.GetBoolRepr()));
00210 } catch (ItemCastError&) {
00211 item.SetFloatRepr(0.0);
00212 }
00213 }
00214
00215 template <> void set_new_type<IT_BOOL,IT_BOOL>(EleImpl& item)
00216 {
00217
00218 }
00219
00220 template <> void set_new_type<IT_BOOL,IT_VECTOR>(EleImpl& item)
00221 {
00222 try {
00223 item.SetVecRepr(geom::Vec3(do_cast<bool, Real>(item.GetBoolRepr()), 0.0, 0.0));
00224 } catch (ItemCastError&) {
00225 item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
00226 }
00227 }
00228
00229 template <> void set_new_type<IT_VECTOR,IT_STRING>(EleImpl& item)
00230 {
00231 std::ostringstream str;
00232 str << item.GetVecRepr();
00233 item.SetStringRepr(str.str(),true);
00234 }
00235
00236 template <> void set_new_type<IT_VECTOR,IT_INT>(EleImpl& item)
00237 {
00238 try {
00239 item.SetIntRepr(do_cast<Real, int>(item.GetVecRepr()[0]));
00240 } catch (ItemCastError&) {
00241 item.SetIntRepr(0);
00242 }
00243 }
00244
00245 template <> void set_new_type<IT_VECTOR,IT_FLOAT>(EleImpl& item)
00246 {
00247 item.SetFloatRepr(item.GetVecRepr()[0]);
00248 }
00249
00250 template <> void set_new_type<IT_VECTOR,IT_BOOL>(EleImpl& item)
00251 {
00252 if(item.GetVecRepr()[0] == 0.0) {
00253 item.SetBoolRepr(false);
00254 } else {
00255 item.SetBoolRepr(true);
00256 }
00257 }
00258
00259 template <> void set_new_type<IT_VECTOR,IT_VECTOR>(EleImpl& item)
00260 {
00261
00262 }
00263
00264 }
00265
00266 #define ITEM_TYPE_CAST_EVAL(INTYPE) \
00267 if(new_type==IT_STRING) { \
00268 set_new_type< INTYPE ,IT_STRING>(item); \
00269 } else if(new_type==IT_INT) { \
00270 set_new_type< INTYPE ,IT_INT>(item); \
00271 } else if(new_type==IT_FLOAT) { \
00272 set_new_type< INTYPE ,IT_FLOAT>(item); \
00273 } else if(new_type==IT_BOOL) { \
00274 set_new_type< INTYPE ,IT_BOOL>(item); \
00275 } else if(new_type==IT_VECTOR) { \
00276 set_new_type< INTYPE ,IT_VECTOR>(item); \
00277 }
00278
00280 void SetInfoItemNewType(EleImpl& item, int new_type)
00281 {
00282 Type in_type=item.GetType();
00283 if(in_type==IT_STRING) {
00284 ITEM_TYPE_CAST_EVAL(IT_STRING)
00285 } else if(in_type==IT_INT) {
00286 ITEM_TYPE_CAST_EVAL(IT_INT)
00287 } else if(in_type==IT_FLOAT) {
00288 ITEM_TYPE_CAST_EVAL(IT_FLOAT)
00289 } else if(in_type==IT_BOOL) {
00290 ITEM_TYPE_CAST_EVAL(IT_BOOL)
00291 } else if(in_type==IT_VECTOR) {
00292 ITEM_TYPE_CAST_EVAL(IT_VECTOR)
00293 }
00294 }
00295
00296 void SetInfoItemNewType(EleImpl& item, Type new_type)
00297 {
00298 Type in_type=item.GetType();
00299 if(in_type==IT_STRING) {
00300 ITEM_TYPE_CAST_EVAL(IT_STRING)
00301 } else if(in_type==IT_INT) {
00302 ITEM_TYPE_CAST_EVAL(IT_INT)
00303 } else if(in_type==IT_FLOAT) {
00304 ITEM_TYPE_CAST_EVAL(IT_FLOAT)
00305 } else if(in_type==IT_BOOL) {
00306 ITEM_TYPE_CAST_EVAL(IT_BOOL)
00307 } else if(in_type==IT_VECTOR) {
00308 ITEM_TYPE_CAST_EVAL(IT_VECTOR)
00309 }
00310 }
00311
00312 }}}
00313
00314 #endif