blob: 5a777f90862c118680928e8d79f52761a8d1de2e [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 "Table:ForwardDeclaration:TypesHeader" }}
{{ EnsureNamespace . }}
class {{ .Name }};
{{- end }}
{{/* TODO(fxbug.dev/36441): Remove __Fuchsia__ ifdefs once we have non-Fuchsia
emulated handles for C++. */}}
{{- define "Table:TypesHeader" }}
{{ if .IsResourceType }}
{{- IfdefFuchsia -}}
{{- end }}
{{ EnsureNamespace "" }}
template <>
struct {{ .WireTableFrame }} final {
public:
{{ .WireTableFrame.Self }}() = default;
// In its intended usage, WireTableFrame will be referenced by an ObjectView.
// If the ObjectView is assigned before a move or copy, then it will reference
// the old invalid object. Because this is unsafe, copies are disallowed and
// moves are only allowed by friend classes that operate safely.
{{ .WireTableFrame.Self }}(const {{ .WireTableFrame.Self }}&) = delete;
{{ .WireTableFrame.Self }}& operator=(const {{ .WireTableFrame.Self }}&) = delete;
private:
{{ .WireTableFrame.Self }}({{ .WireTableFrame.Self }}&&) noexcept = default;
{{ .WireTableFrame.Self }}& operator=({{ .WireTableFrame.Self }}&&) noexcept = default;
{{- range $index, $item := .FrameItems }}
{{- if $item }}
::fidl::Envelope<{{ $item.Type }}> {{ $item.Name }}_;
{{- else }}
[[maybe_unused]] ::fidl::UntypedEnvelope reserved_{{ $index }}_;
{{- end }}
{{- end }}
friend class {{ . }};
};
{{ EnsureNamespace . }}
extern "C" const fidl_type_t {{ .CodingTableType }};
{{ .Docs }}
class {{ .Name }} final {
public:
{{- range .AnonymousChildren }}
using {{ .ScopedName }} = {{ .FlattenedName }};
{{- end }}
// Returns whether no field is set.
bool IsEmpty() const { return max_ordinal_ == 0; }
{{- range .Members }}
{{ "" }}
{{- .Docs }}
const {{ .Type }}& {{ .Name }}() const {
ZX_ASSERT({{ .MethodHasName }}());
return frame_ptr_->{{ .Name }}_.get_data();
}
{{ .Type }}& {{ .Name }}() {
ZX_ASSERT({{ .MethodHasName }}());
return frame_ptr_->{{ .Name }}_.get_data();
}
bool {{ .MethodHasName }}() const {
return max_ordinal_ >= {{ .Ordinal }} && frame_ptr_->{{ .Name }}_.has_data();
}
{{- if .Type.InlineInEnvelope }}
{{ $.Name }}& set_{{ .Name }}({{ .Type }} elem) {
ZX_DEBUG_ASSERT(frame_ptr_ != nullptr);
frame_ptr_->{{ .Name }}_.set_data(std::move(elem));
max_ordinal_ = std::max(max_ordinal_, static_cast<uint64_t>({{ .Ordinal }}));
return *this;
}
{{- else }}
{{- /* TODO(fxbug.dev/7999): The elem pointer should be const if it has no handles. */}}
{{ $.Name }}& set_{{ .Name }}(::fidl::ObjectView<{{ .Type }}> elem) {
ZX_DEBUG_ASSERT(frame_ptr_ != nullptr);
frame_ptr_->{{ .Name }}_.set_data(elem);
max_ordinal_ = std::max(max_ordinal_, static_cast<uint64_t>({{ .Ordinal }}));
return *this;
}
{{ $.Name }}& set_{{ .Name }}(std::nullptr_t) {
ZX_DEBUG_ASSERT(frame_ptr_ != nullptr);
frame_ptr_->{{ .Name }}_.set_data(nullptr);
return *this;
}
template <typename... Args>
{{ $.Name }}& set_{{ .Name }}(::fidl::AnyArena& allocator, Args&&... args) {
ZX_DEBUG_ASSERT(frame_ptr_ != nullptr);
frame_ptr_->{{ .Name }}_.set_data(
::fidl::ObjectView<{{ .Type }}>(allocator, std::forward<Args>(args)...));
max_ordinal_ = std::max(max_ordinal_, static_cast<uint64_t>({{ .Ordinal }}));
return *this;
}
{{- end }}
{{ $.Name }}& clear_{{ .Name }}() {
ZX_DEBUG_ASSERT(frame_ptr_ != nullptr);
frame_ptr_->{{ .Name }}_.clear_data();
return *this;
}
{{- end }}
{{ .Name }}() = default;
explicit {{ .Name }}(::fidl::AnyArena& allocator)
: frame_ptr_(::fidl::ObjectView<{{ .WireTableFrame }}>(allocator)) {}
// This constructor allows a user controlled allocation (not using a Arena).
// It should only be used when performance is key.
// As soon as the frame is given to the table, it must not be used directly or for another table.
explicit {{ .Name }}(::fidl::ObjectView<{{ .WireTableFrame }}>&& frame)
: frame_ptr_(std::move(frame)) {}
~{{ .Name }}() = default;
{{ .Name }}(const {{ .Name }}& other) noexcept = default;
{{ .Name }}& operator=(const {{ .Name }}& other) noexcept = default;
{{ .Name }}({{ .Name }}&& other) noexcept = default;
{{ .Name }}& operator=({{ .Name }}&& other) noexcept = default;
static constexpr const fidl_type_t* Type = &{{ .CodingTableType }};
static constexpr uint32_t MaxNumHandles = {{ .TypeShapeV2.MaxHandles }};
static constexpr uint32_t PrimarySize = {{ .TypeShapeV2.InlineSize }};
static constexpr uint32_t PrimarySizeV1 = {{ .TypeShapeV1.InlineSize }};
[[maybe_unused]]
static constexpr uint32_t MaxOutOfLine = {{ .TypeShapeV2.MaxOutOfLine }};
static constexpr uint32_t MaxOutOfLineV1 = {{ .TypeShapeV1.MaxOutOfLine }};
static constexpr bool HasPointer = {{ .TypeShapeV2.HasPointer }};
void Allocate(::fidl::AnyArena& allocator) {
max_ordinal_ = 0;
frame_ptr_ = ::fidl::ObjectView<{{ .WireTableFrame }}>(allocator);
}
void Init(::fidl::ObjectView<{{ .WireTableFrame }}>&& frame_ptr) {
max_ordinal_ = 0;
frame_ptr_ = std::move(frame_ptr);
}
{{- if .IsResourceType }}
void _CloseHandles();
{{- end }}
class UnownedEncodedMessage;
class OwnedEncodedMessage;
class DecodedMessage;
private:
uint64_t max_ordinal_ = 0;
::fidl::ObjectView<{{ .WireTableFrame }}> frame_ptr_;
};
{{- if .IsResourceType }}
{{- EndifFuchsia -}}
{{ end }}
{{- end }}
{{/* TODO(fxbug.dev/36441): Remove __Fuchsia__ ifdefs once we have non-Fuchsia
emulated handles for C++. */}}
{{- define "Table:TypesSource" }}
{{ if .IsResourceType }}
{{ EnsureNamespace "" }}
{{- IfdefFuchsia -}}
void {{ . }}::_CloseHandles() {
{{- range .Members }}
{{- if .Type.IsResource }}
if (has_{{ .Name }}()) {
{{- CloseHandles . true false }}
}
{{- end }}
{{- end }}
}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}
{{/* TODO(fxbug.dev/36441): Remove __Fuchsia__ ifdefs once we have non-Fuchsia
emulated handles for C++. */}}
{{- define "Table:Traits:TypesHeader" }}
{{ if .IsResourceType }}
{{- IfdefFuchsia -}}
template <>
struct IsResource<{{ . }}> : public std::true_type {};
{{- end }}
template <>
struct IsFidlType<{{ . }}> : public std::true_type {};
template <>
struct IsTable<{{ . }}> : public std::true_type {};
static_assert(std::is_standard_layout_v<{{ . }}>);
{{- if .IsResourceType }}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}