type_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 #include <cstddef>
12 #include <string>
13 #include <type_traits>
14 
15 #include "type_utils/literal_sz.h"
17 
18 namespace cras
19 {
20 
26 ::std::string cleanTypeName(const ::std::string& typeName);
27 
33 ::std::string demangle(const ::std::string& mangled);
34 
40 template <typename T>
41 inline ::std::string getTypeName()
42 {
43 #if defined(__clang__)
44  const ::std::string prefix = "[T = ";
45  const ::std::string suffix = "]";
46  const ::std::string function = __PRETTY_FUNCTION__;
47 #elif defined(__GNUC__)
48  const ::std::string prefix = "with T = ";
49  const ::std::string suffix = "; ";
50  const ::std::string function = __PRETTY_FUNCTION__;
51 #elif defined(__MSC_VER)
52  const ::std::string prefix = "get_type_name<";
53  const ::std::string suffix = ">(void)";
54  const ::std::string function = __FUNCSIG__;
55 #else
56  const ::std::string prefix = "";
57  const ::std::string suffix = "";
58  const ::std::string function = ::cras::demangle(typeid(T).name());
59 #endif
60 
61  const auto start = function.find(prefix) + prefix.size();
62  const auto end = function.find(suffix);
63  const auto size = end - start;
64 
65  return ::cras::cleanTypeName(function.substr(start, size));
66 }
67 
73 ::std::string getTypeName(const ::std::type_info& typeInfo);
74 
75 }
cras
Definition: any.hpp:15
literal_sz.h
Support for literal suffix _sz creating size_t values.
cras::demangle
::std::string demangle(const ::std::string &mangled)
Demangle the given mangle C++ type identifier.
cras::cleanTypeName
::std::string cleanTypeName(const ::std::string &typeName)
Remove not-so-nice parts of demangled C++ type names.
cras::getTypeName
inline ::std::string getTypeName()
Get a human-readable name of T.
Definition: type_utils.hpp:41
start
ROSCPP_DECL void start()
string_traits.hpp
Useful C++ string traits.


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:14