summaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/equals_traits.h
blob: 5d5dfa251e7a9d6cab4503e64599dcf8c06c42e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_
#define MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_

#include <type_traits>
#include <unordered_map>
#include <vector>

#include "base/optional.h"
#include "mojo/public/cpp/bindings/lib/template_util.h"

namespace mojo {

// EqualsTraits<> allows you to specify comparison functions for mapped mojo
// objects. By default objects can be compared if they implement operator==()
// or have a method named Equals().

template <typename T>
struct HasEqualsMethod {
  template <typename U>
  static char Test(decltype(&U::Equals));
  template <typename U>
  static int Test(...);
  static const bool value = sizeof(Test<T>(0)) == sizeof(char);

 private:
  internal::EnsureTypeIsComplete<T> check_t_;
};

template <typename T, bool has_equals_method = HasEqualsMethod<T>::value>
struct EqualsTraits;

template <typename T>
bool Equals(const T& a, const T& b);

template <typename T>
struct EqualsTraits<T, true> {
  static bool Equals(const T& a, const T& b) { return a.Equals(b); }
};

template <typename T>
struct EqualsTraits<T, false> {
  static bool Equals(const T& a, const T& b) { return a == b; }
};

template <typename T>
struct EqualsTraits<base::Optional<T>, false> {
  static bool Equals(const base::Optional<T>& a, const base::Optional<T>& b) {
    if (!a && !b)
      return true;
    if (!a || !b)
      return false;

    // NOTE: Not just Equals() because that's EqualsTraits<>::Equals() and we
    // want mojo::Equals() for things like base::Optional<std::vector<T>>.
    return mojo::Equals(*a, *b);
  }
};

template <typename T>
struct EqualsTraits<std::vector<T>, false> {
  static bool Equals(const std::vector<T>& a, const std::vector<T>& b) {
    if (a.size() != b.size())
      return false;
    for (size_t i = 0; i < a.size(); ++i) {
      if (!mojo::Equals(a[i], b[i]))
        return false;
    }
    return true;
  }
};

template <typename K, typename V>
struct EqualsTraits<std::unordered_map<K, V>, false> {
  static bool Equals(const std::unordered_map<K, V>& a,
                     const std::unordered_map<K, V>& b) {
    if (a.size() != b.size())
      return false;
    for (const auto& element : a) {
      auto iter = b.find(element.first);
      if (iter == b.end() || !mojo::Equals(element.second, iter->second))
        return false;
    }
    return true;
  }
};

template <typename T>
bool Equals(const T& a, const T& b) {
  return EqualsTraits<T>::Equals(a, b);
}

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_EQUALS_TRAITS_H_