-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeName.hpp
116 lines (104 loc) · 2.52 KB
/
TypeName.hpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#if !defined(TYPENAME_HPP)
#define TYPENAME_HPP
#include "Common.hpp"
#include "String.hpp"
namespace mp
{
namespace detail
{
template <int N>
constexpr auto parseInt()
{
if constexpr (N < 0)
return CharSeq<'-'>{} + parseInt<-N>();
else if constexpr (N < 10)
return CharSeq<N + '0'>{};
else
return parseInt<N / 10>() + CharSeq<N % 10 + '0'>{};
}
using namespace mp::literals;
template <typename T, typename = void> //for SFINAE
struct TypeNameImpl
{
constexpr static auto name = "undefined"_str;
};
template <>
struct TypeNameImpl<int>
{
constexpr static auto name = "int"_str;
};
template <>
struct TypeNameImpl<char>
{
constexpr static auto name = "char"_str;
};
template <>
struct TypeNameImpl<double>
{
constexpr static auto name = "double"_str;
};
template <>
struct TypeNameImpl<float>
{
constexpr static auto name = "float"_str;
};
template <>
struct TypeNameImpl<void>
{
constexpr static auto name = "void"_str;
};
template <typename T>
struct TypeNameImpl<const T, typename std::enable_if<!std::is_array<T>::value>::type>
{
constexpr static auto name = "const "_str + TypeNameImpl<T>::name;
};
template <typename T>
struct TypeNameImpl<T *>
{
constexpr static auto name = "pointer to "_str + TypeNameImpl<T>::name;
};
template <typename T>
struct TypeNameImpl<T &>
{
constexpr static auto name = "l-reference of "_str + TypeNameImpl<T>::name;
};
template <typename T>
struct TypeNameImpl<T &&>
{
constexpr static auto name = "r-reference of "_str + TypeNameImpl<T>::name;
};
template <typename T, int N>
struct TypeNameImpl<T[N]>
{
constexpr static auto name = "array["_str + parseInt<N>() +
"] of "_str + TypeNameImpl<T>::name;
};
template <typename...>
struct TypeNames;
template <typename First, typename... Rests>
struct TypeNames<First, Rests...>
{
constexpr static auto name = TypeNameImpl<First>::name + ", "_str + TypeNames<Rests...>::name;
};
template <typename First>
struct TypeNames<First>
{
constexpr static auto name = TypeNameImpl<First>::name;
};
template <>
struct TypeNames<>
{
constexpr static auto name = ""_str;
};
template <typename Ret, typename... Args>
struct TypeNameImpl<Ret(Args...)>
{
// C++17 folding expression
constexpr static auto name = "function ("_str + TypeNames<Args...>::name +
") return "_str + TypeNameImpl<Ret>::name;
};
} // namespace detail
template <typename T>
using TypeName = detail::TypeNameImpl<T>;
} // namespace mp
#endif // TYPENAME_HPP