EnTT 3.13.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 std::string_view pretty_function{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
70template<typename Type, typename = void>
71struct 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>
92struct 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 {
103#endif
104 }
105
107 [[nodiscard]] constexpr operator id_type() const noexcept {
108 return value();
109 }
110};
111
116template<typename Type, typename = void>
117struct 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
133struct type_info final {
138 template<typename Type>
139 constexpr type_info(std::in_place_type_t<Type>) noexcept
142 alias{type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()} {}
143
148 [[nodiscard]] constexpr id_type index() const noexcept {
149 return seq;
150 }
151
156 [[nodiscard]] constexpr id_type hash() const noexcept {
157 return identifier;
158 }
159
164 [[nodiscard]] constexpr std::string_view name() const noexcept {
165 return alias;
166 }
167
168private:
169 id_type seq;
170 id_type identifier;
171 std::string_view alias;
172};
173
180[[nodiscard]] inline constexpr bool operator==(const type_info &lhs, const type_info &rhs) noexcept {
181 return lhs.hash() == rhs.hash();
182}
183
190[[nodiscard]] inline constexpr bool operator!=(const type_info &lhs, const type_info &rhs) noexcept {
191 return !(lhs == rhs);
192}
193
200[[nodiscard]] constexpr bool operator<(const type_info &lhs, const type_info &rhs) noexcept {
201 return lhs.index() < rhs.index();
202}
203
211[[nodiscard]] constexpr bool operator<=(const type_info &lhs, const type_info &rhs) noexcept {
212 return !(rhs < lhs);
213}
214
222[[nodiscard]] constexpr bool operator>(const type_info &lhs, const type_info &rhs) noexcept {
223 return rhs < lhs;
224}
225
233[[nodiscard]] constexpr bool operator>=(const type_info &lhs, const type_info &rhs) noexcept {
234 return !(lhs < rhs);
235}
236
248template<typename Type>
249[[nodiscard]] const type_info &type_id() noexcept {
250 if constexpr(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>) {
251 static type_info instance{std::in_place_type<Type>};
252 return instance;
253 } else {
254 return type_id<std::remove_cv_t<std::remove_reference_t<Type>>>();
255 }
256}
257
259template<typename Type>
260[[nodiscard]] const type_info &type_id(Type &&) noexcept {
261 return type_id<std::remove_cv_t<std::remove_reference_t<Type>>>();
262}
263
264} // namespace entt
265
266#endif
EnTT default namespace.
Definition dense_map.hpp:21
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:13
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Type hash.
Definition type_info.hpp:92
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Type sequential identifier.
Definition type_info.hpp:71
static id_type value() noexcept
Returns the sequential identifier of a given type.
Definition type_info.hpp:76
Implementation specific information about a type.
constexpr id_type index() const noexcept
Type index.
constexpr std::string_view name() const noexcept
Type name.
constexpr id_type hash() const noexcept
Type hash.
constexpr type_info(std::in_place_type_t< Type >) noexcept
Constructs a type info object for a given type.
static constexpr std::string_view value() noexcept
Returns the name of a given type.