EnTT 3.14.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{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
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>
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>
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
138 template<typename Type>
139 // NOLINTBEGIN(modernize-use-transparent-functors)
140 constexpr type_info(std::in_place_type_t<Type>) noexcept
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]] inline constexpr bool operator==(const type_info &lhs, const type_info &rhs) noexcept {
183 return lhs.hash() == rhs.hash();
184}
185
192[[nodiscard]] inline constexpr bool operator!=(const type_info &lhs, const type_info &rhs) noexcept {
193 return !(lhs == rhs);
194}
195
202[[nodiscard]] inline constexpr bool operator<(const type_info &lhs, const type_info &rhs) noexcept {
203 return lhs.index() < rhs.index();
204}
205
213[[nodiscard]] inline constexpr bool operator<=(const type_info &lhs, const type_info &rhs) noexcept {
214 return !(rhs < lhs);
215}
216
224[[nodiscard]] inline constexpr bool operator>(const type_info &lhs, const type_info &rhs) noexcept {
225 return rhs < lhs;
226}
227
235[[nodiscard]] inline constexpr bool operator>=(const type_info &lhs, const type_info &rhs) noexcept {
236 return !(lhs < rhs);
237}
238
250template<typename Type>
252 if constexpr(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>) {
253 static type_info instance{std::in_place_type<Type>};
254 return instance;
255 } else {
257 }
258}
259
261template<typename Type>
265
266} // namespace entt
267
268#endif
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:14
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.