fw4spl
fwCamp/include/fwCamp/Mapper/ValueMapper.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWCAMP_MAPPER_VALUEMAPPER_HPP__
8 #define __FWCAMP_MAPPER_VALUEMAPPER_HPP__
9 
10 #include <boost/date_time/posix_time/posix_time.hpp>
11 #include <boost/filesystem/path.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <boost/logic/tribool.hpp>
14 
15 #include <camp/camptype.hpp>
16 #include <camp/type.hpp>
17 #include <camp/valuemapper.hpp>
18 
19 namespace camp_ext
20 {
21 
22 template <>
23 struct ValueMapper< std::uint64_t>
24 {
25  // The corresponding CAMP type is "string"
26  static const int type = camp::intType;
27 
28  // Convert from MyStringClass to std::string
29  static std::string to(const std::uint64_t& source)
30  {
31  return boost::lexical_cast<std::string>(source);
32  }
33 
34  // Convert from any type to MyStringClass
35  // Be smart, just reuse ValueMapper<std::string> :)
36  template <typename T>
37  static std::uint64_t from(const T& source)
38  {
39  return boost::lexical_cast< std::uint64_t>(source);
40  }
41 };
42 
43 //-----------------------------------------------------------------------------
44 
45 template<typename T>
47 {
48  //------------------------------------------------------------------------------
49 
50  static ::boost::logic::tribool get(const T& source)
51  {
52  return ::boost::logic::tribool();
53  }
54 };
55 
56 //-----------------------------------------------------------------------------
57 
58 template<>
59 struct wrapperTribbol<std::string >
60 {
61  //------------------------------------------------------------------------------
62 
63  static ::boost::logic::tribool get(const std::string& source)
64  {
65  ::boost::logic::tribool value;
66 
67  if (source.compare("true") == 0)
68  {
69  value = true;
70  }
71  else if (source.compare("false") == 0)
72  {
73  value = false;
74  }
75  else
76  {
77  value = boost::logic::indeterminate;
78  }
79 
80  return value;
81  }
82 };
83 
84 //-----------------------------------------------------------------------------
85 
86 template <>
87 struct ValueMapper< ::boost::logic::tribool>
88 {
89  // The corresponding CAMP type is "string"
90  static const int type = camp::stringType;
91 
92  // Convert from MyStringClass to std::string
93  static std::string to(const ::boost::logic::tribool& source)
94  {
95  std::string value;
96 
97  if (source)
98  {
99  value = "true";
100  }
101  else if (!source)
102  {
103  value = "false";
104  }
105  else
106  {
107  value = "indeterminate";
108  }
109  return value;
110  }
111  //------------------------------------------------------------------------------
112 
113  template <typename T>
114  static ::boost::logic::tribool from(const T& source)
115  {
116  return wrapperTribbol<T>::get(source);
117  }
118 };
119 
120 //-----------------------------------------------------------------------------
121 
122 template <>
123 struct ValueMapper< ::boost::posix_time::ptime >
124 {
125  typedef boost::posix_time::ptime ReturnType;
126  static const int type = camp::stringType;
127  //------------------------------------------------------------------------------
128 
129  static const std::string to(const ReturnType& source)
130  {
131  return boost::posix_time::to_simple_string(source);
132  }
133 
134  //------------------------------------------------------------------------------
135 
136  static ReturnType from(bool source)
137  {
138  CAMP_ERROR(camp::BadType(camp::boolType, camp::mapType<ReturnType>()));
139  }
140  //------------------------------------------------------------------------------
141 
142  static ReturnType from(long source)
143  {
144  CAMP_ERROR(camp::BadType(camp::intType, camp::mapType<ReturnType>()));
145  }
146  //------------------------------------------------------------------------------
147 
148  static ReturnType from(double source)
149  {
150  CAMP_ERROR(camp::BadType(camp::realType, camp::mapType<ReturnType>()));
151  }
152  //------------------------------------------------------------------------------
153 
154  static ReturnType from(const camp::EnumObject& source)
155  {
156  CAMP_ERROR(camp::BadType(camp::enumType, camp::mapType<ReturnType>()));
157  }
158  //------------------------------------------------------------------------------
159 
160  static ReturnType from(const camp::UserObject& source)
161  {
162  CAMP_ERROR(camp::BadType(camp::userType, camp::mapType<ReturnType>()));
163  }
164  //------------------------------------------------------------------------------
165 
166  static ReturnType from(const std::string& source)
167  {
168  return boost::posix_time::time_from_string(source);
169  }
170 };
171 
172 //-----------------------------------------------------------------------------
173 
174 template <>
175 struct ValueMapper< ::boost::filesystem::path >
176 {
177  typedef ::boost::filesystem::path ReturnType;
178  static const int type = camp::stringType;
179  //------------------------------------------------------------------------------
180 
181  static const std::string to(const ReturnType& source)
182  {
183  return source.string();
184  }
185 
186  //------------------------------------------------------------------------------
187 
188  static ReturnType from(bool source)
189  {
190  CAMP_ERROR(camp::BadType(camp::boolType, camp::mapType<ReturnType>()));
191  }
192  //------------------------------------------------------------------------------
193 
194  static ReturnType from(long source)
195  {
196  CAMP_ERROR(camp::BadType(camp::intType, camp::mapType<ReturnType>()));
197  }
198  //------------------------------------------------------------------------------
199 
200  static ReturnType from(double source)
201  {
202  CAMP_ERROR(camp::BadType(camp::realType, camp::mapType<ReturnType>()));
203  }
204  //------------------------------------------------------------------------------
205 
206  static ReturnType from(const camp::EnumObject& source)
207  {
208  CAMP_ERROR(camp::BadType(camp::enumType, camp::mapType<ReturnType>()));
209  }
210  //------------------------------------------------------------------------------
211 
212  static ReturnType from(const camp::UserObject& source)
213  {
214  CAMP_ERROR(camp::BadType(camp::userType, camp::mapType<ReturnType>()));
215  }
216  //------------------------------------------------------------------------------
217 
218  static ReturnType from(const std::string& source)
219  {
220  return ::boost::filesystem::path(source);
221  }
222 };
223 
224 //-----------------------------------------------------------------------------
225 
226 /*
227  * Specialization of ValueMapper for unsigned char
228  */
229 template <>
230 struct ValueMapper<unsigned char>
231 {
232  static const int type = camp::stringType;
233  //------------------------------------------------------------------------------
234 
235  static std::string to(unsigned char source)
236  {
237  unsigned int intValue = boost::numeric_cast<unsigned int>(source);
238 
239  return boost::lexical_cast<std::string>(intValue);
240  }
241 
242  //------------------------------------------------------------------------------
243 
244  static unsigned char from(bool source)
245  {
246  return static_cast<unsigned char>(source);
247  }
248  //------------------------------------------------------------------------------
249 
250  static unsigned char from(long source)
251  {
252  return static_cast<unsigned char>(source);
253  }
254  //------------------------------------------------------------------------------
255 
256  static unsigned char from(double source)
257  {
258  return static_cast<unsigned char>(source);
259  }
260  //------------------------------------------------------------------------------
261 
262  static unsigned char from(const std::string& source)
263  {
264  unsigned int intValue = boost::lexical_cast<unsigned int>(source);
265  return boost::numeric_cast<unsigned char>(intValue);
266  }
267  //------------------------------------------------------------------------------
268 
269  static unsigned char from(const camp::EnumObject& source)
270  {
271  return static_cast<unsigned char>(source.value());
272  }
273  //------------------------------------------------------------------------------
274 
275  static unsigned char from(const camp::UserObject&)
276  {
277  CAMP_ERROR(camp::BadType(camp::userType, camp::realType));
278  }
279 };
280 
281 //-----------------------------------------------------------------------------
282 
283 template <typename T>
284 struct ValueMapper<std::shared_ptr<T> >
285 {
286  typedef std::shared_ptr<T> ReturnType;
287  static const int type = camp::userType;
288  //------------------------------------------------------------------------------
289 
290  static const camp::UserObject to(const ReturnType& source)
291  {
292  return camp::UserObject(source);
293  }
294 
295  //------------------------------------------------------------------------------
296 
297  static ReturnType from(bool source)
298  {
299  CAMP_ERROR(camp::BadType(camp::boolType, camp::mapType<ReturnType>()));
300  }
301  //------------------------------------------------------------------------------
302 
303  static ReturnType from(long source)
304  {
305  CAMP_ERROR(camp::BadType(camp::intType, camp::mapType<ReturnType>()));
306  }
307  //------------------------------------------------------------------------------
308 
309  static ReturnType from(double source)
310  {
311  CAMP_ERROR(camp::BadType(camp::realType, camp::mapType<ReturnType>()));
312  }
313  //------------------------------------------------------------------------------
314 
315  static ReturnType from(const std::string& source)
316  {
317  CAMP_ERROR(camp::BadType(camp::stringType, camp::mapType<ReturnType>()));
318  }
319  //------------------------------------------------------------------------------
320 
321  static ReturnType from(const camp::EnumObject& source)
322  {
323  CAMP_ERROR(camp::BadType(camp::enumType, camp::mapType<ReturnType>()));
324  }
325  //------------------------------------------------------------------------------
326 
327  static ReturnType from(const camp::UserObject& source)
328  {
329  ReturnType result;
330  try
331  {
332  T* ptr = source.get< T* >();
333  result = T::dynamicCast(ptr->getSptr());
334  }
335  catch(...)
336  {
337  std::shared_ptr<T> tmp;
338  return tmp;
339  }
340  return result;
341  }
342 };
343 } // namespace camp_ext
344 
345 #endif /* __FWCAMP_MAPPER_VALUEMAPPER_HPP__ */
346 
STL namespace.