blob: 67ca59d52a63736840f97b6ef933895abe868e15 [file] [log] [blame]
Ben Claytonab51bbf2019-02-20 14:36:27 +00001// 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 Claytonab51bbf2019-02-20 14:36:27 +000018#include <cstdint>
Ben Claytonbc1c0672019-12-17 20:37:37 +000019#include <unordered_map>
Ben Claytonab51bbf2019-02-20 14:36:27 +000020
Nicolas Capens157ba262019-12-10 17:49:14 -050021namespace 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 Claytonbc1c0672019-12-17 20:37:37 +000028template<typename T>
Nicolas Capens157ba262019-12-10 17:49:14 -050029class SpirvID
Ben Claytonab51bbf2019-02-20 14:36:27 +000030{
Nicolas Capens157ba262019-12-10 17:49:14 -050031public:
Ben Clayton19b43a62019-12-10 11:57:00 +000032 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 Claytonab51bbf2019-02-20 14:36:27 +000037
Nicolas Capens157ba262019-12-10 17:49:14 -050038 // value returns the numerical value of the identifier.
Ben Clayton19b43a62019-12-10 11:57:00 +000039 inline uint32_t value() const;
Ben Claytonbc1c0672019-12-17 20:37:37 +000040
Nicolas Capens157ba262019-12-10 17:49:14 -050041private:
Ben Clayton19b43a62019-12-10 11:57:00 +000042 uint32_t id = 0;
Nicolas Capens157ba262019-12-10 17:49:14 -050043};
Ben Claytonab51bbf2019-02-20 14:36:27 +000044
Ben Clayton19b43a62019-12-10 11:57:00 +000045template<typename T>
46SpirvID<T>::SpirvID(uint32_t id)
47 : id(id)
48{}
49
50template<typename T>
51bool SpirvID<T>::operator==(const SpirvID<T> &rhs) const
52{
53 return id == rhs.id;
54}
55
56template<typename T>
57bool SpirvID<T>::operator!=(const SpirvID<T> &rhs) const
58{
59 return id != rhs.id;
60}
61
62template<typename T>
63bool SpirvID<T>::operator<(const SpirvID<T> &rhs) const
64{
65 return id < rhs.id;
66}
67
68template<typename T>
69uint32_t SpirvID<T>::value() const
70{
71 return id;
72}
73
Nicolas Capens157ba262019-12-10 17:49:14 -050074// HandleMap<T> is an unordered map of SpirvID<T> to T.
Ben Claytonbc1c0672019-12-17 20:37:37 +000075template<typename T>
Nicolas Capens157ba262019-12-10 17:49:14 -050076using HandleMap = std::unordered_map<SpirvID<T>, T>;
Ben Clayton19b43a62019-12-10 11:57:00 +000077
Ben Claytonbc1c0672019-12-17 20:37:37 +000078} // namespace sw
Ben Claytonab51bbf2019-02-20 14:36:27 +000079
Ben Claytonbc1c0672019-12-17 20:37:37 +000080namespace std {
Ben Clayton19b43a62019-12-10 11:57:00 +000081
Nicolas Capens157ba262019-12-10 17:49:14 -050082// std::hash implementation for sw::SpirvID<T>
83template<typename T>
Ben Claytonbc1c0672019-12-17 20:37:37 +000084struct hash<sw::SpirvID<T> >
Nicolas Capens157ba262019-12-10 17:49:14 -050085{
Ben Claytonbc1c0672019-12-17 20:37:37 +000086 std::size_t operator()(const sw::SpirvID<T> &id) const noexcept
Ben Claytonab51bbf2019-02-20 14:36:27 +000087 {
Nicolas Capens157ba262019-12-10 17:49:14 -050088 return std::hash<uint32_t>()(id.value());
89 }
90};
91
Ben Claytonbc1c0672019-12-17 20:37:37 +000092} // namespace std
Ben Claytonab51bbf2019-02-20 14:36:27 +000093
94#endif // sw_ID_hpp