blob: b18bd5ef0ad5b131d2e15b5de292acd6faf13070 [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.
package generate
var embedded = map[string]string{
go_tmpl_file: go_tmpl,
java_tmpl_file: java_tmpl,
}
const go_tmpl_file = `go.tmpl`
const go_tmpl = `// Copyright (C) 2014 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.
{{define "Register"}} //{{.Signature}}
binary.Register(binary.ID{ {{range .ID}}{{printf "0x%2.2x" .}}, {{end}} }, &{{.Name}}{})
{{end}}
{{define "Encoder"}}
func (o {{.Name}}) Encode(e *binary.Encoder) error {
{{range .Fields}}{{encode (print "o." .Name) .Type}}{{end}}
return nil
}
{{end}}
{{define "EncodeNative"}}
if err := e.{{.Type.Method}}({{.Name}}); err != nil {
return err
}{{end}}
{{define "EncodeRemap"}}
if err := e.{{.Type.Method}}({{.Type.Native}}({{.Name}})); err != nil {
return err
}{{end}}
{{define "EncodeCodeable"}}
if err := {{.Name}}.Encode(e); err != nil {
return err
}{{end}}
{{define "EncodeObject"}}
if {{.Name}} != nil {
if err := e.Object({{.Name}}); err != nil {
return err
}
} else if err := e.Object(nil); err != nil {
return err
}{{end}}
{{define "EncodeArray"}}
if err := e.Int32(int32(len({{.Name}}))); err != nil {
return err
}
for i := range {{.Name}} {
{{encode (print .Name "[i]") .Type.SubType}}
}{{end}}
{{define "Decoder"}}
func (o *{{.Name}}) Decode(d *binary.Decoder) error {
{{range .Fields}}{{decode (print "o." .Name) .Type}}{{end}}
return nil
}{{end}}
{{define "DecodeNative"}}
if obj, err := d.{{.Type.Method}}(); err != nil {
return err
} else {
{{.Name}} = {{.Type.Name}}(obj)
}{{end}}
{{define "DecodeCodeable"}}
if err := {{.Name}}.Decode(d); err != nil {
return err
}{{end}}
{{define "DecodeObject"}}
if obj, err := d.Object(); err != nil {
return err
} else if obj != nil {
{{.Name}} = obj.({{.Type.Name}})
} else {
{{.Name}} = nil
}{{end}}
{{define "DecodeArray"}}
if count, err := d.Int32(); err != nil {
return err
} else {
{{.Name}} = make({{.Type.Name}}, count)
for i := range {{.Name}} {
{{decode (print .Name "[i]") .Type.SubType}}
}
}{{end}}
{{define "File"}}
////////////////////////////////////////////////////////////////////////////////
// Do not modify!
// Generated by {{$.Generated}}
////////////////////////////////////////////////////////////////////////////////
package {{.Package}}
import (
"android.googlesource.com/platform/tools/gpu/binary"
)
func init() {
{{range .Structs}}{{template "Register" .}}{{end}}
}
{{range .Structs}}
{{template "Encoder" .}}
{{template "Decoder" .}}
{{end}}
{{end}}
`
const java_tmpl_file = `java.tmpl`
const java_tmpl = `// Copyright (C) 2014 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.
{{define "Enum"}}{{.Name}}Enum{{end}}
{{define "ID"}}{{.Name}}ID{{end}}
{{define "IDBytes"}}{{.Name}}IDBytes{{end}}
{{define "Encoder"}}
public static void encode(Encoder e, {{class .Name}} o) throws IOException {
{{range .Fields}} {{encode (print "o." (fieldname .Name)) .Type}}
{{end}} }{{end}}
{{define "EncodeNative"}}e.{{lower .Type.Method}}({{.Name}});{{end}}
{{define "EncodeRemap"}}{{.Name}}.encode(e);{{end}}
{{define "EncodeCodeable"}}{{.Name}}.encode(e);{{end}}
{{define "EncodeObject"}}e.object({{.Name}});{{end}}
{{define "EncodeArray"}}e.int32({{.Name}}.length);
for (int i = 0; i < {{.Name}}.length; i++) {
{{encode (print .Name "[i]") .Type.SubType}}
}{{end}}
{{define "Decoder"}}
public static void decode(Decoder d, {{class .Name}} o) throws IOException {
{{range .Fields}} {{decode (print "o." (fieldname .Name)) .Type}}
{{end}} }{{end}}
{{define "DecodeNative"}}{{.Name}} = d.{{lower .Type.Method}}();{{end}}
{{define "DecodeRemap"}}{{.Name}} = {{.Type.Name}}.decode(d);{{end}}
{{define "DecodeCodeable"}}{{.Name}} = new {{.Type.Name}}(d);{{end}}
{{define "DecodeObject"}}{{.Name}} = ({{storage .Type}})d.object();{{end}}
{{define "DecodeArray"}}{{.Name}} = new {{storage .Type.SubType}}[d.int32()];
for (int i = 0; i < {{.Name}}.length; i++) {
{{decode (print .Name "[i]") .Type.SubType}}
}{{end}}
{{define "File"}}/*
* 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.
*
* THIS WILL BE REMOVED ONCE THE CODE GENERATOR IS INTEGRATED INTO THE BUILD.
*/
package {{.Package}};
import com.android.tools.rpclib.binary.BinaryObject;
import com.android.tools.rpclib.binary.BinaryObjectCreator;
import com.android.tools.rpclib.binary.Decoder;
import com.android.tools.rpclib.binary.Encoder;
import com.android.tools.rpclib.binary.ObjectTypeID;
import java.io.IOException;
class ObjectFactory {
public enum Entries implements BinaryObjectCreator {{"{"}}{{range .Structs}}
{{template "Enum" .}} {
@Override public BinaryObject create() {
return new {{class .Name}}();
}
},{{end}}
}
{{range .Structs}}
public static byte[] {{template "IDBytes" .}} = { {{range .ID}}{{toS8 .}}, {{end}}{{"}"}};{{end}}
{{range .Structs}}
public static ObjectTypeID {{template "ID" .}} = new ObjectTypeID({{template "IDBytes" .}});{{end}}
static {{"{"}}{{range .Structs}}
ObjectTypeID.register({{template "ID" .}}, Entries.{{template "Enum" .}});{{end}}
}{{range .Structs}}
{{template "Encoder" .}}
{{template "Decoder" .}}{{end}}
}
{{end}}
`