blob: c17ad0c070b9d44f2bf21fb18cf637ab670313e0 [file] [log] [blame]
{{/*
// Copyright 2022 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 "driver/Method:UnownedResult:MessagingHeader" }}
{{- EnsureNamespace "" }}
template<>
class [[nodiscard]] {{ .WireUnownedResult }} final : public ::fidl::Status {
public:
{{- if .Transport.HasSyncClient }}
{{- $args := (printf "::%s::UnownedClientEnd<%s> client_end" .Transport.Namespace .Protocol) }}
{{- $args = (List $args "const ::fdf::Arena& arena") }}
{{- $args = (List $args (printf "%s* request" .WireTransactionalRequest)) }}
explicit {{ .WireUnownedResult.Self }}({{ RenderParams $args }});
{{- end }}
{{- if .HasResponse }}
explicit {{ .WireUnownedResult.Self }}({{ .WireTransactionalResponse }}* response)
: fidl::Status(fidl::Status::Ok()), decoded_(response) {
{{- if .HasApplicationError }}
auto* raw_response = &decoded_.Value()->body;
if (raw_response->result.is_err()) {
result_ = fitx::error(raw_response->result.err());
} else {
result_ = fitx::ok({{ if .Result.ValueParameters }}&(raw_response->result.response()){{ end }});
}
{{- end }}
}
explicit {{ .WireUnownedResult.Self }}(
::fidl::unstable::DecodedMessage<{{ .WireTransactionalResponse }}>&& decoded,
::fidl::internal::MessageStorageViewBase* storage_view
) : ::fidl::Status(decoded), arena_(::fidl::internal::TakeDriverArenaFromStorage(storage_view)) {
if (decoded.ok()) {
decoded_ = decoded.Take();
{{- if .HasApplicationError }}
auto* raw_response = &decoded_.Value()->body;
if (raw_response->result.is_err()) {
result_ = fitx::error(raw_response->result.err());
} else if (raw_response->result.is_response()) {
result_ = fitx::ok(
{{- if .Result.ValueParameters -}}
&(raw_response->result.response())
{{- end -}}
);
}
{{- end }}
}
}
{{- end }}
explicit {{ .WireUnownedResult.Self }}(const ::fidl::Status& result) : ::fidl::Status(result) {}
{{ .WireUnownedResult.Self }}({{ .WireUnownedResult.Self }}&&) = delete;
{{ .WireUnownedResult.Self }}(const {{ .WireUnownedResult.Self }}&) = delete;
{{ .WireUnownedResult.Self }}* operator=({{ .WireUnownedResult.Self }}&&) = delete;
{{ .WireUnownedResult.Self }}* operator=(const {{ .WireUnownedResult.Self }}&) = delete;
{{- if .HasWireResultBase }}
{{ .WireResultBase }}* Unwrap() {
{{- if .Result }}
return &result_.value();
{{- else }}
ZX_ASSERT(ok());
return &decoded_.Value()->body;
{{- end }}
}
const {{ .WireResultBase }}* Unwrap() const {
{{- if .Result }}
return &result_.value();
{{- else }}
ZX_ASSERT(ok());
return &decoded_.Value()->body;
{{- end }}
}
{{ .WireResultBase }}& value() { return *Unwrap(); }
const {{ .WireResultBase }}& value() const { return *Unwrap(); }
{{ .WireResultBase }}* operator->() { return &value(); }
const {{ .WireResultBase }}* operator->() const { return &value(); }
{{ .WireResultBase }}& operator*() { return value(); }
const {{ .WireResultBase }}& operator*() const { return value(); }
{{- end }}
{{- if .HasResponse }}
fdf::Arena& arena() {
ZX_ASSERT(ok());
return arena_;
}
private:
::fidl::internal::DecodedValue<{{ .WireTransactionalResponse }}> decoded_;
::fdf::Arena arena_;
{{- if and .Result .HasWireResultBase }}
std::optional<{{ .WireResultBase }}> result_ = std::nullopt;
{{- end }}
{{- end }}
};
{{- end }}
{{- define "driver/Method:UnownedResult:MessagingSource" }}
{{- if .Transport.HasSyncClient }}
{{- IfdefFuchsia -}}
{{- EnsureNamespace "" }}
{{- $args := (printf "::%s::UnownedClientEnd<%s> client_end" .Transport.Namespace .Protocol) }}
{{- $args = (List $args "const ::fdf::Arena& arena") }}
{{- $args = (List $args (printf "%s* request" .WireTransactionalRequest)) }}
{{ .WireUnownedResult }}::{{ .WireUnownedResult.Self }}({{ RenderParams $args }}) {
{{/* TODO(fxbug.dev/86367): Factor out common buffer allocation logic once we have other instances
of this pattern. */ -}}
constexpr uint32_t buffer_size =
::fidl::MaxSizeInChannel<{{ .WireTransactionalRequest }}, ::fidl::MessageDirection::kSending>();
uint8_t* buffer = static_cast<uint8_t*>(arena.Allocate(buffer_size));
::fidl::unstable::UnownedEncodedMessage<{{ .WireTransactionalRequest }}, fidl::internal::DriverTransport> request_message(
buffer, buffer_size, request);
auto& outgoing = request_message.GetOutgoingMessage();
{{- if .HasResponse }}
{{ .IncomingMessageStorageForResponse }} response_storage;
::fidl::CallOptions call_options = {
.outgoing_transport_context =
::fidl::internal::OutgoingTransportContext::Create<fidl::internal::DriverTransport>(
arena.get()),
};
::fidl::unstable::DecodedMessage<{{ .WireTransactionalResponse }}> decoded{
outgoing.Call(client_end.handle(), response_storage.view(), std::move(call_options))
};
SetStatus(decoded);
if (ok()) {
decoded_ = decoded.Take();
}
arena_ = std::move(response_storage.arena);
{{- if .HasApplicationError }}
if (ok()) {
auto* raw_response = &decoded_.Value()->body;
if (raw_response->result.is_err()) {
result_ = fitx::error(raw_response->result.err());
} else if (raw_response->result.is_response()) {
result_ = fitx::ok(
{{- if .Result.ValueParameters -}}
&(raw_response->result.response())
{{- end -}}
);
}
}
{{- end }}
{{- else }}
::fidl::WriteOptions write_options = {
.outgoing_transport_context =
::fidl::internal::OutgoingTransportContext::Create<fidl::internal::DriverTransport>(
arena.get()),
};
outgoing.Write(client_end.handle(), std::move(write_options));
SetStatus(outgoing);
{{- end }}
}
{{- EndifFuchsia -}}
{{- end }}
{{- end }}