// Copyright 2017 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_CLONE_TRAITS_H_ #define MOJO_PUBLIC_CPP_BINDINGS_CLONE_TRAITS_H_ #include #include #include "base/containers/flat_map.h" #include "base/optional.h" #include "mojo/public/cpp/bindings/lib/template_util.h" namespace mojo { template struct HasCloneMethod { template static char Test(decltype(&U::Clone)); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: internal::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(mojo::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(mojo::Clone(element)); return result; } }; template struct CloneTraits, false> { static base::flat_map Clone(const base::flat_map& input) { base::flat_map result; for (const auto& element : input) { result.insert(std::make_pair(mojo::Clone(element.first), mojo::Clone(element.second))); } return result; } }; template T Clone(const T& input) { return CloneTraits::Clone(input); }; } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_CLONE_TRAITS_H_