// 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_LIB_CLONE_EQUALS_UTIL_H_ #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ #include #include #include #include "base/optional.h" #include "mojo/public/cpp/bindings/lib/template_util.h" namespace mojo { namespace internal { template struct HasCloneMethod { template static char Test(decltype(&U::Clone)); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template ::value> struct CloneTraits; template T Clone(const T& input); template struct CloneTraits { static T Clone(const T& input) { return input.Clone(); } }; template struct CloneTraits { static T Clone(const T& input) { return input; } }; template struct CloneTraits, false> { static base::Optional Clone(const base::Optional& input) { if (!input) return base::nullopt; return base::Optional(internal::Clone(*input)); } }; template struct CloneTraits, false> { static std::vector Clone(const std::vector& input) { std::vector result; result.reserve(input.size()); for (const auto& element : input) result.push_back(internal::Clone(element)); return result; } }; template struct CloneTraits, false> { static std::unordered_map Clone(const std::unordered_map& input) { std::unordered_map result; for (const auto& element : input) { result.insert(std::make_pair(internal::Clone(element.first), internal::Clone(element.second))); } return result; } }; template T Clone(const T& input) { return CloneTraits::Clone(input); }; template struct HasEqualsMethod { template static char Test(decltype(&U::Equals)); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template ::value> struct EqualsTraits; template bool Equals(const T& a, const T& b); template struct EqualsTraits { static bool Equals(const T& a, const T& b) { return a.Equals(b); } }; template struct EqualsTraits { static bool Equals(const T& a, const T& b) { return a == b; } }; template struct EqualsTraits, false> { static bool Equals(const base::Optional& a, const base::Optional& b) { if (!a && !b) return true; if (!a || !b) return false; return internal::Equals(*a, *b); } }; template struct EqualsTraits, false> { static bool Equals(const std::vector& a, const std::vector& b) { if (a.size() != b.size()) return false; for (size_t i = 0; i < a.size(); ++i) { if (!internal::Equals(a[i], b[i])) return false; } return true; } }; template struct EqualsTraits, false> { static bool Equals(const std::unordered_map& a, const std::unordered_map& b) { if (a.size() != b.size()) return false; for (const auto& element : a) { auto iter = b.find(element.first); if (iter == b.end() || !internal::Equals(element.second, iter->second)) return false; } return true; } }; template bool Equals(const T& a, const T& b) { return EqualsTraits::Equals(a, b); } } // namespace internal } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_