blob: bec58dcd9e674c4c492396f446e2c0549b2b991b [file] [log] [blame]
{{/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/}}
{{/* ---- Includes ---- */}}
{{Include "api_classnames.tmpl"}}
{{Include "cpp_common.tmpl" }}
{{/* ---- Overrides ---- */}}
{{Global "C++.EnumTypeOverride" "uint32_t"}}
{{Global "C++.Statement.Override" "Statement"}}
{{$filename := print (Global "API") "_spy.h" }}
{{Global "SpyName" (print (Title (Global "API")) "Spy")}}
{{$ | Macro "Spy" | Format (Global "clang-format") | Write $filename}}
{{define "Spy"}}
{{AssertType $ "API"}}
{{Macro "C++.AOSP.Copyright"}}
{{$spyname := Global "SpyName"}}
{{$guard := print "GAPII_" (Upper (Global "API")) "_SPY_H"}}
#ifndef {{$guard}}
#define {{$guard}}
#include "{{Global "API"}}_imports.h"
#include "{{Global "API"}}_types.h"
#include "spy_base.h"
#include <gapic/log.h>
#include <memory>
#include <string>
namespace gapii
class {{$spyname}} : public SpyBase {
«public
inline void init(std::shared_ptr<gapic::Encoder> encoder);
{{range $f := AllCommands $}}
{{Macro "MethodDefinitions" $f}}
{{end}}
«protected
{{Macro "ApiClassnames.Imports"}} mImports;
// Globals
{{range $g := $.Globals}}
{{Macro "C++.Type" $g}} {{$g.Name}};
{{end}}
#include "{{Global "API"}}_state_externs.inl"
};
// Inline methods
inline void {{$spyname}}::init(std::shared_ptr<gapic::Encoder> encoder) {
SpyBase::init(encoder);
mImports.Resolve();
}
{{range $i, $f := AllCommands $}}
{{Global "HACK_FUNCTION_INDEX_HACK" $i}}
{{Macro "MethodImplementation" $f}}¶
{{end}}
»} // namespace gapii
#endif // {{$guard}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the static variable name holding the ID of the given function.
-------------------------------------------------------------------------------
*/}}
{{define "ID"}}
{{AssertType $ "Function"}}
{{Macro "CmdName" $ | SplitPascalCase | Upper | JoinWith "_"}}_ID
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a single C++ method definition for the given atom.
-------------------------------------------------------------------------------
*/}}
{{define "MethodDefinitions"}}
{{AssertType $ "Function"}}
inline {{Macro "C++.ReturnType" $}} {{Macro "CmdName" $}}({{Macro "C++.CallParameters" $}});
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the inline method body for the given atom.
-------------------------------------------------------------------------------
*/}}
{{define "MethodImplementation"}}
{{AssertType $ "Function"}}
{{$name := Macro "CmdName" $}}
{{$spyname := Global "SpyName"}}
inline {{Macro "C++.ReturnType" $}} {{$spyname}}::{{$name}}({{Macro "C++.CallParameters" $}}) {
GAPID_INFO("{{$name}}()\n");
{{if not (IsVoid $.Return.Type)}}¶
{{Macro "C++.ReturnType" $}} result = {{Macro "C++.Null" (TypeOf $.Return)}};
{{end}}
do {
{{Global "CurrentCommand" $}}
{{Macro "C++.Block" $.Block}}
} while(false);
mEncoder->Uint16({{Global "HACK_FUNCTION_INDEX_HACK"}}); // Type ID -- TODO: mEncoder->Id({{Macro "ID" $}});
encodeObservations();
{{range $p := $.FullParameters}}
{{Macro "Encode" "Parameter" $p "Type" (TypeOf $p) "Name" $p.Name}}
{{end}}
{{if not (IsVoid $.Return.Type)}}¶
return result;
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the C++ encode logic for the specified parameter.
-------------------------------------------------------------------------------
*/}}
{{define "Encode"}}
{{AssertType $.Type "Type"}}
{{AssertType $.Name "string"}}
{{AssertType $.Parameter "Parameter"}}
{{ if IsPseudonym $.Type}}{{Macro "Encode" "Type" $.Type.To "Name" $.Name "Parameter" $.Parameter}}
{{else if IsBool $.Type}}mEncoder->Bool({{$.Name}});
{{else if IsU8 $.Type}}mEncoder->Uint8({{$.Name}});
{{else if IsS8 $.Type}}mEncoder->Int8({{$.Name}});
{{else if IsU16 $.Type}}mEncoder->Uint16({{$.Name}});
{{else if IsS16 $.Type}}mEncoder->Int16({{$.Name}});
{{else if IsF32 $.Type}}mEncoder->Float32({{$.Name}});
{{else if IsU32 $.Type}}mEncoder->Uint32({{$.Name}});
{{else if IsS32 $.Type}}mEncoder->Int32({{$.Name}});
{{else if IsF64 $.Type}}mEncoder->Float64({{$.Name}});
{{else if IsU64 $.Type}}mEncoder->Uint64({{$.Name}});
{{else if IsS64 $.Type}}mEncoder->Int64({{$.Name}});
{{else if IsInt $.Type}}mEncoder->Int64({{$.Name}});
{{else if IsUint $.Type}}mEncoder->Uint64({{$.Name}});
{{else if IsString $.Type}}mEncoder->String({{$.Name}});
{{else if IsEnum $.Type}}mEncoder->Uint32(static_cast<uint32_t>({{$.Name}}));
{{else if IsPointer $.Type}}
mEncoder->Uint64(reinterpret_cast<uint64_t>({{$.Name}}));
mEncoder->Uint32(0); // PoolID
{{else if IsStaticArray $.Type}}
for (int i = 0; i < {{$.Type.Size}}; i++) {
{{Macro "Encode" "Type" $.Type.ValueType "Name" (print $.Name "[i]") "Parameter" $.Parameter}}
}
{{else if IsClass $.Type}}
mEncoder->Uint64(0); // CreatedAt
{{range $f := $.Type.Fields}}
{{Macro "Encode" "Type" $f.Type "Name" (print $.Name ".m" $f.Name) "Parameter" $.Parameter}}
{{end}}
{{else}}{{Error "Encode passed unsupported type: %s" $.Type.Name}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Override for the "C++.Statement" macro.
-------------------------------------------------------------------------------
*/}}
{{define "Statement"}}
{{ if IsReturn $}}break;
{{else if IsFence $}}{{Macro "Fence" $}}
{{else if IsCopy $}}{{Error "Copy statement found outside of a Fence"}}
{{else }}{{Macro "C++.Statement.Default" $}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
An override for the "C++.Fence" macro.
-------------------------------------------------------------------------------
*/}}
{{define "Fence"}}
{{AssertType $ "Fence"}}
{{$c := Global "CurrentCommand"}}
{{AssertType $c "Function"}}
{{if not (IsNil $.Statement)}}
{{if IsCopy $.Statement}}
{{/* Apply the fenced-copy read */}}
{{Macro "C++.Type" $.Statement.Dst}} copy__dst__ = copy({{Macro "C++.Read" $.Statement.Dst}}, {{Macro "C++.Read" $.Statement.Src}});
{{end}}
{{end}}
{{if not (GetAnnotation $c "synthetic")}}
{{if not (IsVoid $c.Return.Type)}}result = {{end}}mImports.{{Macro "CmdName" $c}}({{Macro "C++.CallArguments" $c}});
{{end}}
{{/* Perform the fenced statement */}}
{{if not (IsNil $.Statement)}}
{{if IsCopy $.Statement}}
write(copy__dst__);
{{else}}
{{Macro "C++.Statement" $.Statement}}
{{end}}
{{end}}
{{end}}