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

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

Issue 2837353002: mojo: Make EqualsTraits<> part of public C++ API (Closed)
Patch Set: fix build Created 3 years, 7 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
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/equals_traits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_EQUALS_TRAITS_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_EQUALS_TRAITS_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
7 7
8 #include <type_traits> 8 #include <type_traits>
9 #include <unordered_map> 9 #include <unordered_map>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/optional.h" 12 #include "base/optional.h"
13 #include "mojo/public/cpp/bindings/lib/template_util.h" 13 #include "mojo/public/cpp/bindings/lib/template_util.h"
14 14
15 namespace mojo { 15 namespace mojo {
16 namespace internal { 16
17 // EqualsTraits<> allows you to specify comparison functions for mapped mojo
18 // objects. By default objects can be compared if they implement operator==()
19 // or have a method named Equals().
17 20
18 template <typename T> 21 template <typename T>
19 struct HasEqualsMethod { 22 struct HasEqualsMethod {
20 template <typename U> 23 template <typename U>
21 static char Test(decltype(&U::Equals)); 24 static char Test(decltype(&U::Equals));
22 template <typename U> 25 template <typename U>
23 static int Test(...); 26 static int Test(...);
24 static const bool value = sizeof(Test<T>(0)) == sizeof(char); 27 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
25 28
26 private: 29 private:
27 EnsureTypeIsComplete<T> check_t_; 30 internal::EnsureTypeIsComplete<T> check_t_;
28 }; 31 };
29 32
30 template <typename T, bool has_equals_method = HasEqualsMethod<T>::value> 33 template <typename T, bool has_equals_method = HasEqualsMethod<T>::value>
31 struct EqualsTraits; 34 struct EqualsTraits;
32 35
33 template <typename T> 36 template <typename T>
34 bool Equals(const T& a, const T& b); 37 bool Equals(const T& a, const T& b);
35 38
36 template <typename T> 39 template <typename T>
37 struct EqualsTraits<T, true> { 40 struct EqualsTraits<T, true> {
38 static bool Equals(const T& a, const T& b) { return a.Equals(b); } 41 static bool Equals(const T& a, const T& b) { return a.Equals(b); }
39 }; 42 };
40 43
41 template <typename T> 44 template <typename T>
42 struct EqualsTraits<T, false> { 45 struct EqualsTraits<T, false> {
43 static bool Equals(const T& a, const T& b) { return a == b; } 46 static bool Equals(const T& a, const T& b) { return a == b; }
44 }; 47 };
45 48
46 template <typename T> 49 template <typename T>
47 struct EqualsTraits<base::Optional<T>, false> { 50 struct EqualsTraits<base::Optional<T>, false> {
48 static bool Equals(const base::Optional<T>& a, const base::Optional<T>& b) { 51 static bool Equals(const base::Optional<T>& a, const base::Optional<T>& b) {
49 if (!a && !b) 52 if (!a && !b)
50 return true; 53 return true;
51 if (!a || !b) 54 if (!a || !b)
52 return false; 55 return false;
53 56
54 return internal::Equals(*a, *b); 57 // NOTE: Not just Equals() because that's EqualsTraits<>::Equals() and we
58 // want mojo::Equals() for things like base::Optional<std::vector<T>>.
59 return mojo::Equals(*a, *b);
55 } 60 }
56 }; 61 };
57 62
58 template <typename T> 63 template <typename T>
59 struct EqualsTraits<std::vector<T>, false> { 64 struct EqualsTraits<std::vector<T>, false> {
60 static bool Equals(const std::vector<T>& a, const std::vector<T>& b) { 65 static bool Equals(const std::vector<T>& a, const std::vector<T>& b) {
61 if (a.size() != b.size()) 66 if (a.size() != b.size())
62 return false; 67 return false;
63 for (size_t i = 0; i < a.size(); ++i) { 68 for (size_t i = 0; i < a.size(); ++i) {
64 if (!internal::Equals(a[i], b[i])) 69 if (!mojo::Equals(a[i], b[i]))
65 return false; 70 return false;
66 } 71 }
67 return true; 72 return true;
68 } 73 }
69 }; 74 };
70 75
71 template <typename K, typename V> 76 template <typename K, typename V>
72 struct EqualsTraits<std::unordered_map<K, V>, false> { 77 struct EqualsTraits<std::unordered_map<K, V>, false> {
73 static bool Equals(const std::unordered_map<K, V>& a, 78 static bool Equals(const std::unordered_map<K, V>& a,
74 const std::unordered_map<K, V>& b) { 79 const std::unordered_map<K, V>& b) {
75 if (a.size() != b.size()) 80 if (a.size() != b.size())
76 return false; 81 return false;
77 for (const auto& element : a) { 82 for (const auto& element : a) {
78 auto iter = b.find(element.first); 83 auto iter = b.find(element.first);
79 if (iter == b.end() || !internal::Equals(element.second, iter->second)) 84 if (iter == b.end() || !mojo::Equals(element.second, iter->second))
80 return false; 85 return false;
81 } 86 }
82 return true; 87 return true;
83 } 88 }
84 }; 89 };
85 90
86 template <typename T> 91 template <typename T>
87 bool Equals(const T& a, const T& b) { 92 bool Equals(const T& a, const T& b) {
88 return EqualsTraits<T>::Equals(a, b); 93 return EqualsTraits<T>::Equals(a, b);
89 } 94 }
90 95
91 } // namespace internal
92 } // namespace mojo 96 } // namespace mojo
93 97
94 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_EQUALS_TRAITS_H_ 98 #endif // MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/equals_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698