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

Side by Side Diff: mojo/public/cpp/bindings/map.h

Issue 2031823002: Mojo C++ bindings: more consistent Clone() and Equals(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_MAP_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <map> 9 #include <map>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "mojo/public/cpp/bindings/lib/map_internal.h" 13 #include "base/move.h"
14 #include "mojo/public/cpp/bindings/lib/value_traits.h" 14 #include "mojo/public/cpp/bindings/array.h"
15 #include "mojo/public/cpp/bindings/lib/map_data_internal.h"
16 #include "mojo/public/cpp/bindings/lib/template_util.h"
17 #include "mojo/public/cpp/bindings/type_converter.h"
15 18
16 namespace mojo { 19 namespace mojo {
17 20
18 // A move-only map that can handle move-only values. Map has the following 21 // A move-only map that can handle move-only values. Map has the following
19 // characteristics: 22 // characteristics:
20 // - The map itself can be null, and this is distinct from empty. 23 // - The map itself can be null, and this is distinct from empty.
21 // - Keys must not be move-only. 24 // - Keys must not be move-only.
22 // - The Key-type's "<" operator is used to sort the entries, and also is 25 // - The Key-type's "<" operator is used to sort the entries, and also is
23 // used to determine equality of the key values. 26 // used to determine equality of the key values.
24 // - There can only be one entry per unique key. 27 // - There can only be one entry per unique key.
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 value_vector.push_back(std::move(entry.second)); 180 value_vector.push_back(std::move(entry.second));
178 } 181 }
179 182
180 map_.clear(); 183 map_.clear();
181 is_null_ = true; 184 is_null_ = true;
182 185
183 keys->Swap(&key_vector); 186 keys->Swap(&key_vector);
184 values->Swap(&value_vector); 187 values->Swap(&value_vector);
185 } 188 }
186 189
187 // Returns a new Map that contains a copy of the contents of this map. If the 190 // Returns a new Map that contains a copy of the contents of this map. If the
188 // values are of a type that is designated move-only, they will be cloned 191 // key/value type defines a Clone() method, it will be used; otherwise copy
189 // using the Clone() method of the type. Please note that calling this method 192 // constructor/assignment will be used.
190 // will fail compilation if the value type cannot be cloned (which usually 193 //
191 // means that it is a Mojo handle type or a type that contains Mojo handles). 194 // Please note that calling this method will fail compilation if the key/value
195 // type cannot be cloned (which usually means that it is a Mojo handle type or
196 // a type containing Mojo handles).
192 Map Clone() const { 197 Map Clone() const {
193 Map result; 198 Map result;
194 result.is_null_ = is_null_; 199 result.is_null_ = is_null_;
195 Traits::Clone(map_, &result.map_); 200 for (auto it = map_.begin(); it != map_.end(); ++it) {
201 result.map_.insert(std::make_pair(internal::Clone(it->first),
202 internal::Clone(it->second)));
203 }
196 return result; 204 return result;
197 } 205 }
198 206
199 // Indicates whether the contents of this map are equal to those of another 207 // Indicates whether the contents of this map are equal to those of another
200 // Map (including nullness). Keys are compared by the != operator. Values are 208 // Map (including nullness). If the key/value type defines an Equals() method,
201 // compared as follows: 209 // it will be used; otherwise == operator will be used.
202 // - Map, Array, Struct, or StructPtr values are compared by their Equals()
203 // method.
204 // - ScopedHandleBase-derived types are compared by their handles.
205 // - Values of other types are compared by their "==" operator.
206 bool Equals(const Map& other) const { 210 bool Equals(const Map& other) const {
207 if (is_null() != other.is_null()) 211 if (is_null() != other.is_null())
208 return false; 212 return false;
209 if (size() != other.size()) 213 if (size() != other.size())
210 return false; 214 return false;
211 auto i = begin(); 215 auto i = begin();
212 auto j = other.begin(); 216 auto j = other.begin();
213 while (i != end()) { 217 while (i != end()) {
214 if (i->first != j->first) 218 if (!internal::Equals(i->first, j->first))
215 return false; 219 return false;
216 if (!internal::ValueTraits<Value>::Equals(i->second, j->second)) 220 if (!internal::Equals(i->second, j->second))
217 return false; 221 return false;
218 ++i; 222 ++i;
219 ++j; 223 ++j;
220 } 224 }
221 return true; 225 return true;
222 } 226 }
223 227
224 // Provide read-only iteration over map members in a way similar to STL 228 // Provide read-only iteration over map members in a way similar to STL
225 // collections. 229 // collections.
226 ConstIterator begin() const { return map_.begin(); } 230 ConstIterator begin() const { return map_.begin(); }
(...skipping 10 matching lines...) Expand all
237 private: 241 private:
238 typedef std::map<Key, Value> Map::*Testable; 242 typedef std::map<Key, Value> Map::*Testable;
239 243
240 public: 244 public:
241 // The Map may be used in boolean expressions to determine if it is non-null, 245 // The Map may be used in boolean expressions to determine if it is non-null,
242 // but is not implicitly convertible to an actual bool value (which would be 246 // but is not implicitly convertible to an actual bool value (which would be
243 // dangerous). 247 // dangerous).
244 operator Testable() const { return is_null_ ? 0 : &Map::map_; } 248 operator Testable() const { return is_null_ ? 0 : &Map::map_; }
245 249
246 private: 250 private:
247 using Traits =
248 internal::MapCloneTraits<Key,
249 Value,
250 internal::IsMoveOnlyType<Value>::value>;
251
252 // Forbid the == and != operators explicitly, otherwise Map will be converted 251 // Forbid the == and != operators explicitly, otherwise Map will be converted
253 // to Testable to do == or != comparison. 252 // to Testable to do == or != comparison.
254 template <typename T, typename U> 253 template <typename T, typename U>
255 bool operator==(const Map<T, U>& other) const = delete; 254 bool operator==(const Map<T, U>& other) const = delete;
256 template <typename T, typename U> 255 template <typename T, typename U>
257 bool operator!=(const Map<T, U>& other) const = delete; 256 bool operator!=(const Map<T, U>& other) const = delete;
258 257
259 void Take(Map* other) { 258 void Take(Map* other) {
260 operator=(nullptr); 259 operator=(nullptr);
261 Swap(other); 260 Swap(other);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 TypeConverter<STLValue, MojoValue>::Convert(it->second))); 299 TypeConverter<STLValue, MojoValue>::Convert(it->second)));
301 } 300 }
302 } 301 }
303 return result; 302 return result;
304 } 303 }
305 }; 304 };
306 305
307 } // namespace mojo 306 } // namespace mojo
308 307
309 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ 308 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/value_traits.h ('k') | mojo/public/cpp/bindings/tests/equals_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698