blob: a2b2d60f371384f62a136b8c631d52f92629bcd4 [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.
package codegen
const fragmentClientTmpl = `
{{- define "ClientForwardDeclaration" }}
struct AsyncEventHandlers;
{{- range .TwoWayMethods }}
class {{ .Name }}ResponseContext;
{{- end }}
class ClientImpl;
{{- end }}
{{- define "ClientDeclaration" }}
{{- $outer := . }}
struct {{ .Name }}::AsyncEventHandlers {
{{- range .Events -}}
{{- range .DocComments }}
//{{ . }}
{{- end }}
::fit::function<void (
{{- if .Response -}}
{{ .Name }}Response* msg
{{- end -}}
)> {{ .NameInLowerSnakeCase }};
{{ "" }}
{{- end }}
};
{{- range .TwoWayMethods }}
{{ "" }}
class {{ $outer.Name }}::{{ .Name }}ResponseContext : public ::fidl::internal::ResponseContext {
public:
{{ .Name }}ResponseContext();
virtual void OnReply({{ $outer.Name }}::{{ .Name }}Response* message) = 0;
private:
void OnReply(uint8_t* reply) override;
};
{{- end }}
class {{ .Name }}::ClientImpl final : private ::fidl::internal::ClientBase {
public:
{{- range .ClientMethods -}}
{{- if .HasResponse -}}
{{- range .DocComments }}
//{{ . }}
{{- end }}
{{ "" }}
// Asynchronous variant of |{{ $outer.Name }}.{{ .Name }}()|. {{ template "AsyncClientAllocationComment" . }}
::fidl::Result {{ .Name }}({{ template "ClientAsyncRequestManagedMethodArguments" . }});
{{- range .DocComments }}
//{{ . }}
{{- end }}
// Asynchronous variant of |{{ $outer.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.
::fidl::Result {{ .Name }}({{ template "ClientAsyncRequestCallerAllocateMethodArguments" . }});
{{- end }}
{{- range .DocComments }}
//{{ . }}
{{- end }}
// {{- if .HasResponse }} Synchronous variant of |{{ $outer.Name }}.{{ .Name }}()|. {{- end }}{{ template "ClientAllocationComment" . }}
{{ if .HasResponse }}ResultOf::{{ .Name }}{{ else }}::fidl::Result{{ end }} {{ .Name }}{{ if .HasResponse }}_Sync{{ end }}({{ template "SyncRequestManagedMethodArguments" . }});
{{- if or .Request .Response }}
{{ "" }}
{{- range .DocComments }}
//{{ . }}
{{- end }}
// {{- if .HasResponse }} Synchronous variant of |{{ $outer.Name }}.{{ .Name }}()|. {{- end }} Caller provides the backing storage for FIDL message via request and response buffers.
{{ if .HasResponse }}UnownedResultOf::{{ .Name }}{{ else }}::fidl::Result{{ end }} {{ .Name }}{{ if .HasResponse }}_Sync{{ end }}({{ template "SyncRequestCallerAllocateMethodArguments" . }});
{{- end }}
{{ "" }}
{{- end }}
private:
friend class ::fidl::Client<{{ .Name }}>;
explicit ClientImpl(AsyncEventHandlers handlers) : handlers_(std::move(handlers)) {}
std::optional<::fidl::UnbindInfo> DispatchEvent(fidl_incoming_msg_t* msg) override;
AsyncEventHandlers handlers_;
};
{{- end }}
{{- define "ClientDispatchDefinition" }}
std::optional<::fidl::UnbindInfo> {{ .Name }}::ClientImpl::DispatchEvent(fidl_incoming_msg_t* msg) {
fidl_message_header_t* hdr = reinterpret_cast<fidl_message_header_t*>(msg->bytes);
switch (hdr->ordinal) {
{{- range .Events }}
case {{ .OrdinalName }}:
{
const char* error_message;
zx_status_t status = fidl_decode_etc({{ .Name }}Response::Type, msg->bytes, msg->num_bytes,
msg->handles, msg->num_handles, &error_message);
if (status != ZX_OK) {
return ::fidl::UnbindInfo{::fidl::UnbindInfo::kDecodeError, status};
}
if (!handlers_.{{ .NameInLowerSnakeCase }}) {
return ::fidl::UnbindInfo{::fidl::UnbindInfo::kUnexpectedMessage, ZX_ERR_NOT_SUPPORTED};
}
handlers_.{{ .NameInLowerSnakeCase }}(
{{- if .Response -}}
reinterpret_cast<{{ .Name }}Response*>(msg->bytes)
{{- end -}}
);
break;
}
{{- end }}
default:
FidlHandleInfoCloseMany(msg->handles, msg->num_handles);
return ::fidl::UnbindInfo{::fidl::UnbindInfo::kUnexpectedMessage, ZX_ERR_NOT_SUPPORTED};
}
return {};
}
{{- end }}
`