Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1 | // Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef sw_ID_hpp |
| 16 | #define sw_ID_hpp |
| 17 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 18 | #include <cstdint> |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 19 | #include <unordered_map> |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 20 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 21 | namespace sw { |
| 22 | |
| 23 | // SpirvID is a strongly-typed identifier backed by a uint32_t. |
| 24 | // The template parameter T is not actually used by the implementation of |
| 25 | // ID; instead it is used to prevent implicit casts between identifiers of |
| 26 | // different T types. |
| 27 | // IDs are typically used as a map key to value of type T. |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 28 | template<typename T> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 29 | class SpirvID |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 30 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 31 | public: |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 32 | SpirvID() = default; |
| 33 | inline SpirvID(uint32_t id); |
| 34 | inline bool operator==(const SpirvID<T> &rhs) const; |
| 35 | inline bool operator!=(const SpirvID<T> &rhs) const; |
| 36 | inline bool operator<(const SpirvID<T> &rhs) const; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 37 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 38 | // value returns the numerical value of the identifier. |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 39 | inline uint32_t value() const; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 40 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 41 | private: |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 42 | uint32_t id = 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | }; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 44 | |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 45 | template<typename T> |
| 46 | SpirvID<T>::SpirvID(uint32_t id) |
| 47 | : id(id) |
| 48 | {} |
| 49 | |
| 50 | template<typename T> |
| 51 | bool SpirvID<T>::operator==(const SpirvID<T> &rhs) const |
| 52 | { |
| 53 | return id == rhs.id; |
| 54 | } |
| 55 | |
| 56 | template<typename T> |
| 57 | bool SpirvID<T>::operator!=(const SpirvID<T> &rhs) const |
| 58 | { |
| 59 | return id != rhs.id; |
| 60 | } |
| 61 | |
| 62 | template<typename T> |
| 63 | bool SpirvID<T>::operator<(const SpirvID<T> &rhs) const |
| 64 | { |
| 65 | return id < rhs.id; |
| 66 | } |
| 67 | |
| 68 | template<typename T> |
| 69 | uint32_t SpirvID<T>::value() const |
| 70 | { |
| 71 | return id; |
| 72 | } |
| 73 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 74 | // HandleMap<T> is an unordered map of SpirvID<T> to T. |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 75 | template<typename T> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 76 | using HandleMap = std::unordered_map<SpirvID<T>, T>; |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 77 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 78 | } // namespace sw |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 79 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 80 | namespace std { |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 81 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 82 | // std::hash implementation for sw::SpirvID<T> |
| 83 | template<typename T> |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 84 | struct hash<sw::SpirvID<T> > |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 85 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 86 | std::size_t operator()(const sw::SpirvID<T> &id) const noexcept |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 87 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 88 | return std::hash<uint32_t>()(id.value()); |
| 89 | } |
| 90 | }; |
| 91 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 92 | } // namespace std |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 93 | |
| 94 | #endif // sw_ID_hpp |