-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_name.h
63 lines (48 loc) · 1.52 KB
/
type_name.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef _TYPE_NAME_H_
#define _TYPE_NAME_H_
#include <array>
namespace detail {
template <size_t ... Ns> struct range {};
template <size_t ... Ns> struct range_gen;
template <size_t B, size_t E, size_t ... Ns>
struct range_gen<B, E, Ns...> {
using type = typename range_gen<B, E - 1, E - 1, Ns...>::type;
};
template <size_t B, size_t ... Ns>
struct range_gen<B, B, Ns...> {
using type = range<Ns...>;
};
template <size_t B, size_t E>
using range_t = typename range_gen<B, E + 1>::type;
template <size_t N>
using sequence_t = typename range_gen<0, N>::type;
template <typename C, std::size_t N, std::size_t...Is>
constexpr std::array<C, sizeof...(Is) + 1> substr(const C(&s)[N], range<Is...>)
{
return {((Is < N) ? s[Is] : 0)..., 0};
}
template <std::size_t L, typename C, std::size_t N>
constexpr std::array<C, L + 1> truncate(const C(&s)[N])
{
return substr(s, 0, L);
}
template <std::size_t B, std::size_t E, typename C, std::size_t N>
constexpr std::array<C, E - B + 2> substr(const C(&s)[N])
{
return substr(s, range_t<B, E>{});
}
}
template <typename T>
constexpr auto get_type_name ()
{
#if defined(__GNUC__)
constexpr decltype(__PRETTY_FUNCTION__) func_name = __PRETTY_FUNCTION__;
return detail::substr<41, std::size(func_name) - 3>(func_name);
#elif defined(_MSC_VER)
constexpr decltype(__FUNCSIG__) func_name = __FUNCSIG__;
return detail::substr<27, std::size(func_name) - 9>(func_name);
#else
static_assert(false, "Unsupported compiler");
#endif
}
#endif