OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
item_type_cast.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  Collection of template specializations for
23  info item type interconversion
24 
25  Author: Johan Hebert, Ansgar Philippsen
26 */
27 
28 #ifndef OST_INFO_TYPE_CAST_HH
29 #define OST_INFO_TYPE_CAST_HH
30 
31 #include <boost/regex.hpp>
32 #include <boost/lexical_cast.hpp>
33 
34 namespace ost { namespace info { namespace detail {
35 
36 namespace {
37 
38 struct ItemCastError
39 {};
40 
41 template <typename IN, typename OUT>
42 OUT do_cast(const IN& in) {
43  try {
44  return boost::lexical_cast<OUT>(in);
45  } catch (boost::bad_lexical_cast &) {
46  throw ItemCastError();
47  }
48  return OUT(); // should never get here, but makes some compilers happy
49 }
50 
51 // this is the to-be-specialized template function
52 // -> implementation detail
53 template <int INTYPE, int OUTTYPE>
54 void set_new_type(EleImpl& item);
55 
56 template <> void set_new_type<IT_STRING,IT_STRING>(EleImpl& item)
57 {
58  //item.SetStringRepr(item.GetStringRepr(),true);
59 }
60 
61 template <> void set_new_type<IT_STRING,IT_INT>(EleImpl& item)
62 {
63  try {
64  item.SetIntRepr(do_cast<String, int>(item.GetStringRepr()));
65  } catch (ItemCastError&) {
66  item.SetIntRepr(0);
67  }
68 }
69 
70 template <> void set_new_type<IT_STRING,IT_FLOAT>(EleImpl& item)
71 {
72  try {
73  item.SetFloatRepr(do_cast<String, float>(item.GetStringRepr()));
74  } catch (ItemCastError&) {
75  item.SetFloatRepr(0.0);
76  }
77 }
78 
79 template <> void set_new_type<IT_STRING,IT_BOOL>(EleImpl& item)
80 {
81  const int Strings = 3;
82  String trueStrings[] = {"true", "1", "yes"};
83  String falseStrings[] = {"false", "0", "no"};
84 
85  String value = item.GetStringRepr();
86 
87  for(int i=0;i<Strings;++i) {
88  if(value == trueStrings[i]) {
89  item.SetBoolRepr(true);
90  i = Strings;
91  } else if(value == falseStrings[i]) {
92  item.SetBoolRepr(false);
93  i = Strings;
94  } else {
95  }
96  }
97 }
98 
99 template <> void set_new_type<IT_STRING,IT_VECTOR>(EleImpl& item)
100 {
101  static String num("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
102  boost::regex expression("\\(("+num+"),("+num+"),("+num+")\\)");
103  boost::cmatch what;
104 
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])));
109  } else {
110  item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
111  }
112 }
113 
114 template <> void set_new_type<IT_INT,IT_STRING>(EleImpl& item)
115 {
116  try {
117  item.SetStringRepr(do_cast<int, String>(item.GetIntRepr()),true);
118  } catch (ItemCastError&) {
119  item.SetStringRepr("",true);
120  }
121 }
122 
123 template <> void set_new_type<IT_INT,IT_INT>(EleImpl& item)
124 {
125  //item.SetIntRepr(item.GetIntRepr());
126 }
127 
128 template <> void set_new_type<IT_INT,IT_FLOAT>(EleImpl& item)
129 {
130  try {
131  item.SetFloatRepr(do_cast<int, float>(item.GetIntRepr()));
132  } catch (ItemCastError&) {
133  item.SetFloatRepr(0.0);
134  }
135 }
136 
137 template <> void set_new_type<IT_INT,IT_BOOL>(EleImpl& item)
138 {
139  if(item.GetIntRepr() == 0) {
140  item.SetBoolRepr(false);
141  } else {
142  item.SetBoolRepr(true);
143  }
144 }
145 
146 template <> void set_new_type<IT_INT,IT_VECTOR>(EleImpl& item)
147 {
148  try {
149  item.SetVecRepr(geom::Vec3(do_cast<int, Real>(item.GetIntRepr()), 0.0, 0.0));
150  } catch (ItemCastError&) {
151  item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
152  }
153 }
154 
155 template <> void set_new_type<IT_FLOAT,IT_STRING>(EleImpl& item)
156 {
157  try {
158  item.SetStringRepr(do_cast<float, String>(item.GetFloatRepr()),true);
159  } catch (ItemCastError&) {
160  item.SetStringRepr("",true);
161  }
162 }
163 
164 template <> void set_new_type<IT_FLOAT,IT_INT>(EleImpl& item)
165 {
166  item.SetIntRepr(static_cast<int>(round(item.GetFloatRepr())));
167 }
168 
169 template <> void set_new_type<IT_FLOAT,IT_FLOAT>(EleImpl& item)
170 {
171  //item.SetFloatRepr(item.GetFloatRepr());
172 }
173 
174 template <> void set_new_type<IT_FLOAT,IT_BOOL>(EleImpl& item)
175 {
176  if(item.GetFloatRepr() == 0.0) {
177  item.SetBoolRepr(false);
178  } else {
179  item.SetBoolRepr(true);
180  }
181 }
182 
183 template <> void set_new_type<IT_FLOAT,IT_VECTOR>(EleImpl& item)
184 {
185  item.SetVecRepr(geom::Vec3(item.GetFloatRepr(), 0.0, 0.0));
186 }
187 
188 template <> void set_new_type<IT_BOOL,IT_STRING>(EleImpl& item)
189 {
190  try {
191  item.SetStringRepr(do_cast<bool, String>(item.GetBoolRepr()),true);
192  } catch (ItemCastError&) {
193  item.SetStringRepr("",true);
194  }
195 }
196 
197 template <> void set_new_type<IT_BOOL,IT_INT>(EleImpl& item)
198 {
199  try {
200  item.SetIntRepr(do_cast<bool, int>(item.GetBoolRepr()));
201  } catch (ItemCastError&) {
202  item.SetIntRepr(0);
203  }
204 }
205 
206 template <> void set_new_type<IT_BOOL,IT_FLOAT>(EleImpl& item)
207 {
208  try {
209  item.SetFloatRepr(do_cast<bool, float>(item.GetBoolRepr()));
210  } catch (ItemCastError&) {
211  item.SetFloatRepr(0.0);
212  }
213 }
214 
215 template <> void set_new_type<IT_BOOL,IT_BOOL>(EleImpl& item)
216 {
217  //item.SetBoolRepr(item.GetBoolRepr());
218 }
219 
220 template <> void set_new_type<IT_BOOL,IT_VECTOR>(EleImpl& item)
221 {
222  try {
223  item.SetVecRepr(geom::Vec3(do_cast<bool, Real>(item.GetBoolRepr()), 0.0, 0.0));
224  } catch (ItemCastError&) {
225  item.SetVecRepr(geom::Vec3(0.0, 0.0, 0.0));
226  }
227 }
228 
229 template <> void set_new_type<IT_VECTOR,IT_STRING>(EleImpl& item)
230 {
231  std::ostringstream str;
232  str << item.GetVecRepr();
233  item.SetStringRepr(str.str(),true);
234 }
235 
236 template <> void set_new_type<IT_VECTOR,IT_INT>(EleImpl& item)
237 {
238  try {
239  item.SetIntRepr(do_cast<Real, int>(item.GetVecRepr()[0]));
240  } catch (ItemCastError&) {
241  item.SetIntRepr(0);
242  }
243 }
244 
245 template <> void set_new_type<IT_VECTOR,IT_FLOAT>(EleImpl& item)
246 {
247  item.SetFloatRepr(item.GetVecRepr()[0]);
248 }
249 
250 template <> void set_new_type<IT_VECTOR,IT_BOOL>(EleImpl& item)
251 {
252  if(item.GetVecRepr()[0] == 0.0) {
253  item.SetBoolRepr(false);
254  } else {
255  item.SetBoolRepr(true);
256  }
257 }
258 
259 template <> void set_new_type<IT_VECTOR,IT_VECTOR>(EleImpl& item)
260 {
261  //item.SetVecRepr(item.GetVecRepr());
262 }
263 
264 } // anon ns
265 
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); \
277  }
278 
280 void SetInfoItemNewType(EleImpl& item, int new_type)
281 {
282  Type in_type=item.GetType();
283  if(in_type==IT_STRING) {
285  } else if(in_type==IT_INT) {
287  } else if(in_type==IT_FLOAT) {
289  } else if(in_type==IT_BOOL) {
291  } else if(in_type==IT_VECTOR) {
293  }
294 }
295 
296 void SetInfoItemNewType(EleImpl& item, Type new_type)
297 {
298  Type in_type=item.GetType();
299  if(in_type==IT_STRING) {
301  } else if(in_type==IT_INT) {
303  } else if(in_type==IT_FLOAT) {
305  } else if(in_type==IT_BOOL) {
307  } else if(in_type==IT_VECTOR) {
309  }
310 }
311 
312 }}} // ns
313 
314 #endif