ClassID: Support use as unordered map / set keys

Change-Id: Ia6c21dfc33445ba828b2f244d8a3479fb3328805
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/42263
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
Ben Clayton 2021-02-24 13:51:32 +00:00 committed by Commit Bot service account
parent ce9229cd2e
commit 8b1851dbdc
1 changed files with 21 additions and 1 deletions

View File

@ -16,6 +16,7 @@
#define SRC_CASTABLE_H_ #define SRC_CASTABLE_H_
#include <cstdint> #include <cstdint>
#include <functional>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -48,7 +49,10 @@ class ClassID {
/// Equality operator /// Equality operator
/// @param rhs the ClassID to compare against /// @param rhs the ClassID to compare against
/// @returns true if this ClassID is equal to `rhs` /// @returns true if this ClassID is equal to `rhs`
inline bool operator==(ClassID& rhs) const { return id == rhs.id; } inline bool operator==(const ClassID& rhs) const { return id == rhs.id; }
/// @return the unique numerical identifier of this ClassID
inline uintptr_t ID() const { return id; }
private: private:
inline explicit ClassID(uintptr_t v) : id(v) {} inline explicit ClassID(uintptr_t v) : id(v) {}
@ -191,4 +195,20 @@ inline TO* As(FROM* obj) {
} // namespace tint } // namespace tint
namespace std {
/// Custom std::hash specialization for tint::ClassID so ClassID can be used as
/// keys for std::unordered_map and std::unordered_set.
template <>
class hash<tint::ClassID> {
public:
/// @param id the ClassID to hash
/// @return the ClassID's internal numerical identifier
inline std::size_t operator()(const tint::ClassID& id) const {
return static_cast<std::size_t>(id.ID());
}
};
} // namespace std
#endif // SRC_CASTABLE_H_ #endif // SRC_CASTABLE_H_