| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_CLONE_EQUALS_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_CLONE_TRAITS_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_CLONE_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 { | |
| 17 | 16 |
| 18 template <typename T> | 17 template <typename T> |
| 19 struct HasCloneMethod { | 18 struct HasCloneMethod { |
| 20 template <typename U> | 19 template <typename U> |
| 21 static char Test(decltype(&U::Clone)); | 20 static char Test(decltype(&U::Clone)); |
| 22 template <typename U> | 21 template <typename U> |
| 23 static int Test(...); | 22 static int Test(...); |
| 24 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | 23 static const bool value = sizeof(Test<T>(0)) == sizeof(char); |
| 25 | 24 |
| 26 private: | 25 private: |
| 27 EnsureTypeIsComplete<T> check_t_; | 26 internal::EnsureTypeIsComplete<T> check_t_; |
| 28 }; | 27 }; |
| 29 | 28 |
| 30 template <typename T, bool has_clone_method = HasCloneMethod<T>::value> | 29 template <typename T, bool has_clone_method = HasCloneMethod<T>::value> |
| 31 struct CloneTraits; | 30 struct CloneTraits; |
| 32 | 31 |
| 33 template <typename T> | 32 template <typename T> |
| 34 T Clone(const T& input); | 33 T Clone(const T& input); |
| 35 | 34 |
| 36 template <typename T> | 35 template <typename T> |
| 37 struct CloneTraits<T, true> { | 36 struct CloneTraits<T, true> { |
| 38 static T Clone(const T& input) { return input.Clone(); } | 37 static T Clone(const T& input) { return input.Clone(); } |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 template <typename T> | 40 template <typename T> |
| 42 struct CloneTraits<T, false> { | 41 struct CloneTraits<T, false> { |
| 43 static T Clone(const T& input) { return input; } | 42 static T Clone(const T& input) { return input; } |
| 44 }; | 43 }; |
| 45 | 44 |
| 46 template <typename T> | 45 template <typename T> |
| 47 struct CloneTraits<base::Optional<T>, false> { | 46 struct CloneTraits<base::Optional<T>, false> { |
| 48 static base::Optional<T> Clone(const base::Optional<T>& input) { | 47 static base::Optional<T> Clone(const base::Optional<T>& input) { |
| 49 if (!input) | 48 if (!input) |
| 50 return base::nullopt; | 49 return base::nullopt; |
| 51 | 50 |
| 52 return base::Optional<T>(internal::Clone(*input)); | 51 return base::Optional<T>(mojo::Clone(*input)); |
| 53 } | 52 } |
| 54 }; | 53 }; |
| 55 | 54 |
| 56 template <typename T> | 55 template <typename T> |
| 57 struct CloneTraits<std::vector<T>, false> { | 56 struct CloneTraits<std::vector<T>, false> { |
| 58 static std::vector<T> Clone(const std::vector<T>& input) { | 57 static std::vector<T> Clone(const std::vector<T>& input) { |
| 59 std::vector<T> result; | 58 std::vector<T> result; |
| 60 result.reserve(input.size()); | 59 result.reserve(input.size()); |
| 61 for (const auto& element : input) | 60 for (const auto& element : input) |
| 62 result.push_back(internal::Clone(element)); | 61 result.push_back(mojo::Clone(element)); |
| 63 | 62 |
| 64 return result; | 63 return result; |
| 65 } | 64 } |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 template <typename K, typename V> | 67 template <typename K, typename V> |
| 69 struct CloneTraits<std::unordered_map<K, V>, false> { | 68 struct CloneTraits<std::unordered_map<K, V>, false> { |
| 70 static std::unordered_map<K, V> Clone(const std::unordered_map<K, V>& input) { | 69 static std::unordered_map<K, V> Clone(const std::unordered_map<K, V>& input) { |
| 71 std::unordered_map<K, V> result; | 70 std::unordered_map<K, V> result; |
| 72 for (const auto& element : input) { | 71 for (const auto& element : input) { |
| 73 result.insert(std::make_pair(internal::Clone(element.first), | 72 result.insert(std::make_pair(mojo::Clone(element.first), |
| 74 internal::Clone(element.second))); | 73 mojo::Clone(element.second))); |
| 75 } | 74 } |
| 76 return result; | 75 return result; |
| 77 } | 76 } |
| 78 }; | 77 }; |
| 79 | 78 |
| 80 template <typename T> | 79 template <typename T> |
| 81 T Clone(const T& input) { | 80 T Clone(const T& input) { |
| 82 return CloneTraits<T>::Clone(input); | 81 return CloneTraits<T>::Clone(input); |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 template <typename T> | |
| 86 struct HasEqualsMethod { | |
| 87 template <typename U> | |
| 88 static char Test(decltype(&U::Equals)); | |
| 89 template <typename U> | |
| 90 static int Test(...); | |
| 91 static const bool value = sizeof(Test<T>(0)) == sizeof(char); | |
| 92 | |
| 93 private: | |
| 94 EnsureTypeIsComplete<T> check_t_; | |
| 95 }; | |
| 96 | |
| 97 template <typename T, bool has_equals_method = HasEqualsMethod<T>::value> | |
| 98 struct EqualsTraits; | |
| 99 | |
| 100 template <typename T> | |
| 101 bool Equals(const T& a, const T& b); | |
| 102 | |
| 103 template <typename T> | |
| 104 struct EqualsTraits<T, true> { | |
| 105 static bool Equals(const T& a, const T& b) { return a.Equals(b); } | |
| 106 }; | |
| 107 | |
| 108 template <typename T> | |
| 109 struct EqualsTraits<T, false> { | |
| 110 static bool Equals(const T& a, const T& b) { return a == b; } | |
| 111 }; | |
| 112 | |
| 113 template <typename T> | |
| 114 struct EqualsTraits<base::Optional<T>, false> { | |
| 115 static bool Equals(const base::Optional<T>& a, const base::Optional<T>& b) { | |
| 116 if (!a && !b) | |
| 117 return true; | |
| 118 if (!a || !b) | |
| 119 return false; | |
| 120 | |
| 121 return internal::Equals(*a, *b); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 template <typename T> | |
| 126 struct EqualsTraits<std::vector<T>, false> { | |
| 127 static bool Equals(const std::vector<T>& a, const std::vector<T>& b) { | |
| 128 if (a.size() != b.size()) | |
| 129 return false; | |
| 130 for (size_t i = 0; i < a.size(); ++i) { | |
| 131 if (!internal::Equals(a[i], b[i])) | |
| 132 return false; | |
| 133 } | |
| 134 return true; | |
| 135 } | |
| 136 }; | |
| 137 | |
| 138 template <typename K, typename V> | |
| 139 struct EqualsTraits<std::unordered_map<K, V>, false> { | |
| 140 static bool Equals(const std::unordered_map<K, V>& a, | |
| 141 const std::unordered_map<K, V>& b) { | |
| 142 if (a.size() != b.size()) | |
| 143 return false; | |
| 144 for (const auto& element : a) { | |
| 145 auto iter = b.find(element.first); | |
| 146 if (iter == b.end() || !internal::Equals(element.second, iter->second)) | |
| 147 return false; | |
| 148 } | |
| 149 return true; | |
| 150 } | |
| 151 }; | |
| 152 | |
| 153 template <typename T> | |
| 154 bool Equals(const T& a, const T& b) { | |
| 155 return EqualsTraits<T>::Equals(a, b); | |
| 156 } | |
| 157 | |
| 158 } // namespace internal | |
| 159 } // namespace mojo | 84 } // namespace mojo |
| 160 | 85 |
| 161 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ | 86 #endif // MOJO_PUBLIC_CPP_BINDINGS_CLONE_TRAITS_H_ |
| OLD | NEW |