-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_name
142 lines (126 loc) · 8.84 KB
/
type_name
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_TYPE_TYPE_NAME_HPP
#define GTL_TYPE_TYPE_NAME_HPP
// Summary: Compile-time type name as a string with -fno-rtti.
namespace gtl {
/// @brief The type_name class provides a name function for a given type.
template<typename type>
class type_name final {
private:
/// @brief Template struct that holds a sequence of integers, derived from http://stackoverflow.com/a/32223343.
template <typename integer_type, integer_type... indexes>
struct integer_sequence {
using sequence_type = integer_sequence;
};
/// @brief Template struct declaration that combines two sequences into one.
template <typename integer_type, typename lhs_sequence, typename rhs_sequence>
struct merge_sequences;
/// @brief Template struct definition and partial specialisation for merging two interger_sequences.
template <typename integer_type, integer_type... lhs_indexes, integer_type... rhs_indexes>
struct merge_sequences<integer_type, integer_sequence<integer_type, lhs_indexes...>, integer_sequence<integer_type, rhs_indexes...>>
: integer_sequence<integer_type, lhs_indexes..., (sizeof...(lhs_indexes) + rhs_indexes)...> {
};
/// @brief Template struct to create an integer_sequence from zero to the given length.
template <typename integer_type, unsigned long long length>
struct make_integer_sequence final
: merge_sequences<integer_type, typename make_integer_sequence<integer_type, length / 2>::sequence_type, typename make_integer_sequence<integer_type, length - length / 2>::sequence_type> {
};
/// @brief Template struct partial specialisation of make_integer_sequence for an integer_sequence of length zero.
template<typename integer_type>
struct make_integer_sequence<integer_type, 0> final
: integer_sequence<integer_type> {
};
/// @brief Template struct partial specialisation of make_integer_sequence for an integer_sequence of length one.
template<typename integer_type>
struct make_integer_sequence<integer_type, 1> final
: integer_sequence<integer_type, 0> {
};
private:
/// @brief The string_literal class holds a compile-time constant string and provides both substring and character finding operations.
template <unsigned long long length>
class string_literal final {
private:
/// @brief The compile-time string is held in a constant sized array.
const char string[length];
public:
/// @brief Compile-time string construction from a string array and an integer_sequence to index it.
/// @param raw_string Input string to wrap.
/// @param index_sequence Template holding the indexes of the string.
template<unsigned long long... indexes>
constexpr string_literal(const char(&raw_string)[sizeof...(indexes)], integer_sequence<unsigned long long, indexes...> index_sequence)
: string{ raw_string[indexes]... } {
static_cast<void>(index_sequence);
}
public:
/// @brief Get a pointer to the string data.
/// @return Pointer to the string data stored in this class.
constexpr const char* c_str() const {
return this->string;
}
public:
/// @brief Create a substring from this string by providing a template start position, length, and integer_sequence of indexes.
/// @param substring_index_sequence Template holding the indexes of the new string.
/// @return A string_literal holding the requested substring.
template<unsigned long long substring_start, unsigned long long substring_length, unsigned long long... substring_indexes>
constexpr string_literal<substring_length + 1> substr(integer_sequence<unsigned long long, substring_indexes...> substring_index_sequence) const {
static_cast<void>(substring_index_sequence);
return string_literal<substring_length + 1>({ this->string[substring_start + substring_indexes]..., '\0' }, make_integer_sequence<unsigned long long, substring_length + 1>{});
}
public:
/// @brief Search for a given character within this string.
/// @param character The character to find.
/// @param starting_index The index of the string to start searching from.
/// @return The index of the character if found, otherwise 0xFFFFFFFFFFFFFFFF.
constexpr unsigned long long find(const char character, const unsigned long long starting_index = 0) const {
return ((starting_index >= length) ? 0xFFFFFFFFFFFFFFFF : ((this->string[starting_index] == character) ? starting_index : this->find(character, starting_index + 1)));
}
/// @brief Search for a given character within this string in reverse order starting from the end.
/// @param character The character to find.
/// @param starting_index The index of the string to start searching from.
/// @return The index of the character if found, otherwise 0xFFFFFFFFFFFFFFFF.
constexpr unsigned long long rfind(const char character, const unsigned long long starting_index = length - 1) const {
return ((starting_index == 0) ? ((this->string[starting_index] == character) ? starting_index : 0xFFFFFFFFFFFFFFFF) : ((this->string[starting_index] == character) ? starting_index : this->rfind(character, starting_index - 1)));
}
};
public:
/// @brief Get the name of the classes template type as a string.
/// @return Pointer to the string representation of the classes template type.
static const char* name() {
// Clang compiler format: __PRETTY_FUNCTION__ = static const char *gtl::type_name<TYPE_NAME>::name() [template_type = TYPE_NAME]
// GNU compiler format: __PRETTY_FUNCTION__ = static const char *gtl::type_name<template_type>::name() [with template_type = TYPE_NAME]
// MSVC compiler format: __FUNCSIG__ = const char *__cdecl gtl::type_name<TYPE_NAME>::name(void)
# if defined(__PRETTY_FUNCTION__) || defined(__GNUC__) || defined(__clang__)
constexpr static const unsigned long long function_name_length = sizeof(__PRETTY_FUNCTION__);
constexpr static const string_literal<function_name_length> function_name(__PRETTY_FUNCTION__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.rfind('=') + 2;
constexpr static const unsigned long long type_name_end = function_name_length - 2;
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
# elif defined(__FUNCSIG__) || defined(_MSC_VER)
constexpr static const unsigned long long function_name_length = sizeof(__FUNCSIG__);
constexpr static const string_literal<function_name_length> function_name(__FUNCSIG__, make_integer_sequence<unsigned long long, function_name_length>{});
constexpr static const unsigned long long type_name_start = function_name.find('<') + 1;
constexpr static const unsigned long long type_name_end = function_name.rfind('>');
constexpr static const unsigned long long type_name_length = type_name_end - type_name_start;
constexpr static const string_literal<type_name_length + 1> type_name_string = function_name.template substr<type_name_start, type_name_length>(make_integer_sequence<unsigned long long, type_name_length>{});
return type_name_string.c_str();
# else
# error "Unsupported compiler."
# endif
}
};
}
#endif // GTL_TYPE_TYPE_NAME_HPP