RSC  0.16.0
TypeStringTools.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010, 2011 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <typeinfo>
30 #include <stdexcept>
31 #include <string>
32 #include <ostream>
33 
34 #include <boost/type_traits.hpp>
35 
36 #include <boost/format.hpp>
37 
38 #include "Demangle.h"
39 #include "ContainerIO.h"
40 #include "rsc/rscexports.h"
41 
42 namespace boost {
43 
44 template<typename T>
45 struct has_stream_output: public false_type {
46 };
47 
48 template<>
49 struct has_stream_output<bool> : public true_type {
50 };
51 
52 template<>
53 struct has_stream_output<char> : public true_type {
54 };
55 
56 template<>
57 struct has_stream_output<unsigned char> : public true_type {
58 };
59 
60 template<>
61 struct has_stream_output<short> : public true_type {
62 };
63 
64 template<>
65 struct has_stream_output<unsigned short> : public true_type {
66 };
67 
68 template<>
69 struct has_stream_output<int> : public true_type {
70 };
71 
72 template<>
73 struct has_stream_output<unsigned int> : public true_type {
74 };
75 
76 template<>
77 struct has_stream_output<long> : public true_type {
78 };
79 
80 template<>
81 struct has_stream_output<unsigned long> : public true_type {
82 };
83 
84 template<>
85 struct has_stream_output<float> : public true_type {
86 };
87 
88 template<>
89 struct has_stream_output<double> : public true_type {
90 };
91 
92 template<>
93 struct has_stream_output<char*> : public true_type {
94 };
95 
96 template<>
97 struct has_stream_output<std::string> : public true_type {
98 };
99 
100 template<typename T>
101 struct has_stream_output<std::vector<T> > : public has_stream_output<T>::type {
102 };
103 
104 template<>
105 struct has_stream_output<std::type_info> : public true_type {
106 };
107 
108 }
109 
110 namespace rsc {
111 namespace runtime {
112 
123 RSC_EXPORT std::string typeName(const std::type_info& type);
124 
134 template<typename T>
135 std::string typeName();
136 
146 template<typename T>
147 std::string typeName(const T& object);
148 
173 template<typename T>
174 std::string typeString(const std::string& known_type_string,
175  const std::string& unknown_type_string, const T& value);
176 
177 }
178 }
179 
180 namespace std {
181 
182 template<typename Ch, typename Tr>
183 basic_ostream<Ch, Tr>&
184 operator<<(basic_ostream<Ch, Tr>& stream, const type_info& type_info_);
185 
186 }
187 
188 // free function implementations
189 
190 namespace rsc {
191 namespace runtime {
192 
193 template<typename T>
194 std::string typeName() {
195  return demangle(typeid(T).name());
196 }
197 
198 template<typename T>
199 std::string typeName(const T& object) {
200  return demangle(typeid(object).name());
201 }
202 
203 template<typename T>
204 std::string doTypeString(const std::string& known_type_string,
205  const std::string&, const T& value, boost::true_type) {
206  return (boost::format(known_type_string) % value).str();
207 }
208 
209 template<typename T>
210 std::string doTypeString(const std::string&,
211  const std::string& unknown_type_string, const T&, boost::false_type) {
212  return unknown_type_string;
213 }
214 
215 template<typename T>
216 std::string typeString(const std::string& known_type_string,
217  const std::string& unknown_type_string, const T& value) {
218  return doTypeString(known_type_string, unknown_type_string, value,
220 }
221 
222 }
223 }
224 
225 namespace std {
226 
227 template<typename Ch, typename Tr>
228 basic_ostream<Ch, Tr>&
229 operator<<(basic_ostream<Ch, Tr>& stream, const type_info& type_info_) {
230  stream << rsc::runtime::typeName(type_info_);
231 
232  return stream;
233 }
234 
235 }
std::string typeName(const T &object)
Returns a (demangled) string representation of the type of object.
std::string typeName(const std::type_info &type)
Returns a (demangled) string representation of type.
STL namespace.
std::string demangle(const char *mangled_symbol)
This function takes the mangled name of a symbol and returns the demangled name of the symbol...
Definition: Demangle.cpp:86
std::string typeString(const std::string &known_type_string, const std::string &unknown_type_string, const T &value)
Returns one of two to strings depending on whether type T is known to be able to support stream outpu...
std::string doTypeString(const std::string &, const std::string &unknown_type_string, const T &, boost::false_type)