EnTT  3.6.0
type_info.hpp
1 #ifndef ENTT_CORE_TYPE_INFO_HPP
2 #define ENTT_CORE_TYPE_INFO_HPP
3 
4 
5 #include <string_view>
6 #include <type_traits>
7 #include "../config/config.h"
8 #include "../core/attribute.h"
9 #include "hashed_string.hpp"
10 #include "fwd.hpp"
11 
12 
13 namespace entt {
14 
15 
22 namespace internal {
23 
24 
25 struct ENTT_API type_seq final {
26  [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
27  static ENTT_MAYBE_ATOMIC(id_type) value{};
28  return value++;
29  }
30 };
31 
32 
33 template<typename Type>
34 [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
35 #if defined ENTT_PRETTY_FUNCTION
36  std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
37  auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
38  auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
39  return value;
40 #else
41  return std::string_view{};
42 #endif
43 }
44 
45 
46 template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
47 [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
48  constexpr auto value = stripped_type_name<Type>();
49  return value;
50 }
51 
52 
53 template<typename Type>
54 [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
55  static const auto value = stripped_type_name<Type>();
56  return value;
57 }
58 
59 
60 template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
61 [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
62  constexpr auto stripped = stripped_type_name<Type>();
63  constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
64  return value;
65 }
66 
67 
68 template<typename Type>
69 [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
70  static const auto value = [](const auto stripped) {
71  return hashed_string::value(stripped.data(), stripped.size());
72  }(stripped_type_name<Type>());
73  return value;
74 }
75 
76 
77 }
78 
79 
90 template<typename Type, typename = void>
91 struct ENTT_API type_seq final {
96  [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
97  static const id_type value = internal::type_seq::next();
98  return value;
99  }
100 };
101 
102 
107 template<typename Type, typename = void>
108 struct type_hash final {
113 #if defined ENTT_PRETTY_FUNCTION
114  [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
115  return internal::type_hash<Type>(0);
116 #else
117  [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
118  return type_seq<Type>::value();
119 #endif
120  }
121 };
122 
123 
128 template<typename Type, typename = void>
129 struct type_name final {
134  [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
135  return internal::type_name<Type>(0);
136  }
137 };
138 
139 
141 class type_info final {
142  template<typename>
143  friend type_info type_id() ENTT_NOEXCEPT;
144 
145  type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
146  : seq_value{seq_v},
147  hash_value{hash_v},
148  name_value{name_v}
149  {}
150 
151 public:
153  type_info() ENTT_NOEXCEPT
154  : type_info({}, {}, {})
155  {}
156 
158  type_info(const type_info &) ENTT_NOEXCEPT = default;
160  type_info(type_info &&) ENTT_NOEXCEPT = default;
161 
166  type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
167 
172  type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
173 
178  [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
179  return !name_value.empty();
180  }
181 
186  [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
187  return seq_value;
188  }
189 
194  [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
195  return hash_value;
196  }
197 
202  [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
203  return name_value;
204  }
205 
211  [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
212  return hash_value == other.hash_value;
213  }
214 
215 private:
216  id_type seq_value;
217  id_type hash_value;
218  std::string_view name_value;
219 };
220 
221 
228 [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
229  return !(lhs == rhs);
230 }
231 
232 
238 template<typename Type>
239 type_info type_id() ENTT_NOEXCEPT {
240  return type_info{
244  };
245 }
246 
247 
248 }
249 
250 
251 #endif
entt::type_seq::value
static id_type value() noexcept
Returns the sequential identifier of a given type.
Definition: type_info.hpp:96
entt::type_seq
Type sequential identifier.
Definition: type_info.hpp:91
entt::type_info::hash
id_type hash() const noexcept
Type hash.
Definition: type_info.hpp:194
entt::type_hash
Type hash.
Definition: type_info.hpp:108
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition: fwd.hpp:12
entt::type_info::operator==
bool operator==(const type_info &other) const noexcept
Compares the contents of two type info objects.
Definition: type_info.hpp:211
entt::type_info::type_info
type_info(const type_info &) noexcept=default
Default copy constructor.
entt::type_id
type_info type_id() noexcept
Returns the type info object for a given type.
Definition: type_info.hpp:239
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::type_hash::value
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.
Definition: type_info.hpp:117
entt::basic_hashed_string::value
constexpr hash_type value() const noexcept
Returns the numeric representation of a hashed string.
Definition: hashed_string.hpp:176
entt::type_info::type_info
type_info(type_info &&) noexcept=default
Default move constructor.
entt::type_info::seq
id_type seq() const noexcept
Type sequential identifier.
Definition: type_info.hpp:186
entt::type_info::type_info
type_info() noexcept
Default constructor.
Definition: type_info.hpp:153
entt::type_name
Type name.
Definition: type_info.hpp:129
entt::type_name::value
static constexpr std::string_view value() noexcept
Returns the name of a given type.
Definition: type_info.hpp:134
entt::type_info
Implementation specific information about a type.
Definition: type_info.hpp:141
entt::type_info::name
std::string_view name() const noexcept
Type name.
Definition: type_info.hpp:202
entt::operator!=
bool operator!=(const any &lhs, const any &rhs) noexcept
Checks if two wrappers differ in their content.
Definition: any.hpp:313