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 | |
Nicolas Capens | ff9bb52 | 2021-11-08 21:29:29 -0500 | [diff] [blame] | 18 | #include "System/Debug.hpp" |
| 19 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 20 | #include <cstdint> |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 21 | #include <unordered_map> |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 22 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 23 | namespace sw { |
| 24 | |
| 25 | // SpirvID is a strongly-typed identifier backed by a uint32_t. |
| 26 | // The template parameter T is not actually used by the implementation of |
| 27 | // ID; instead it is used to prevent implicit casts between identifiers of |
| 28 | // different T types. |
| 29 | // IDs are typically used as a map key to value of type T. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 30 | template<typename T> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 31 | class SpirvID |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 32 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 33 | public: |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 34 | SpirvID() = default; |
| 35 | inline SpirvID(uint32_t id); |
Nicolas Capens | ff9bb52 | 2021-11-08 21:29:29 -0500 | [diff] [blame] | 36 | |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 37 | inline bool operator==(const SpirvID<T> &rhs) const; |
| 38 | inline bool operator!=(const SpirvID<T> &rhs) const; |
| 39 | inline bool operator<(const SpirvID<T> &rhs) const; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 40 | |
Nicolas Capens | ff9bb52 | 2021-11-08 21:29:29 -0500 | [diff] [blame] | 41 | // Returns the numerical value of the identifier. |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 42 | inline uint32_t value() const; |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 43 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 44 | private: |
Nicolas Capens | ff9bb52 | 2021-11-08 21:29:29 -0500 | [diff] [blame] | 45 | uint32_t id = 0; // Valid ids are in the range "0 < id < Bound". |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 46 | }; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 47 | |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 48 | template<typename T> |
| 49 | SpirvID<T>::SpirvID(uint32_t id) |
| 50 | : id(id) |
| 51 | {} |
| 52 | |
| 53 | template<typename T> |
| 54 | bool SpirvID<T>::operator==(const SpirvID<T> &rhs) const |
| 55 | { |
| 56 | return id == rhs.id; |
| 57 | } |
| 58 | |
| 59 | template<typename T> |
| 60 | bool SpirvID<T>::operator!=(const SpirvID<T> &rhs) const |
| 61 | { |
| 62 | return id != rhs.id; |
| 63 | } |
| 64 | |
| 65 | template<typename T> |
| 66 | bool SpirvID<T>::operator<(const SpirvID<T> &rhs) const |
| 67 | { |
| 68 | return id < rhs.id; |
| 69 | } |
| 70 | |
| 71 | template<typename T> |
| 72 | uint32_t SpirvID<T>::value() const |
| 73 | { |
Nicolas Capens | ff9bb52 | 2021-11-08 21:29:29 -0500 | [diff] [blame] | 74 | // Assert that we don't attempt to use unassigned IDs. |
| 75 | // Use if(id != 0) { ... } to avoid invalid code paths. |
| 76 | ASSERT(id != 0); |
| 77 | |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 78 | return id; |
| 79 | } |
| 80 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 81 | // HandleMap<T> is an unordered map of SpirvID<T> to T. |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 82 | template<typename T> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 83 | using HandleMap = std::unordered_map<SpirvID<T>, T>; |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 84 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 85 | } // namespace sw |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 86 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 87 | namespace std { |
Ben Clayton | 19b43a6 | 2019-12-10 11:57:00 +0000 | [diff] [blame] | 88 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 89 | // std::hash implementation for sw::SpirvID<T> |
| 90 | template<typename T> |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 91 | struct hash<sw::SpirvID<T> > |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 92 | { |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 93 | std::size_t operator()(const sw::SpirvID<T> &id) const noexcept |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 94 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 95 | return std::hash<uint32_t>()(id.value()); |
| 96 | } |
| 97 | }; |
| 98 | |
Ben Clayton | bc1c067be | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 99 | } // namespace std |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 100 | |
| 101 | #endif // sw_ID_hpp |