• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Dawn Authors
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 DAWNNATIVE_OBJECT_CONTENT_HASHER_H_
16 #define DAWNNATIVE_OBJECT_CONTENT_HASHER_H_
17 
18 #include "common/HashUtils.h"
19 
20 #include <string>
21 #include <vector>
22 
23 namespace dawn_native {
24 
25     // ObjectContentHasher records a hash that can be used as a key to lookup a cached object in a
26     // cache.
27     class ObjectContentHasher {
28       public:
29         // Record calls the appropriate record function based on the type.
30         template <typename T, typename... Args>
Record(const T & value,const Args &...args)31         void Record(const T& value, const Args&... args) {
32             RecordImpl<T, Args...>::Call(this, value, args...);
33         }
34 
35         size_t GetContentHash() const;
36 
37       private:
38         template <typename T, typename... Args>
39         struct RecordImpl {
CallRecordImpl40             static constexpr void Call(ObjectContentHasher* recorder,
41                                        const T& value,
42                                        const Args&... args) {
43                 HashCombine(&recorder->mContentHash, value, args...);
44             }
45         };
46 
47         template <typename T>
48         struct RecordImpl<T*> {
49             static constexpr void Call(ObjectContentHasher* recorder, T* obj) {
50                 // Calling Record(objPtr) is not allowed. This check exists to only prevent such
51                 // mistakes.
52                 static_assert(obj == nullptr, "");
53             }
54         };
55 
56         template <typename T>
57         struct RecordImpl<std::vector<T>> {
58             static constexpr void Call(ObjectContentHasher* recorder, const std::vector<T>& vec) {
59                 recorder->RecordIterable<std::vector<T>>(vec);
60             }
61         };
62 
63         template <typename IteratorT>
64         constexpr void RecordIterable(const IteratorT& iterable) {
65             for (auto it = iterable.begin(); it != iterable.end(); ++it) {
66                 Record(*it);
67             }
68         }
69 
70         size_t mContentHash = 0;
71     };
72 
73     template <>
74     struct ObjectContentHasher::RecordImpl<std::string> {
75         static constexpr void Call(ObjectContentHasher* recorder, const std::string& str) {
76             recorder->RecordIterable<std::string>(str);
77         }
78     };
79 
80 }  // namespace dawn_native
81 
82 #endif  // DAWNNATIVE_OBJECT_CONTENT_HASHER_H_
83