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