• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
7 
8 #include <type_traits>
9 #include <vector>
10 
11 #include "base/containers/flat_map.h"
12 #include "base/optional.h"
13 #include "mojo/public/cpp/bindings/lib/template_util.h"
14 
15 namespace mojo {
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().
20 
21 template <typename T>
22 struct HasEqualsMethod {
23   template <typename U>
24   static char Test(decltype(&U::Equals));
25   template <typename U>
26   static int Test(...);
27   static const bool value = sizeof(Test<T>(0)) == sizeof(char);
28 
29  private:
30   internal::EnsureTypeIsComplete<T> check_t_;
31 };
32 
33 template <typename T, bool has_equals_method = HasEqualsMethod<T>::value>
34 struct EqualsTraits;
35 
36 template <typename T>
37 bool Equals(const T& a, const T& b);
38 
39 template <typename T>
40 struct EqualsTraits<T, true> {
41   static bool Equals(const T& a, const T& b) { return a.Equals(b); }
42 };
43 
44 template <typename T>
45 struct EqualsTraits<T, false> {
46   static bool Equals(const T& a, const T& b) { return a == b; }
47 };
48 
49 template <typename T>
50 struct EqualsTraits<base::Optional<T>, false> {
51   static bool Equals(const base::Optional<T>& a, const base::Optional<T>& b) {
52     if (!a && !b)
53       return true;
54     if (!a || !b)
55       return false;
56 
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);
60   }
61 };
62 
63 template <typename T>
64 struct EqualsTraits<std::vector<T>, false> {
65   static bool Equals(const std::vector<T>& a, const std::vector<T>& b) {
66     if (a.size() != b.size())
67       return false;
68     for (size_t i = 0; i < a.size(); ++i) {
69       if (!mojo::Equals(a[i], b[i]))
70         return false;
71     }
72     return true;
73   }
74 };
75 
76 template <typename K, typename V>
77 struct EqualsTraits<base::flat_map<K, V>, false> {
78   static bool Equals(const base::flat_map<K, V>& a,
79                      const base::flat_map<K, V>& b) {
80     if (a.size() != b.size())
81       return false;
82     for (const auto& element : a) {
83       auto iter = b.find(element.first);
84       if (iter == b.end() || !mojo::Equals(element.second, iter->second))
85         return false;
86     }
87     return true;
88   }
89 };
90 
91 template <typename T>
92 bool Equals(const T& a, const T& b) {
93   return EqualsTraits<T>::Equals(a, b);
94 }
95 
96 }  // namespace mojo
97 
98 #endif  // MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
99