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
18#include <unordered_map>
19#include <cstdint>
20
21namespace 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 idenfitifers of
26 // different T types.
27 // IDs are typically used as a map key to value of type T.
28 template <typename T>
29 class SpirvID
30 {
31 public:
32 SpirvID() : id(0) {}
33 SpirvID(uint32_t id) : id(id) {}
34 bool operator == (const SpirvID<T>& rhs) const { return id == rhs.id; }
35 bool operator != (const SpirvID<T>& rhs) const { return id != rhs.id; }
36 bool operator < (const SpirvID<T>& rhs) const { return id < rhs.id; }
37
38 // value returns the numerical value of the identifier.
39 uint32_t value() const { return id; }
40 private:
41 uint32_t id;
42 };
43
44 // HandleMap<T> is an unordered map of SpirvID<T> to T.
45 template <typename T>
46 using HandleMap = std::unordered_map<SpirvID<T>, T>;
47}
48
49namespace std
50{
51 // std::hash implementation for sw::SpirvID<T>
52 template<typename T>
53 struct hash< sw::SpirvID<T> >
54 {
55 std::size_t operator()(const sw::SpirvID<T>& id) const noexcept
56 {
57 return std::hash<uint32_t>()(id.value());
58 }
59 };
60}
61
62#endif // sw_ID_hpp
63