// Copyright 2013 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_SERIALIZATION_UTIL_H_ #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ #include #include #include #include "base/logging.h" #include "base/macros.h" #include "mojo/public/cpp/bindings/lib/bindings_internal.h" #include "mojo/public/cpp/bindings/lib/serialization_context.h" namespace mojo { namespace internal { template struct HasIsNullMethod { template static char Test(decltype(U::IsNull) *); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template < typename Traits, typename UserType, typename std::enable_if::value>::type* = nullptr> bool CallIsNullIfExists(const UserType& input) { return Traits::IsNull(input); } template < typename Traits, typename UserType, typename std::enable_if::value>::type* = nullptr> bool CallIsNullIfExists(const UserType& input) { return false; } template struct HasSetToNullMethod { template static char Test(decltype(U::SetToNull) *); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template < typename Traits, typename UserType, typename std::enable_if::value>::type* = nullptr> bool CallSetToNullIfExists(UserType* output) { Traits::SetToNull(output); return true; } template ::value>::type* = nullptr> bool CallSetToNullIfExists(UserType* output) { LOG(ERROR) << "A null value is received. But the Struct/Array/StringTraits " << "class doesn't define a SetToNull() function and therefore is " << "unable to deserialize the value."; return false; } template struct HasSetUpContextMethod { template static char Test(decltype(U::SetUpContext) *); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template ::value> struct CustomContextHelper; template struct CustomContextHelper { template static void* SetUp(MaybeConstUserType& input, SerializationContext* context) { return Traits::SetUpContext(input); } template static void TearDown(MaybeConstUserType& input, void* custom_context) { Traits::TearDownContext(input, custom_context); } }; template struct CustomContextHelper { template static void* SetUp(MaybeConstUserType& input, SerializationContext* context) { return nullptr; } template static void TearDown(MaybeConstUserType& input, void* custom_context) { DCHECK(!custom_context); } }; template ReturnType CallWithContext(ReturnType (*f)(ParamType, void*), InputUserType&& input, void* context) { return f(std::forward(input), context); } template ReturnType CallWithContext(ReturnType (*f)(ParamType), InputUserType&& input, void* context) { return f(std::forward(input)); } template struct HasGetBeginMethod { template static char Test( decltype(U::GetBegin(std::declval())) *); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template < typename Traits, typename MaybeConstUserType, typename std::enable_if< HasGetBeginMethod::value>::type* = nullptr> decltype(Traits::GetBegin(std::declval())) CallGetBeginIfExists(MaybeConstUserType& input) { return Traits::GetBegin(input); } template < typename Traits, typename MaybeConstUserType, typename std::enable_if< !HasGetBeginMethod::value>::type* = nullptr> size_t CallGetBeginIfExists(MaybeConstUserType& input) { return 0; } template struct HasGetDataMethod { template static char Test(decltype(U::GetData(std::declval())) *); template static int Test(...); static const bool value = sizeof(Test(0)) == sizeof(char); private: EnsureTypeIsComplete check_t_; }; template < typename Traits, typename MaybeConstUserType, typename std::enable_if< HasGetDataMethod::value>::type* = nullptr> decltype(Traits::GetData(std::declval())) CallGetDataIfExists(MaybeConstUserType& input) { return Traits::GetData(input); } template < typename Traits, typename MaybeConstUserType, typename std::enable_if< !HasGetDataMethod::value>::type* = nullptr> void* CallGetDataIfExists(MaybeConstUserType& input) { return nullptr; } } // namespace internal } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_