Loading...
Searching...
No Matches
typestring.h
Go to the documentation of this file.
1#ifndef _BBM_TYPE_NAME_H_
2#define _BBM_TYPE_NAME_H_
3
4#include <string_view>
5
6#if !defined(__clang__) || !defined(__GNUC__) || !defined(__MSC_VER)
7#include <typeinfo>
8#endif
9
10/***********************************************************************/
11/*! \file typestring.h
12 \brief produce stringview of type name of a type. Avoids using typeid
13 for GCC, MSVC, and CLANG. For other compilers it will fall back to typeid.name()
14************************************************************************/
15
16namespace bbm {
17
18 namespace detail {
19 template <typename T>
20 inline constexpr std::string_view typestring_impl(void)
21 {
22#if defined(__clang__)
23 constexpr auto prefix = std::string_view{"[T = "};
24 constexpr auto suffix = std::string_view{"]"};
25 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
26#elif defined(__GNUC__)
27 constexpr auto prefix = std::string_view{"with T = "};
28 constexpr auto suffix = std::string_view{"; "};
29 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
30#elif defined(__MSC_VER)
31 constexpr auto prefix = std::string_view{"get_type_name<"};
32 constexpr auto suffix = std::string_view{">(void)"};
33 constexpr auto function = std::string_view{__FUNCSIG__};
34#else
35 constexpr auto prefix = std::string_view{""};
36 constexpr auto suffix = std::string_view{""};
37 auto function = std::string_view( typeid(T{}).name() );
38 return function;
39#endif
40
41 const auto start = function.find(prefix) + prefix.size();
42 const auto end = function.find(suffix);
43 const auto size = end - start;
44
45 return function.substr(start, size);
46 }
47
48 } // end detail namespace
49
50 template<typename T>
51 static constexpr std::string_view typestring = bbm::detail::typestring_impl<T>();
52
53 #define toTypestring(...) bbm::typestring<decltype(__VA_ARGS__)>
54
55}
56
57#endif /* _BBM_TYPE_NAME_H_ */
Definition: aggregatebsdf.h:29
static constexpr std::string_view typestring
Definition: typestring.h:51
auto end(T &&t)
Definition: iterator_util.h:43
size_t size(T &&t)
Definition: iterator_util.h:22