blob: 14c5e6035061e2d17f4d0ae599efdc250c965a76 [file] [log] [blame]
// Copyright 2018 The Fuchsia 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 LIB_FIDL_CPP_TRAITS_H_
#define LIB_FIDL_CPP_TRAITS_H_
#include <array>
#include <cstdint>
#include <type_traits>
#include <vector>
namespace fidl {
// A type trait that indicates whether the given type is a primitive FIDL type.
template <typename T>
struct IsPrimitive : public std::false_type {};
// clang-format off
template <> struct IsPrimitive<bool> : public std::true_type {};
template <> struct IsPrimitive<uint8_t> : public std::true_type {};
template <> struct IsPrimitive<uint16_t> : public std::true_type {};
template <> struct IsPrimitive<uint32_t> : public std::true_type {};
template <> struct IsPrimitive<uint64_t> : public std::true_type {};
template <> struct IsPrimitive<int8_t> : public std::true_type {};
template <> struct IsPrimitive<int16_t> : public std::true_type {};
template <> struct IsPrimitive<int32_t> : public std::true_type {};
template <> struct IsPrimitive<int64_t> : public std::true_type {};
template <> struct IsPrimitive<float> : public std::true_type {};
template <> struct IsPrimitive<double> : public std::true_type {};
// clang-format on
template <typename T>
struct IsFidlUnion : public std::false_type {};
template <typename T>
struct IsFidlXUnion : public std::false_type {};
template <typename T>
struct IsStdArray : public std::false_type {};
template <typename T, size_t N>
struct IsStdArray<std::array<T, N>> : public std::true_type {};
template <typename T>
struct IsStdVector : public std::false_type {};
template <typename V, typename A>
struct IsStdVector<std::vector<V, A>> : public std::true_type {};
template <typename T>
struct IsStdString : public std::false_type {};
template <>
struct IsStdString<std::string> : public std::true_type {};
// True if a struct has padding.
// Does not include inner structs or other types recursively.
template <typename T>
struct HasPadding : public std::false_type {};
// The type be directly copied with memcpy to/from the wire format representation.
template <typename T>
struct IsMemcpyCompatible : public std::false_type {};
// clang-format off
template <> struct IsMemcpyCompatible<uint8_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<uint16_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<uint32_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<uint64_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<int8_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<int16_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<int32_t> : public std::true_type {};
template <> struct IsMemcpyCompatible<int64_t> : public std::true_type {};
// clang-format on
template <typename T, size_t N>
struct IsMemcpyCompatible<std::array<T, N>> : public IsMemcpyCompatible<T> {};
namespace internal {
// A C++14 compatible replacement for std::bool_constant.
template <bool Value>
struct BoolConstant {
static const bool value = Value;
};
} // namespace internal
} // namespace fidl
#endif // LIB_FIDL_CPP_TRAITS_H_