NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
icommon.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_INTERFACE_ICOMMON_HPP__
2#define NEFORCE_CORE_INTERFACE_ICOMMON_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
20
29template <typename T>
30struct ihashable {
31private:
32 constexpr const T& derived() const noexcept { return static_cast<const T&>(*this); }
33
34public:
41 NEFORCE_NODISCARD constexpr size_t to_hash() const noexcept(noexcept(derived().to_hash())) {
42 return derived().to_hash();
43 }
44};
45 // CRTPInterfaces
47
52
57template <typename T>
58struct hash<T, enable_if_t<is_base_of<ihashable<T>, T>::value>> {
59 NEFORCE_NODISCARD constexpr size_t operator()(const T& obj) const noexcept(noexcept(obj.to_hash())) {
60 return obj.to_hash();
61 }
62};
63 // HashPrimary
65
70
79template <typename T>
81private:
82 constexpr const T& derived() const noexcept { return static_cast<const T&>(*this); }
83
84public:
92 NEFORCE_NODISCARD constexpr bool operator==(const T& rhs) const noexcept(noexcept(derived() == rhs)) {
93 return derived() == rhs;
94 }
95
103 NEFORCE_NODISCARD constexpr bool operator!=(const T& rhs) const noexcept(noexcept(!(*this == rhs))) {
104 return !(*this == rhs);
105 }
106
114 NEFORCE_NODISCARD constexpr bool operator<(const T& rhs) const noexcept(noexcept(derived() < rhs)) {
115 return derived() < rhs;
116 }
117
125 NEFORCE_NODISCARD constexpr bool operator>(const T& rhs) const noexcept(noexcept(rhs < derived())) {
126 return rhs < derived();
127 }
128
136 NEFORCE_NODISCARD constexpr bool operator<=(const T& rhs) const noexcept(noexcept(!(derived() > rhs))) {
137 return !(derived() > rhs);
138 }
139
147 NEFORCE_NODISCARD constexpr bool operator>=(const T& rhs) const noexcept(noexcept(!(derived() < rhs))) {
148 return !(derived() < rhs);
149 }
150};
151
152
162template <typename T>
163struct icommon : icomparable<T>, ihashable<T> {};
164 // CRTPInterfaces
166
167NEFORCE_END_NAMESPACE__
168#endif // NEFORCE_CORE_INTERFACE_ICOMMON_HPP__
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
哈希函数库
哈希函数的主模板
通用接口,同时具备可比较和可哈希功能
可比较对象接口模板
NEFORCE_NODISCARD constexpr bool operator<(const T &rhs) const noexcept(noexcept(derived()< rhs))
小于比较运算符
NEFORCE_NODISCARD constexpr bool operator>=(const T &rhs) const noexcept(noexcept(!(derived()< rhs)))
大于等于比较运算符
NEFORCE_NODISCARD constexpr bool operator>(const T &rhs) const noexcept(noexcept(rhs< derived()))
大于比较运算符
NEFORCE_NODISCARD constexpr bool operator==(const T &rhs) const noexcept(noexcept(derived()==rhs))
相等比较运算符
NEFORCE_NODISCARD constexpr bool operator!=(const T &rhs) const noexcept(noexcept(!(*this==rhs)))
不等比较运算符
NEFORCE_NODISCARD constexpr bool operator<=(const T &rhs) const noexcept(noexcept(!(derived() > rhs)))
小于等于比较运算符
可哈希对象接口模板
NEFORCE_NODISCARD constexpr size_t to_hash() const noexcept(noexcept(derived().to_hash()))
获取对象的哈希值
判断Base是否是Derived的基类