blob: a01d9a885f765e33909a348a41d82e5be661c50f [file] [log] [blame]
{{/*
// Copyright 2020 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 "Method:ClientImplAsync:MessagingHeader" }}
{{- IfdefFuchsia -}}
{{- /* Async managed flavor */}}
{{- .Docs }}
{{- if .DocComments }}
//
{{- end }}
// Asynchronous variant of |{{ .Protocol.Name }}.{{ .Name }}()|.
{{- if .Request.ClientAllocationV2.IsStack }}
// Allocates {{ .Request.ClientAllocationV2.Size }} bytes of request buffer on the stack. The callback is stored on the heap.
{{- else }}
// The request and callback are allocated on the heap.
{{- end }}
void {{ .Name }}({{ RenderParams .RequestArgs
(printf "::fidl::WireClientCallback<%s> _cb" .Marker) }});
void {{ .Name }}({{ RenderParams .RequestArgs
(printf "::fit::callback<void (%s* response)> _cb" .WireResponse) }});
{{- /* Async caller-allocate flavor */}}
{{ .Docs }}
{{- if .DocComments }}
//
{{- end }}
// Asynchronous variant of |{{ .Protocol.Name }}.{{ .Name }}()|.
// Caller provides the backing storage for FIDL message via request buffer.
// Ownership of |_context| is given unsafely to the binding until |OnError|
// or |OnReply| are called on it.
void {{ .Name }}(
{{- if .RequestArgs }}
{{ RenderParams "::fidl::BufferSpan _request_buffer"
.RequestArgs
(printf "%s* _context" .WireResponseContext) }}
{{- else }}
{{ .WireResponseContext }}* _context
{{- end -}}
);
{{- EndifFuchsia -}}
{{- end }}
{{- define "Method:ClientImplAsync:MessagingSource" }}
{{- IfdefFuchsia -}}
{{- /* Async managed flavor */}}
void {{ .Protocol.WireClientImpl.NoLeading }}::{{ .Name }}(
{{ RenderParams .RequestArgs
(printf "::fidl::WireClientCallback<%s> _cb" .Marker) }}) {
using Callback = decltype(_cb);
class ResponseContext final : public {{ .WireResponseContext }} {
public:
ResponseContext(Callback cb)
: cb_(std::move(cb)) {}
void OnResult({{ .WireUnownedResult }}& result) override {
cb_(result);
delete this;
}
private:
Callback cb_;
};
auto* _context = new ResponseContext(std::move(_cb));
{{ .WireRequest }} _request{ {{ RenderForwardParams .RequestArgs }} };
FIDL_INTERNAL_DISABLE_AUTO_VAR_INIT
::fidl::OwnedEncodedMessage<{{ .WireRequest }}> _request_message(
::fidl::internal::AllowUnownedInputRef{}, &_request);
::fidl::internal::ClientBase::SendTwoWay(_request_message.GetOutgoingMessage(), _context);
}
void {{ .Protocol.WireClientImpl.NoLeading }}::{{ .Name }}(
{{ RenderParams .RequestArgs
(printf "::fit::callback<void (%s* response)> _cb" .WireResponse) }}) {
using Callback = decltype(_cb);
class ResponseContext final : public {{ .WireResponseContext }} {
public:
ResponseContext(Callback cb)
: cb_(std::move(cb)) {}
void OnResult({{ .WireUnownedResult }}& result) override {
if (result.ok()) {
::fidl::WireResponse<{{ .Marker }}>* response = result.Unwrap();
cb_(response);
}
delete this;
}
private:
Callback cb_;
};
auto* _context = new ResponseContext(std::move(_cb));
{{ .WireRequest }} _request{ {{ RenderForwardParams .RequestArgs }} };
FIDL_INTERNAL_DISABLE_AUTO_VAR_INIT
::fidl::OwnedEncodedMessage<{{ .WireRequest }}> _request_message(
::fidl::internal::AllowUnownedInputRef{}, &_request);
::fidl::internal::ClientBase::SendTwoWay(_request_message.GetOutgoingMessage(), _context);
}
{{- /* Async caller-allocate flavor */}}
void {{ .Protocol.WireClientImpl.NoLeading }}::{{ .Name }}(
{{- if .RequestArgs }}
{{ RenderParams "::fidl::BufferSpan _request_buffer"
.RequestArgs
(printf "%s* _context" .WireResponseContext) }}
{{- else }}
{{ .WireResponseContext }}* _context
{{- end -}}) {
{{ .WireRequest }} _request{ {{ RenderForwardParams .RequestArgs }} };
{{ if .RequestArgs }}
::fidl::UnownedEncodedMessage<{{ .WireRequest }}> _request_message(
_request_buffer.data, _request_buffer.capacity, &_request);
{{- else }}
FIDL_INTERNAL_DISABLE_AUTO_VAR_INIT
::fidl::OwnedEncodedMessage<{{ .WireRequest }}> _request_message(
::fidl::internal::AllowUnownedInputRef{}, &_request);
{{- end }}
::fidl::internal::ClientBase::SendTwoWay(_request_message.GetOutgoingMessage(), _context);
}
{{- EndifFuchsia -}}
{{- end }}