Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: mojo/public/cpp/bindings/lib/wtf_clone_equals_util.h

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Remove left-over import Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
7 7
8 #include <type_traits> 8 #include <type_traits>
9 9
10 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h" 10 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h"
11 #include "mojo/public/cpp/bindings/struct_ptr.h"
12 #include "third_party/WebKit/Source/wtf/HashFunctions.h"
11 #include "third_party/WebKit/Source/wtf/HashMap.h" 13 #include "third_party/WebKit/Source/wtf/HashMap.h"
12 #include "third_party/WebKit/Source/wtf/Optional.h" 14 #include "third_party/WebKit/Source/wtf/Optional.h"
13 #include "third_party/WebKit/Source/wtf/Vector.h" 15 #include "third_party/WebKit/Source/wtf/Vector.h"
16 #include "third_party/WebKit/Source/wtf/text/StringHash.h"
14 #include "third_party/WebKit/Source/wtf/text/WTFString.h" 17 #include "third_party/WebKit/Source/wtf/text/WTFString.h"
15 18
16 namespace mojo { 19 namespace mojo {
17 namespace internal { 20 namespace internal {
18 21
19 template <typename T> 22 template <typename T>
20 struct CloneTraits<WTF::Vector<T>, false> { 23 struct CloneTraits<WTF::Vector<T>, false> {
21 static WTF::Vector<T> Clone(const WTF::Vector<T>& input) { 24 static WTF::Vector<T> Clone(const WTF::Vector<T>& input) {
22 WTF::Vector<T> result; 25 WTF::Vector<T> result;
23 result.reserveCapacity(input.size()); 26 result.reserveCapacity(input.size());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 66
64 for (auto iter = a.begin(); iter != a_end; ++iter) { 67 for (auto iter = a.begin(); iter != a_end; ++iter) {
65 auto b_iter = b.find(iter->key); 68 auto b_iter = b.find(iter->key);
66 if (b_iter == b_end || !internal::Equals(iter->value, b_iter->value)) 69 if (b_iter == b_end || !internal::Equals(iter->value, b_iter->value))
67 return false; 70 return false;
68 } 71 }
69 return true; 72 return true;
70 } 73 }
71 }; 74 };
72 75
76 template <typename T>
77 struct HashTraits<WTF::Vector<T>, false> {
Sam McNally 2016/09/21 04:57:58 Ditto for these two.
tibell 2016/09/21 07:10:53 Done.
78 static size_t Hash(size_t seed, const WTF::Vector<T>& value) {
79 for (size_t i = 0; i < value.size(); ++i) {
80 seed = Hash(seed, value[i]);
81 }
82 return seed;
83 }
84 };
85
86 template <typename K, typename V>
87 struct HashTraits<WTF::HashMap<K, V>, false> {
88 static size_t Hash(size_t seed, const WTF::HashMap<K, V>& value) {
89 for (auto iter = value.begin(); iter != value.end(); ++iter) {
90 seed = Hash(seed, iter->key);
91 seed = Hash(seed, iter->value);
92 }
93 return seed;
94 }
95 };
96
97 template <>
98 struct HashTraits<WTF::String, false> {
99 static size_t Hash(size_t seed, const WTF::String& value) {
100 return HashCombine(seed, WTF::StringHash::hash(value));
101 }
102 };
103
104 template <typename T>
105 struct StructPtrHashFn {
106 static unsigned hash(const mojo::StructPtr<T>& value) {
yzshen1 2016/09/20 23:44:39 nit: namespace is not needed (here and changes bel
tibell 2016/09/21 07:10:53 Done. These now all uses hashing/equality on the
107 return mojo::internal::Hash(mojo::internal::kHashSeed, *value.get());
yzshen1 2016/09/20 23:44:39 *value.get(): Is it possible to dereference null p
tibell 2016/09/21 07:10:53 Done.
108 }
109 static bool equal(const mojo::StructPtr<T>& left,
110 const mojo::StructPtr<T>& right) {
111 return left->Equals(*right);
yzshen1 2016/09/20 23:44:39 ditto
tibell 2016/09/21 07:10:53 Done.
112 }
113 static const bool safeToCompareToEmptyOrDeleted = false;
114 };
115
116 template <typename T>
117 struct InlinedStructPtrHashFn {
118 static unsigned hash(const mojo::InlinedStructPtr<T>& value) {
119 return mojo::internal::Hash(mojo::internal::kHashSeed, *value.get());
yzshen1 2016/09/20 23:44:39 ditto
tibell 2016/09/21 07:10:53 Done.
120 }
121 static bool equal(const mojo::InlinedStructPtr<T>& left,
122 const mojo::InlinedStructPtr<T>& right) {
123 return left->Equals(*right);
yzshen1 2016/09/20 23:44:39 ditto
tibell 2016/09/21 07:10:53 Done.
124 }
125 static const bool safeToCompareToEmptyOrDeleted = false;
126 };
127
73 } // namespace internal 128 } // namespace internal
74 } // namespace mojo 129 } // namespace mojo
75 130
131 namespace WTF {
132
133 template <typename T>
134 struct DefaultHash<mojo::StructPtr<T>> {
135 using Hash = mojo::internal::StructPtrHashFn<T>;
136 };
137
138 template <typename T>
139 struct HashTraits<mojo::StructPtr<T>>
140 : public GenericHashTraits<mojo::StructPtr<T>> {
141 static const bool hasIsEmptyValueFunction = true;
142 static bool isEmptyValue(const mojo::StructPtr<T>& value) {
143 return value.is_null();
144 }
145 static void constructDeletedValue(mojo::StructPtr<T>& slot, bool) {
146 mojo::StructPtr<T>::constructDeletedValue(slot);
147 }
148 static bool isDeletedValue(const mojo::StructPtr<T>& value) {
149 return value.isHashTableDeletedValue();
150 }
151 };
152
153 template <typename T>
154 struct DefaultHash<mojo::InlinedStructPtr<T>> {
155 using Hash = mojo::internal::InlinedStructPtrHashFn<T>;
156 };
157
158 template <typename T>
159 struct HashTraits<mojo::InlinedStructPtr<T>>
160 : public GenericHashTraits<mojo::InlinedStructPtr<T>> {
161 static const bool hasIsEmptyValueFunction = true;
162 static bool isEmptyValue(const mojo::InlinedStructPtr<T>& value) {
163 return value.is_null();
164 }
165 static void constructDeletedValue(mojo::InlinedStructPtr<T>& slot, bool) {
166 mojo::InlinedStructPtr<T>::constructDeletedValue(slot);
167 }
168 static bool isDeletedValue(const mojo::InlinedStructPtr<T>& value) {
169 return value.isHashTableDeletedValue();
170 }
171 };
172
173 } // namespace WTF
174
76 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_ 175 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698