blob: 3652c4b92da2a6c862379a5cd42b84966d1e50c5 [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.
*/}}
{{- define "Struct:ForwardDeclaration:WireTypesHeader" }}
{{ EnsureNamespace . }}
struct {{ .Name }};
{{- end }}
{{- define "Struct:WireTypesHeader" }}
{{ EnsureNamespace . }}
{{ if .IsResourceType }}
{{- IfdefFuchsia -}}
{{- end }}
{{ .Docs }}
struct {{ .Name }} {
{{- range .AnonymousChildren }}
using {{ .ScopedName }} = {{ .FlattenedName }};
{{- end }}
{{- range .Members }}
{{ "" }}
{{- .Docs }}
{{ .Type }} {{ .Name }} = {};
{{- end }}
{{- if .IsResourceType }}
void _CloseHandles();
{{- end }}
};
{{- if .IsResourceType }}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}
{{- define "Struct:WireTypesSource" }}
{{ EnsureNamespace "" }}
{{ if .IsResourceType }}
{{- IfdefFuchsia -}}
void {{ . }}::_CloseHandles() {
{{- range .Members }}
{{- CloseHandles . false }}
{{- end }}
}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}
{{- define "Struct:Traits:WireTypesHeader" }}
{{ if .IsResourceType }}
{{- IfdefFuchsia -}}
template <>
struct IsResource<{{ . }}> : public std::true_type {};
{{- end }}
template<>
struct TypeTraits<{{ . }}> {
static constexpr uint32_t kMaxNumHandles = {{ .TypeShapeV2.MaxHandles }};
static constexpr uint32_t kMaxDepth = {{ .TypeShapeV2.Depth }};
static constexpr uint32_t kPrimarySize = {{ .TypeShapeV2.InlineSize }};
[[maybe_unused]]
static constexpr uint32_t kMaxOutOfLine = {{ .TypeShapeV2.MaxOutOfLine }};
static constexpr bool kHasFlexibleEnvelope = {{ .TypeShapeV2.HasFlexibleEnvelope }};
static constexpr bool kHasPointer = {{ .TypeShapeV2.HasPointer }};
};
template <>
struct IsFidlType<{{ . }}> : public std::true_type {};
template <>
struct IsWire<{{ . }}> : public std::true_type {};
template <>
struct IsStruct<{{ . }}> : public std::true_type {};
static_assert(std::is_standard_layout_v<{{ . }}>);
{{- $struct := . }}
{{- range .Members }}
static_assert(offsetof({{ $struct }}, {{ .Name }}) == {{ .OffsetV2 }});
{{- end }}
static_assert(sizeof({{ . }}) == TypeTraits<{{ . }}>::kPrimarySize);
template <bool IsRecursive>
struct ::fidl::internal::WireCodingTraits<{{ . }}, ::fidl::internal::WireCodingConstraintEmpty, IsRecursive> {
static constexpr size_t kInlineSize = {{ .TypeShapeV2.InlineSize }};
static constexpr auto kMembers = std::make_tuple(
{{- range $i, $m := .Members }}
{{- if $i }}, {{ end -}}
::fidl::internal::WireStructMemberCodingInfo<{{ $m.Type }}, {{ $m.WireConstraint }}, IsRecursive>()
{{- end }});
static constexpr bool kHasPadding = {{ gt (len .PaddingV2) 0 }};
using Base = WireStructCodingTraitsBase<{{ . }}, ::fidl::internal::WireCodingConstraintEmpty, IsRecursive>;
static constexpr bool kIsMemcpyCompatible = Base::kIsMemcpyCompatible;
static void Encode(
internal::WireEncoder* encoder, {{ . }}* value, ::fidl::internal::WirePosition position, RecursionDepth<IsRecursive> recursion_depth) {
if constexpr (kIsMemcpyCompatible) {
memcpy(position.As<void>(), value, sizeof({{ . }}));
} else {
{{- range .PaddingV2 }}
internal::WireZeroPadding<uint{{ .MaskBitWidth }}_t>(encoder, position + {{ .Offset }});
{{- end }}
{{- range .Members }}
internal::WireCodingTraits<{{ .Type }}, {{ .WireConstraint }}, IsRecursive>::Encode(encoder, &value->{{ . }}, position + {{ .OffsetV2 }}, recursion_depth);
{{- end }}
}
}
static void Decode(
internal::WireDecoder* decoder, ::fidl::internal::WirePosition position, RecursionDepth<IsRecursive> recursion_depth) {
if constexpr (!Base::are_members_memcpy_compatible) {
{{- range .Members }}
internal::WireCodingTraits<{{ .Type }}, {{ .WireConstraint }}, IsRecursive>::Decode(
decoder, position + {{ .OffsetV2 }}, recursion_depth);
{{- end }}
}
{{- range .PaddingV2 }}
internal::WireCheckPadding<uint{{ .MaskBitWidth }}_t>(decoder, position + {{ .Offset }}, {{ .Mask | printf "%#x" }});
{{- end }}
{{- if .IsEmpty }}
if (*position.As<uint8_t>() != 0) {
decoder->SetError(kCodingErrorInvalidPaddingBytes);
}
{{- end }}
}
};
{{- if .IsResourceType }}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}