fw4spl
Stringizer.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 __FWTOOLS_STRINGIZER_HPP__
8 #define __FWTOOLS_STRINGIZER_HPP__
9 
10 #include "fwTools/config.hpp"
11 #include "fwTools/TypeInfo.hpp"
12 
13 #include <fwCore/Demangler.hpp>
14 
15 #include <boost/lexical_cast.hpp>
16 #include <boost/mpl/if.hpp>
17 #include <boost/type_traits/is_arithmetic.hpp>
18 
19 #include <string>
20 #include <type_traits>
21 #include <typeinfo>
22 
23 namespace fwTools
24 {
25 
26 namespace
27 {
28 struct NumericCast
29 {
30  //------------------------------------------------------------------------------
31 
32  template<class T>
33  static std::string eval(const T& t)
34  {
35  // note boost::lexical_cast with char is a ASCII-code conversion
36  // instead numerical casting. We provide fix with specialization
37  return ::boost::lexical_cast<std::string>(t);
38  }
39 };
40 
41 struct Default
42 {
43  //------------------------------------------------------------------------------
44 
45  template<class T>
46  static std::string eval(const T& t)
47  {
48  return "No getString for " + ::fwCore::Demangler(typeid(t)).getClassname();
49  }
50 };
51 }
52 
58 // inline mandatory to be supported in multi compilation unit and avoid conflict when linking
59 // function doesn't exist code is rewritted
65 template<class T>
66 inline std::string getString(const T& t)
67 {
68  typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< std::is_arithmetic<T>::value,
69  NumericCast,
70  Default
71  >::type Choice;
72 
73  return Choice::eval(t);
74 }
75 
84 template<class T1, class T2>
85 inline std::string getString(const std::pair<T1, T2>& t)
86 {
87  std::string res = ("[");
88  res += getString(t.first) + "," + getString(t.second) + "]";
89  return res;
90 }
91 
101 template<class ForwardIterator>
102 inline std::string getString(ForwardIterator begin, ForwardIterator end)
103 {
104  std::string result("[");
105  if (begin != end)
106  {
107  result += getString( *begin );
108  while (++begin != end)
109  {
110  result += "," + getString( *begin );
111  }
112  }
113  result += "]";
114  return result;
115 }
116 
118 template<>
119 FWTOOLS_API std::string getString(const std::string& aString);
120 
121 template<>
122 FWTOOLS_API std::string getString(const std::type_info& ti);
123 
124 template<>
125 FWTOOLS_API std::string getString(const TypeInfo& ti);
126 
127 // char are numerically casted
128 template<>
129 FWTOOLS_API std::string getString(const signed char& c);
130 
131 // char are numerically casted // signed char and char doesn't are the same :/ !!!!
132 template<>
133 FWTOOLS_API std::string getString(const char& c);
134 
135 // char are numerically casted
136 template<>
137 FWTOOLS_API std::string getString(const unsigned char& c);
138 
139 template<>
140 FWTOOLS_API std::string getString(const std::string& aString);
141 
143 
144 }
145 
146 #endif // __FWTOOLS_STRINGIZER_HPP__
147 
Purpose: offer a first-class, comparable wrapper over std::type_info ( but copy construcible ) ...
Definition: TypeInfo.hpp:22
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
FWCORE_API std::string getClassname() const
Returns "::some::long::namespace::Object" from "::some::long::namespace::Object". ...
Definition: Demangler.cpp:59
typeid, string or object name demangler.
Definition: Demangler.hpp:23