EnTT 4.0.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 <compare>
5#include "../config/config.h"
6#include "../stl/string_view.hpp"
7#include "../stl/type_traits.hpp"
8#include "../stl/utility.hpp"
9#include "fwd.hpp"
10#include "hashed_string.hpp"
11
12namespace entt {
13
15namespace internal {
16
17struct ENTT_API type_index final {
18 [[nodiscard]] static id_type next() noexcept {
19 static ENTT_MAYBE_ATOMIC(id_type) value{};
20 return value++;
21 }
22};
23
24template<typename Type>
25[[nodiscard]] constexpr const char *pretty_function() noexcept {
26#if defined ENTT_PRETTY_FUNCTION
27 return static_cast<const char *>(ENTT_PRETTY_FUNCTION);
28#else
29 return "";
30#endif
31}
32
33template<typename Type>
34[[nodiscard]] constexpr auto stripped_type_name() noexcept {
35#if defined ENTT_PRETTY_FUNCTION
36 const stl::string_view full_name{pretty_function<Type>()};
37 auto first = full_name.find_first_not_of(' ', full_name.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX) + 1);
38 auto value = full_name.substr(first, full_name.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
39 return value;
40#else
41 return stl::string_view{};
42#endif
43}
44
45template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
46[[nodiscard]] ENTT_CONSTEVAL stl::string_view type_name(int) noexcept {
47 constexpr auto value = stripped_type_name<Type>();
48 return value;
49}
50
51template<typename Type>
52[[nodiscard]] stl::string_view type_name(char) noexcept {
53 static const auto value = stripped_type_name<Type>();
54 return value;
55}
56
57template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
58[[nodiscard]] ENTT_CONSTEVAL id_type type_hash(int) noexcept {
59 constexpr auto stripped = stripped_type_name<Type>();
60 constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
61 return value;
62}
63
64template<typename Type>
65[[nodiscard]] id_type type_hash(char) noexcept {
66 static const auto value = [](const auto stripped) {
67 return hashed_string::value(stripped.data(), stripped.size());
68 }(stripped_type_name<Type>());
69 return value;
70}
71
72} // namespace internal
74
79template<typename Type>
80struct ENTT_API type_index final {
85 [[nodiscard]] static id_type value() noexcept {
86 static const id_type value = internal::type_index::next();
87 return value;
88 }
89
91 [[nodiscard]] constexpr operator id_type() const noexcept {
92 return value();
93 }
94};
95
100template<typename Type>
101struct type_hash final {
106#if defined ENTT_PRETTY_FUNCTION
107 [[nodiscard]] static constexpr id_type value() noexcept {
108 return internal::type_hash<Type>(0);
109#else
110 [[nodiscard]] static constexpr id_type value() noexcept {
112#endif
113 }
114
116 [[nodiscard]] constexpr operator id_type() const noexcept {
117 return value();
118 }
119};
120
125template<typename Type>
126struct type_name final {
131 [[nodiscard]] static constexpr stl::string_view value() noexcept {
132 return internal::type_name<Type>(0);
133 }
134
136 [[nodiscard]] constexpr operator stl::string_view() const noexcept {
137 return value();
138 }
139};
140
142struct type_info final {
147 template<typename Type>
148 // NOLINTBEGIN(modernize-use-transparent-functors)
149 constexpr type_info(stl::in_place_type_t<Type>) noexcept
151 identifier{type_hash<stl::remove_cvref_t<Type>>::value()},
152 alias{type_name<stl::remove_cvref_t<Type>>::value()} {}
153 // NOLINTEND(modernize-use-transparent-functors)
154
159 [[nodiscard]] constexpr id_type index() const noexcept {
160 return seq;
161 }
162
167 [[nodiscard]] constexpr id_type hash() const noexcept {
168 return identifier;
169 }
170
175 [[nodiscard]] constexpr stl::string_view name() const noexcept {
176 return alias;
177 }
178
184 [[nodiscard]] constexpr bool operator==(const type_info &other) const noexcept {
185 return identifier == other.identifier;
186 }
187
193 [[nodiscard]] constexpr auto operator<=>(const type_info &other) const noexcept {
194 return seq <=> other.seq;
195 }
196
197private:
198 id_type seq;
199 id_type identifier;
200 stl::string_view alias;
201};
202
214template<typename Type>
215[[nodiscard]] const type_info &type_id() noexcept {
216 if constexpr(stl::is_same_v<Type, stl::remove_cvref_t<Type>>) {
217 static const type_info instance{stl::in_place_type<Type>};
218 return instance;
219 } else {
221 }
222}
223
225template<typename Type>
226[[nodiscard]] const type_info &type_id(const Type &) noexcept {
228}
229
230} // namespace entt
231
232#endif
EnTT default namespace.
Definition dense_map.hpp:25
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
stl::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Type sequential identifier.
Definition type_info.hpp:80
static id_type value() noexcept
Returns the sequential identifier of a given type.
Definition type_info.hpp:85
Implementation specific information about a type.
constexpr auto operator<=>(const type_info &other) const noexcept
Lexicographically compares two type info objects.
constexpr type_info(stl::in_place_type_t< Type >) noexcept
Constructs a type info object for a given type.
constexpr stl::string_view name() const noexcept
Type name.
constexpr id_type index() const noexcept
Type index.
constexpr id_type hash() const noexcept
Type hash.
constexpr bool operator==(const type_info &other) const noexcept
Compares two type info objects.
static constexpr stl::string_view value() noexcept
Returns the name of a given type.