// Copyright 2019 The SwiftShader Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef sw_ID_hpp #define sw_ID_hpp #include #include namespace sw { // SpirvID is a strongly-typed identifier backed by a uint32_t. // The template parameter T is not actually used by the implementation of // ID; instead it is used to prevent implicit casts between identifiers of // different T types. // IDs are typically used as a map key to value of type T. template class SpirvID { public: SpirvID() = default; inline SpirvID(uint32_t id); inline bool operator==(const SpirvID &rhs) const; inline bool operator!=(const SpirvID &rhs) const; inline bool operator<(const SpirvID &rhs) const; // value returns the numerical value of the identifier. inline uint32_t value() const; private: uint32_t id = 0; }; template SpirvID::SpirvID(uint32_t id) : id(id) {} template bool SpirvID::operator==(const SpirvID &rhs) const { return id == rhs.id; } template bool SpirvID::operator!=(const SpirvID &rhs) const { return id != rhs.id; } template bool SpirvID::operator<(const SpirvID &rhs) const { return id < rhs.id; } template uint32_t SpirvID::value() const { return id; } // HandleMap is an unordered map of SpirvID to T. template using HandleMap = std::unordered_map, T>; } // namespace sw namespace std { // std::hash implementation for sw::SpirvID template struct hash > { std::size_t operator()(const sw::SpirvID &id) const noexcept { return std::hash()(id.value()); } }; } // namespace std #endif // sw_ID_hpp