blob: 49144c01308a26f0a137eac6542466d0892c91c2 [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");
{{Macro "CallImport" $}} {{/* TODO: Call this between all API reads and writes*/}}
do {
{{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++ logic to call the imported (real driver) function.
-------------------------------------------------------------------------------
*/}}
{{define "CallImport"}}
{{AssertType $ "Function"}}
{{$name := Macro "CmdName" $}}
{{$args := Macro "C++.CallArguments" $}}
{{$synthetic := GetAnnotation $ "synthetic"}}
{{if not (IsVoid $.Return.Type)}}
{{if $synthetic}}
{{Macro "C++.ReturnType" $}} result = {{Macro "C++.Null" (TypeOf $.Return)}};
{{else}}
{{Macro "C++.ReturnType" $}} result = mImports.{{$name}}({{$args}});
{{end}}
{{else}}
{{if not $synthetic}}mImports.{{$name}}({{$args}});{{end}}
{{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}}
{{/*
-------------------------------------------------------------------------------
Emits the logic to execute the given statement without mutating outputs.
-------------------------------------------------------------------------------
*/}}
{{define "Statement"}}
{{ if IsAssign $}}{{Macro "C++.Statement.Default" $}}
{{else if IsMapAssign $}}{{Macro "C++.Statement.Default" $}}
{{else if IsReturn $}}break;
{{else }}{{Macro "C++.Statement.Default" $}}
{{end}}
{{end}}