-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtype_id
97 lines (80 loc) · 4.16 KB
/
type_id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_TYPE_TYPE_ID_HPP
#define GTL_TYPE_TYPE_ID_HPP
// Summary: Simple run-time-type-information (rtti) with -fno-rtti.
namespace gtl {
/// @brief The type_id class creates and holds a unique identifier for a type.
class type_id final {
private:
/// @brief To generate the unique types an lambda function is created in a template struct, when the struct is used the template parameter makes the address of the lambda unique.
template <typename type>
struct identifier_generator final {
/// @brief The actual bit of unique type information.
constexpr static const auto identifier = [](){};
};
public:
/// @brief Once generated the type_id class holds the unique identifier as an integer.
const unsigned long long int identifier;
public:
/// @brief Constructor, using the provided type argument the constructor sets the internal ID value using the identifier_generator.
/// @param unused The unnamed argument allows the constructor template argument to be deduced from the provided argument. For constructors template parameters have to be automatically deduced.
template<typename type>
constexpr type_id(type unused)
: identifier(reinterpret_cast<const unsigned long long int>(&identifier_generator<type>::identifier)) {
static_cast<void>(unused);
}
public:
/// @brief Get the value of the identifier.
constexpr unsigned long long int id() const {
return this->identifier;
}
/// @brief Return the value of the identifier when the class is cast to an unsigned long long int.
constexpr operator unsigned long long int() const {
return this->identifier;
}
public:
/// @brief The less than operator is the only one defined using the identifier value, all other operators are defined using this one.
/// @param rhs The right hand side of the comparison.
constexpr bool operator < (const type_id& rhs) const {
return (this->identifier < rhs.identifier);
}
/// @brief The greater than operator defined using the less than operator with the sides of the comparison reversed.
/// @param rhs The right hand side of the comparison.
constexpr bool operator > (const type_id& rhs) const {
return rhs < *this;
}
/// @brief The less than or equal to operator defined using the greater than operator and inverted.
/// @param rhs The right hand side of the comparison.
constexpr bool operator <=(const type_id& rhs) const {
return !(*this > rhs);
}
/// @brief The greater than or equal to operator defined using the less than operator and inverted.
/// @param rhs The right hand side of the comparison.
constexpr bool operator >=(const type_id& rhs) const {
return !(*this < rhs);
}
/// @brief The inequality operator defined using the less than and greater than operators.
/// @param rhs The right hand side of the comparison.
constexpr bool operator !=(const type_id& rhs) const {
return (*this < rhs) || (*this > rhs);
}
/// @brief The equality operator defined using the inequality operator and inverted.
/// @param rhs The right hand side of the comparison.
constexpr bool operator ==(const type_id& rhs) const {
return !(*this != rhs);
}
};
}
#endif // GTL_TYPE_TYPE_ID_HPP