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

EnTT 3.15.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 "../core/attribute.h"
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 auto stripped_type_name() noexcept {
26#if defined ENTT_PRETTY_FUNCTION
27 const std::string_view pretty_function{static_cast<const char *>(ENTT_PRETTY_FUNCTION)};
28 auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX) + 1);
29 auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
30 return value;
31#else
32 return std::string_view{""};
33#endif
34}
35
36template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
37[[nodiscard]] constexpr std::string_view type_name(int) noexcept {
38 constexpr auto value = stripped_type_name<Type>();
39 return value;
40}
41
42template<typename Type>
43[[nodiscard]] std::string_view type_name(char) noexcept {
44 static const auto value = stripped_type_name<Type>();
45 return value;
46}
47
48template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
49[[nodiscard]] constexpr id_type type_hash(int) noexcept {
50 constexpr auto stripped = stripped_type_name<Type>();
51 constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
52 return value;
53}
54
55template<typename Type>
56[[nodiscard]] id_type type_hash(char) noexcept {
57 static const auto value = [](const auto stripped) {
58 return hashed_string::value(stripped.data(), stripped.size());
59 }(stripped_type_name<Type>());
60 return value;
61}
62
63} // namespace internal
65
70template<typename Type, typename = void>
71 struct ENTT_API type_index final {
76 [[nodiscard]] static id_type value() noexcept {
77 static const id_type value = internal::type_index::next();
78 return value;
79 }
80
82 [[nodiscard]] constexpr operator id_type() const noexcept {
83 return value();
84 }
85};
86
91template<typename Type, typename = void>
92 struct type_hash final {
97#if defined ENTT_PRETTY_FUNCTION
98 [[nodiscard]] static constexpr id_type value() noexcept {
99 return internal::type_hash<Type>(0);
100#else
101 [[nodiscard]] static constexpr id_type value() noexcept {
102 return type_index<Type>::value();
103#endif
104 }
105
107 [[nodiscard]] constexpr operator id_type() const noexcept {
108 return value();
109 }
110};
111
116template<typename Type, typename = void>
117 struct type_name final {
122 [[nodiscard]] static constexpr std::string_view value() noexcept {
123 return internal::type_name<Type>(0);
124 }
125
127 [[nodiscard]] constexpr operator std::string_view() const noexcept {
128 return value();
129 }
130};
131
133 struct type_info final {
138 template<typename Type>
139 // NOLINTBEGIN(modernize-use-transparent-functors)
140 constexpr type_info(std::in_place_type_t<Type>) noexcept
141 : seq{type_index<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
142 identifier{type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
143 alias{type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()} {}
144 // NOLINTEND(modernize-use-transparent-functors)
145
150 [[nodiscard]] constexpr id_type index() const noexcept {
151 return seq;
152 }
153
158 [[nodiscard]] constexpr id_type hash() const noexcept {
159 return identifier;
160 }
161
166 [[nodiscard]] constexpr std::string_view name() const noexcept {
167 return alias;
168 }
169
170private:
171 id_type seq;
172 id_type identifier;
173 std::string_view alias;
174};
175
182[[nodiscard]] constexpr bool operator==(const type_info &lhs, const type_info &rhs) noexcept {
183 return lhs.hash() == rhs.hash();
184}
185
192[[nodiscard]] constexpr bool operator!=(const type_info &lhs, const type_info &rhs) noexcept {
193 return !(lhs == rhs);
194}
195
202[[nodiscard]] constexpr bool operator<(const type_info &lhs, const type_info &rhs) noexcept {
203 return lhs.index() < rhs.index();
204}
205
213[[nodiscard]] constexpr bool operator<=(const type_info &lhs, const type_info &rhs) noexcept {
214 return !(rhs < lhs);
215}
216
224[[nodiscard]] constexpr bool operator>(const type_info &lhs, const type_info &rhs) noexcept {
225 return rhs < lhs;
226}
227
235[[nodiscard]] constexpr bool operator>=(const type_info &lhs, const type_info &rhs) noexcept {
236 return !(lhs < rhs);
237}
238
250template<typename Type>
251[[nodiscard]] const type_info &type_id() noexcept {
252 if constexpr(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>) {
253 static const type_info instance{std::in_place_type<Type>};
254 return instance;
255 } else {
256 return type_id<std::remove_cv_t<std::remove_reference_t<Type>>>();
257 }
258}
259
261template<typename Type>
262// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
263[[nodiscard]] const type_info &type_id(Type &&) noexcept {
264 return type_id<std::remove_cv_t<std::remove_reference_t<Type>>>();
265}
266
267} // namespace entt
268
269#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:257
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:244
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:232
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:283
entt::type_id
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
Definition type_info.hpp:251
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:270
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:220
entt::type_hash
Type hash.
Definition type_info.hpp:92
entt::type_hash::value
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Definition type_info.hpp:101
entt::type_index
Type sequential identifier.
Definition type_info.hpp:71
entt::type_index::value
static id_type value() noexcept
Returns the sequential identifier of a given type.
Definition type_info.hpp:76
entt::type_info
Implementation specific information about a type.
Definition type_info.hpp:133
entt::type_info::index
constexpr id_type index() const noexcept
Type index.
Definition type_info.hpp:150
entt::type_info::name
constexpr std::string_view name() const noexcept
Type name.
Definition type_info.hpp:166
entt::type_info::hash
constexpr id_type hash() const noexcept
Type hash.
Definition type_info.hpp:158
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:140
entt::type_name
Type name.
Definition type_info.hpp:117
entt::type_name::value
static constexpr std::string_view value() noexcept
Returns the name of a given type.
Definition type_info.hpp:122