mirror of
https://github.com/gosticks/gml.git
synced 2025-10-16 12:05:33 +00:00
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
/*
|
|
* GML - Go QML
|
|
* Copyright (c) 2019 Roland Singer [roland.singer@deserbit.com]
|
|
* Copyright (c) 2019 Sebastian Borchers [sebastian@deserbit.com]
|
|
*/
|
|
|
|
package build
|
|
|
|
import "text/template"
|
|
|
|
var cppHeaderTmpl = template.Must(template.New("t").Funcs(tmplFuncMap).Parse(cppHeaderTmplText))
|
|
|
|
const cppHeaderTmplText = `// This file is auto-generated by gml.
|
|
#ifndef GML_GEN_CPP_{{.PackageName}}_H
|
|
#define GML_GEN_CPP_{{.PackageName}}_H
|
|
|
|
#include "../gen_c/{{.PackageName}}.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <iostream>
|
|
#include <gml.h>
|
|
|
|
#include <QObject>
|
|
#include <QByteArray>
|
|
#include <QString>
|
|
#include <QChar>
|
|
#include <QVariant>
|
|
|
|
{{/* Struct loop */ -}}
|
|
{{range $struct := .Structs -}}
|
|
//###
|
|
//### {{$struct.Name}}
|
|
//###
|
|
|
|
class {{$struct.CPPBaseName}} : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
private:
|
|
void* goPtr;
|
|
|
|
public:
|
|
{{$struct.CPPBaseName}}(void* goPtr);
|
|
|
|
{{/* Signals */ -}}
|
|
signals:
|
|
{{- range $signal := $struct.Signals }}
|
|
void {{$signal.CPPName}}({{cppParams $signal.Params true true}});
|
|
{{- end}}
|
|
|
|
{{/* Slots */ -}}
|
|
public slots:
|
|
{{- range $slot := $struct.Slots }}
|
|
void {{$slot.CPPName}}({{cppParams $slot.Params true true}});
|
|
{{end}}
|
|
};
|
|
|
|
{{- /* End of struct loop */ -}}
|
|
{{- end}}
|
|
|
|
#endif
|
|
`
|
|
|
|
var cppSourceTmpl = template.Must(template.New("t").Funcs(tmplFuncMap).Parse(cppSourceTmplText))
|
|
|
|
const cppSourceTmplText = `// This file is auto-generated by gml.
|
|
#include "{{.PackageName}}.h"
|
|
|
|
{{/* Struct loop */ -}}
|
|
{{range $struct := .Structs -}}
|
|
//###
|
|
//### {{$struct.Name}}
|
|
//###
|
|
|
|
{{$struct.CBaseName}} {{$struct.CBaseName}}_new(void* go_ptr) {
|
|
auto _vv = new {{$struct.CPPBaseName}}(go_ptr);
|
|
return (void*)_vv;
|
|
}
|
|
|
|
void {{$struct.CBaseName}}_free({{$struct.CBaseName}} _v) {
|
|
if (_v == NULL) return;
|
|
auto _vv = ({{$struct.CPPBaseName}}*)_v;
|
|
delete _vv;
|
|
_v = NULL;
|
|
}
|
|
|
|
{{$struct.CPPBaseName}}::{{$struct.CPPBaseName}}(void* goPtr) :
|
|
goPtr(goPtr)
|
|
{}
|
|
|
|
{{/* Signals */ -}}
|
|
{{- range $signal := $struct.Signals }}
|
|
void {{$struct.CBaseName}}_{{$signal.Name}}({{$struct.CBaseName}} _v{{cParams $signal.Params true false}}) {
|
|
auto _vv = ({{$struct.CPPBaseName}}*)_v;
|
|
emit _vv->{{$signal.CPPName}}({{cToCPPParams $signal.Params true}});
|
|
}
|
|
{{end}}
|
|
|
|
{{- /* Slots */ -}}
|
|
{{- range $slot := $struct.Slots }}
|
|
{{$struct.CBaseName}}_{{$slot.Name}}_cb_t {{$struct.CBaseName}}_{{$slot.Name}}_cb = NULL;
|
|
|
|
void {{$struct.CBaseName}}_{{$slot.Name}}_cb_register({{$struct.CBaseName}}_{{$slot.Name}}_cb_t cb) {
|
|
{{$struct.CBaseName}}_{{$slot.Name}}_cb = cb;
|
|
}
|
|
|
|
void {{$struct.CPPBaseName}}::{{$slot.CPPName}}({{cppParams $slot.Params true true}}) {
|
|
try {
|
|
{{$struct.CBaseName}}_{{$slot.Name}}_cb(this->goPtr);
|
|
}
|
|
catch (std::exception& e) {
|
|
std::cerr << "gml: catched slot exception: " << e.what() << std::endl;
|
|
}
|
|
catch (...) {
|
|
std::cerr << "gml: catched slot exception" << std::endl;
|
|
}
|
|
}
|
|
{{end}}
|
|
|
|
{{- /* End of struct loop */ -}}
|
|
{{- end}}
|
|
`
|