OLD | NEW |
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_WTF_MAP_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "mojo/public/cpp/bindings/lib/template_util.h" | 12 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 13 #include "mojo/public/cpp/bindings/lib/wtf_clone_equals_util.h" |
13 #include "mojo/public/cpp/bindings/type_converter.h" | 14 #include "mojo/public/cpp/bindings/type_converter.h" |
14 #include "third_party/WebKit/Source/wtf/HashMap.h" | 15 #include "third_party/WebKit/Source/wtf/HashMap.h" |
15 #include "third_party/WebKit/Source/wtf/text/StringHash.h" | 16 #include "third_party/WebKit/Source/wtf/text/StringHash.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 | 19 |
19 // Represents a map backed by WTF::HashMap. Comparing with WTF::HashMap, | 20 // Represents a map backed by WTF::HashMap. Comparing with WTF::HashMap, |
20 // mojo::WTFMap is move-only and can be null. | 21 // mojo::WTFMap is move-only and can be null. |
21 // | 22 // |
22 // It is easy to convert between WTF::HashMap<K, V> and mojo::WTFMap<K, V>: | 23 // It is easy to convert between WTF::HashMap<K, V> and mojo::WTFMap<K, V>: |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // Returns a new WTFMap that contains a copy of the contents of this map. If | 150 // Returns a new WTFMap that contains a copy of the contents of this map. If |
150 // the key/value type defines a Clone() method, it will be used; otherwise | 151 // the key/value type defines a Clone() method, it will be used; otherwise |
151 // copy constructor/assignment will be used. | 152 // copy constructor/assignment will be used. |
152 // | 153 // |
153 // Please note that calling this method will fail compilation if the key/value | 154 // Please note that calling this method will fail compilation if the key/value |
154 // type cannot be cloned (which usually means that it is a Mojo handle type or | 155 // type cannot be cloned (which usually means that it is a Mojo handle type or |
155 // a type containing Mojo handles). | 156 // a type containing Mojo handles). |
156 WTFMap Clone() const { | 157 WTFMap Clone() const { |
157 WTFMap result; | 158 WTFMap result; |
158 result.is_null_ = is_null_; | 159 result.is_null_ = is_null_; |
159 auto map_end = map_.end(); | 160 result.map_ = internal::Clone(map_); |
160 for (auto it = map_.begin(); it != map_end; ++it) | |
161 result.map_.add(internal::Clone(it->key), internal::Clone(it->value)); | |
162 return result; | 161 return result; |
163 } | 162 } |
164 | 163 |
165 // Indicates whether the contents of this map are equal to those of another | 164 // Indicates whether the contents of this map are equal to those of another |
166 // WTFMap (including nullness). If the key/value type defines an Equals() | 165 // WTFMap (including nullness). If the key/value type defines an Equals() |
167 // method, it will be used; otherwise == operator will be used. | 166 // method, it will be used; otherwise == operator will be used. |
168 bool Equals(const WTFMap& other) const { | 167 bool Equals(const WTFMap& other) const { |
169 if (is_null() != other.is_null()) | 168 if (is_null() != other.is_null()) |
170 return false; | 169 return false; |
171 if (size() != other.size()) | 170 return internal::Equals(map_, other.map_); |
172 return false; | |
173 | |
174 auto this_end = map_.end(); | |
175 auto other_end = other.map_.end(); | |
176 | |
177 for (auto iter = map_.begin(); iter != this_end; ++iter) { | |
178 auto other_iter = other.map_.find(iter->key); | |
179 if (other_iter == other_end || | |
180 !internal::Equals(iter->value, other_iter->value)) { | |
181 return false; | |
182 } | |
183 } | |
184 return true; | |
185 } | 171 } |
186 | 172 |
187 ConstIterator begin() const { return map_.begin(); } | 173 ConstIterator begin() const { return map_.begin(); } |
188 Iterator begin() { return map_.begin(); } | 174 Iterator begin() { return map_.begin(); } |
189 | 175 |
190 ConstIterator end() const { return map_.end(); } | 176 ConstIterator end() const { return map_.end(); } |
191 Iterator end() { return map_.end(); } | 177 Iterator end() { return map_.end(); } |
192 | 178 |
193 // Returns the iterator pointing to the entry for |key|, if present, or else | 179 // Returns the iterator pointing to the entry for |key|, if present, or else |
194 // returns end(). | 180 // returns end(). |
(...skipping 10 matching lines...) Expand all Loading... |
205 | 191 |
206 WTF::HashMap<Key, Value> map_; | 192 WTF::HashMap<Key, Value> map_; |
207 bool is_null_; | 193 bool is_null_; |
208 | 194 |
209 DISALLOW_COPY_AND_ASSIGN(WTFMap); | 195 DISALLOW_COPY_AND_ASSIGN(WTFMap); |
210 }; | 196 }; |
211 | 197 |
212 } // namespace mojo | 198 } // namespace mojo |
213 | 199 |
214 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_ | 200 #endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_ |
OLD | NEW |