From 8b1851dbdc75cfa091d014f6c96690416e40b2ef Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 24 Feb 2021 13:51:32 +0000 Subject: [PATCH] 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 Reviewed-by: Ryan Harrison --- src/castable.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/castable.h b/src/castable.h index afda1e088f..3599c34cbb 100644 --- a/src/castable.h +++ b/src/castable.h @@ -16,6 +16,7 @@ #define SRC_CASTABLE_H_ #include +#include #include #include @@ -48,7 +49,10 @@ class ClassID { /// Equality operator /// @param rhs the ClassID to compare against /// @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: inline explicit ClassID(uintptr_t v) : id(v) {} @@ -191,4 +195,20 @@ inline TO* As(FROM* obj) { } // 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 { + 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(id.ID()); + } +}; + +} // namespace std + #endif // SRC_CASTABLE_H_