chromium/mojo/public/cpp/bindings/lib/serialization_util.h

// Copyright 2013 The Chromium Authors
// 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 <stddef.h>

#include <type_traits>
#include <utility>

#include "mojo/public/cpp/bindings/lib/bindings_internal.h"
#include "mojo/public/cpp/bindings/lib/serialization_forward.h"
#include "mojo/public/cpp/bindings/lib/template_util.h"

namespace mojo {
namespace internal {

template <typename T, typename U, typename SFINAE = void>
struct HasIsNullMethod : std::false_type {};

HasIsNullMethod<T, U, std::void_t<decltype(T::IsNull(std::declval<const U &>()))>>;

template <typename Traits, typename UserType>
bool CallIsNullIfExists(const UserType& input) {}

template <typename T, typename U, typename SFINAE = void>
struct HasSetToNullMethod : std::false_type {};

HasSetToNullMethod<T, U, std::void_t<decltype(T::SetToNull(std::declval<U *>()))>>;

template <typename Traits, typename UserType>
bool CallSetToNullIfExists(UserType* output) {}

template <typename MojomType, typename UserType, typename = void>
struct TraitsFinder {};

TraitsFinder<MojomType, UserType, std::enable_if_t<BelongsTo<MojomType, MojomTypeCategory::kUnion>::value>>;

TraitsFinder<StringDataView, UserType>;

TraitsFinder<ArrayDataView<ElementType>, UserType>;

TraitsFinder<MapDataView<KeyType, ValueType>, UserType>;

template <typename MojomType, typename UserType>
constexpr bool IsValidUserTypeForOptionalValue() {}

template <typename T, typename U, typename SFINAE = void>
struct HasGetBeginMethod : std::false_type {};

HasGetBeginMethod<T, U, std::void_t<decltype(T::GetBegin(std::declval<U &>()))>>;

template <typename T, typename U, typename SFINAE = void>
struct HasGetDataMethod : std::false_type {};

// TODO(dcheng): Figure out why the `&` below is load-bearing and document it or
// improve this and remove the hack.
HasGetDataMethod<T, U, std::void_t<decltype(& T::GetData(std::declval<U &>()))>>;

}  // namespace internal
}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_