halcheck 1.0
Loading...
Searching...
No Matches
typeinfo.hpp
1#ifndef HALCHECK_LIB_RTTI_HPP
2#define HALCHECK_LIB_RTTI_HPP
3
11#include <halcheck/lib/type_traits.hpp>
12
13#include <cstddef>
14#include <functional>
15#include <type_traits>
16
17#if defined(__clang__)
18#if __has_feature(cxx_rtti)
19#define HALCHECK_RTTI
20#endif
21#elif defined(__GNUG__)
22#if defined(__GXX_RTTI)
23#define HALCHECK_RTTI
24#endif
25#elif defined(_MSC_VER)
26#if defined(_CPPRTTI)
27#define HALCHECK_RTTI
28#endif
29#elif HALCHECK_DOXYGEN
30#define HALCHECK_RTTI
31#endif
32
33#ifdef HALCHECK_RTTI
34#include <string>
35#include <typeinfo> // IWYU pragma: export
36#endif
37
38namespace halcheck { namespace lib {
39
40#ifdef HALCHECK_RTTI
48
55template<typename T>
57 return nameof(typeid(T));
58}
59#endif
60
65struct type_id {
66public:
70 constexpr type_id() : _index(0) {}
71
76 std::size_t hash() const { return std::hash<std::size_t>()(_index); }
77
85 template<typename T, HALCHECK_REQUIRE(!std::is_same<T, void>())>
86 static type_id of() {
87 static const type_id output(next());
88 return output;
89 }
90
98 template<typename T, HALCHECK_REQUIRE(std::is_same<T, void>())>
99 static type_id of() {
100 return {};
101 }
102
103private:
104 explicit constexpr type_id(std::size_t index) : _index(index) {}
105
106 static std::size_t next();
107
108 friend bool operator==(const type_id &lhs, const type_id &rhs) { return lhs._index == rhs._index; }
109 friend bool operator!=(const type_id &lhs, const type_id &rhs) { return lhs._index != rhs._index; }
110 friend bool operator<(const type_id &lhs, const type_id &rhs) { return lhs._index < rhs._index; }
111 friend bool operator>(const type_id &lhs, const type_id &rhs) { return lhs._index > rhs._index; }
112 friend bool operator<=(const type_id &lhs, const type_id &rhs) { return lhs._index <= rhs._index; }
113 friend bool operator>=(const type_id &lhs, const type_id &rhs) { return lhs._index >= rhs._index; }
114
115 std::size_t _index;
116};
117
118}} // namespace halcheck::lib
119
120namespace std {
125template<>
126struct hash<halcheck::lib::type_id> {
127 std::size_t operator()(halcheck::lib::type_id value) const { return value.hash(); }
128};
129} // namespace std
130#endif
std::string nameof()
Gets the demangled name associated with a type.
Definition typeinfo.hpp:56
STL namespace.
T operator()(T... args)
std::size_t hash() const
Gets a hash code associated with this type identifier.
Definition typeinfo.hpp:76
static type_id of()
Gets the unique type identifier associated with the given type.
Definition typeinfo.hpp:86
A runtime type identifier that does not require RTTI.
Definition typeinfo.hpp:65
constexpr type_id()
Constructs the type identifier for void.
Definition typeinfo.hpp:70