EnTT: src/entt/core/type_info.hpp Source File

EnTT 3.16.0

Loading...
Searching...
No Matches
type_info.hpp
1#ifndef ENTT_CORE_TYPE_INFO_HPP
2#define ENTT_CORE_TYPE_INFO_HPP
3
4#include <string_view>
5#include <type_traits>
6#include <utility>
7#include "../config/config.h"
8#include "fwd.hpp"
9#include "hashed_string.hpp"
10
11namespace entt {
12
14namespace internal {
15
16struct ENTT_API type_index final {
17 [[nodiscard]] static id_type next() noexcept {
18 static ENTT_MAYBE_ATOMIC(id_type) value{};
19 return value++;
20 }
21};
22
23template<typename Type>
24[[nodiscard]] constexpr const char *pretty_function() noexcept {
25#if defined ENTT_PRETTY_FUNCTION
26 return static_cast<const char *>(ENTT_PRETTY_FUNCTION);
27#else
28 return "";
29#endif
30}
31
32template<typename Type>
33[[nodiscard]] constexpr auto stripped_type_name() noexcept {
34#if defined ENTT_PRETTY_FUNCTION
35 const std::string_view full_name{pretty_function<Type>()};
36 auto first = full_name.find_first_not_of(' ', full_name.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX) + 1);
37 auto value = full_name.substr(first, full_name.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
38 return value;
39#else
40 return std::string_view{};
41#endif
42}
43
44template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
45[[nodiscard]] constexpr std::string_view type_name(int) noexcept {
46 constexpr auto value = stripped_type_name<Type>();
47 return value;
48}
49
50template<typename Type>
51[[nodiscard]] std::string_view type_name(char) noexcept {
52 static const auto value = stripped_type_name<Type>();
53 return value;
54}
55
56template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
57[[nodiscard]] constexpr id_type type_hash(int) noexcept {
58 constexpr auto stripped = stripped_type_name<Type>();
59 constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
60 return value;
61}
62
63template<typename Type>
64[[nodiscard]] id_type type_hash(char) noexcept {
65 static const auto value = [](const auto stripped) {
66 return hashed_string::value(stripped.data(), stripped.size());
67 }(stripped_type_name<Type>());
68 return value;
69}
70
71} // namespace internal
73
78template<typename Type, typename = void>
79 struct ENTT_API type_index final {
84 [[nodiscard]] static id_type value() noexcept {
85 static const id_type value = internal::type_index::next();
86 return value;
87 }
88
90 [[nodiscard]] constexpr operator id_type() const noexcept {
91 return value();
92 }
93};
94
99template<typename Type, typename = void>
100 struct type_hash final {
105#if defined ENTT_PRETTY_FUNCTION
106 [[nodiscard]] static constexpr id_type value() noexcept {
107 return internal::type_hash<Type>(0);
108#else
109 [[nodiscard]] static constexpr id_type value() noexcept {
110 return type_index<Type>::value();
111#endif
112 }
113
115 [[nodiscard]] constexpr operator id_type() const noexcept {
116 return value();
117 }
118};
119
124template<typename Type, typename = void>
125 struct type_name final {
130 [[nodiscard]] static constexpr std::string_view value() noexcept {
131 return internal::type_name<Type>(0);
132 }
133
135 [[nodiscard]] constexpr operator std::string_view() const noexcept {
136 return value();
137 }
138};
139
141 struct type_info final {
146 template<typename Type>
147 // NOLINTBEGIN(modernize-use-transparent-functors)
148 constexpr type_info(std::in_place_type_t<Type>) noexcept
149 : seq{type_index<std::remove_const_t<std::remove_reference_t<Type>>>::value()},
150 identifier{type_hash<std::remove_const_t<std::remove_reference_t<Type>>>::value()},
151 alias{type_name<std::remove_const_t<std::remove_reference_t<Type>>>::value()} {}
152 // NOLINTEND(modernize-use-transparent-functors)
153
158 [[nodiscard]] constexpr id_type index() const noexcept {
159 return seq;
160 }
161
166 [[nodiscard]] constexpr id_type hash() const noexcept {
167 return identifier;
168 }
169
174 [[nodiscard]] constexpr std::string_view name() const noexcept {
175 return alias;
176 }
177
178private:
179 id_type seq;
180 id_type identifier;
181 std::string_view alias;
182};
183
190[[nodiscard]] constexpr bool operator==(const type_info &lhs, const type_info &rhs) noexcept {
191 return lhs.hash() == rhs.hash();
192}
193
200[[nodiscard]] constexpr bool operator!=(const type_info &lhs, const type_info &rhs) noexcept {
201 return !(lhs == rhs);
202}
203
210[[nodiscard]] constexpr bool operator<(const type_info &lhs, const type_info &rhs) noexcept {
211 return lhs.index() < rhs.index();
212}
213
221[[nodiscard]] constexpr bool operator<=(const type_info &lhs, const type_info &rhs) noexcept {
222 return !(rhs < lhs);
223}
224
232[[nodiscard]] constexpr bool operator>(const type_info &lhs, const type_info &rhs) noexcept {
233 return rhs < lhs;
234}
235
243[[nodiscard]] constexpr bool operator>=(const type_info &lhs, const type_info &rhs) noexcept {
244 return !(lhs < rhs);
245}
246
258template<typename Type>
259[[nodiscard]] const type_info &type_id() noexcept {
260 if constexpr(std::is_same_v<Type, std::remove_const_t<std::remove_reference_t<Type>>>) {
261 static const type_info instance{std::in_place_type<Type>};
262 return instance;
263 } else {
264 return type_id<std::remove_const_t<std::remove_reference_t<Type>>>();
265 }
266}
267
269template<typename Type>
270// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
271[[nodiscard]] const type_info &type_id(Type &&) noexcept {
272 return type_id<std::remove_const_t<std::remove_reference_t<Type>>>();
273}
274
275} // namespace entt
276
277#endif
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
entt::operator<=
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:263
entt::operator<
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:250
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:238
entt::operator>=
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:289
entt::type_id
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
Definition type_info.hpp:259
entt::operator>
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:276
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:226
entt::type_hash
Type hash.
Definition type_info.hpp:100
entt::type_hash::value
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Definition type_info.hpp:109
entt::type_index
Type sequential identifier.
Definition type_info.hpp:79
entt::type_index::value
static id_type value() noexcept
Returns the sequential identifier of a given type.
Definition type_info.hpp:84
entt::type_info
Implementation specific information about a type.
Definition type_info.hpp:141
entt::type_info::index
constexpr id_type index() const noexcept
Type index.
Definition type_info.hpp:158
entt::type_info::name
constexpr std::string_view name() const noexcept
Type name.
Definition type_info.hpp:174
entt::type_info::hash
constexpr id_type hash() const noexcept
Type hash.
Definition type_info.hpp:166
entt::type_info::type_info
constexpr type_info(std::in_place_type_t< Type >) noexcept
Constructs a type info object for a given type.
Definition type_info.hpp:148
entt::type_name
Type name.
Definition type_info.hpp:125
entt::type_name::value
static constexpr std::string_view value() noexcept
Returns the name of a given type.
Definition type_info.hpp:130