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_CLONE_EQUALS_UTIL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_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 HasCloneMethod { 20 template <typename U> 21 static char Test(decltype(&U::Clone)); 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_clone_method = HasCloneMethod<T>::value> 31 struct CloneTraits; 32 33 template <typename T> 34 T Clone(const T& input); 35 36 template <typename T> 37 struct CloneTraits<T, true> { 38 static T Clone(const T& input) { return input.Clone(); } 39 }; 40 41 template <typename T> 42 struct CloneTraits<T, false> { 43 static T Clone(const T& input) { return input; } 44 }; 45 46 template <typename T> 47 struct CloneTraits<base::Optional<T>, false> { 48 static base::Optional<T> Clone(const base::Optional<T>& input) { 49 if (!input) 50 return base::nullopt; 51 52 return base::Optional<T>(internal::Clone(*input)); 53 } 54 }; 55 56 template <typename T> 57 struct CloneTraits<std::vector<T>, false> { 58 static std::vector<T> Clone(const std::vector<T>& input) { 59 std::vector<T> result; 60 result.reserve(input.size()); 61 for (const auto& element : input) 62 result.push_back(internal::Clone(element)); 63 64 return result; 65 } 66 }; 67 68 template <typename K, typename V> 69 struct CloneTraits<std::unordered_map<K, V>, false> { 70 static std::unordered_map<K, V> Clone(const std::unordered_map<K, V>& input) { 71 std::unordered_map<K, V> result; 72 for (const auto& element : input) { 73 result.insert(std::make_pair(internal::Clone(element.first), 74 internal::Clone(element.second))); 75 } 76 return result; 77 } 78 }; 79 80 template <typename T> 81 T Clone(const T& input) { 82 return CloneTraits<T>::Clone(input); 83 }; 84 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 160 161 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ 162