/* * GML - Go QML * * The MIT License (MIT) * * Copyright (c) 2019 Roland Singer * Copyright (c) 2019 Sebastian Borchers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ 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 #include #include #include #include #include #include #include {{/* Struct loop */ -}} {{range $struct := .Structs -}} //### //### {{$struct.Name}} //### class {{$struct.CPPBaseName}} : {{ if eq $struct.AdditionalType "ListModel"}}public GmlListModel{{else}}public QObject{{end}} { Q_OBJECT {{- /* Properties */ -}} {{- range $prop := $struct.Properties }} Q_PROPERTY({{$prop.CPPType}} {{$prop.CPPName}} READ {{$prop.CPPName}}Get WRITE {{$prop.CPPName}}Set NOTIFY {{$prop.CPPName}}Changed) {{- end}} 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 }} {{$slot.CPPRetType}} {{$slot.CPPName}}({{cppParams $slot.Params true true}}); {{- end}} {{/* Properties */ -}} public: {{- range $prop := $struct.Properties }} void {{$prop.CPPName}}Set({{$prop.CPPType}} v); {{$prop.CPPType}} {{$prop.CPPName}}Get(); {{- end}} private: {{- range $prop := $struct.Properties }} {{$prop.CPPType}} {{$prop.CPPName}}; {{- end}} signals: {{- range $prop := $struct.Properties }} void {{$prop.CPPName}}Changed({{$prop.CPPType}} v); {{- end}} private slots: {{- range $prop := $struct.Properties }} {{if not $prop.Silent}}void {{$prop.CPPName}}OnChanged();{{end}} {{- end}} }; {{- /* End of struct loop */ -}} {{- end}} #endif ` var cppSourceTmpl = template.Must(template.New("t").Funcs(tmplFuncMap).Parse(cppSourceTmplText)) // TODO: add some catch exception handlers? 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) : {{ if eq $struct.AdditionalType "ListModel"}}GmlListModel(goPtr), {{end}}goPtr(goPtr) {{- range $prop := $struct.Properties -}} , {{$prop.CPPName}}({{defaultCPPValue $prop.Type}}) {{- end}} { {{- /* Connect property changed signals */ -}} {{- range $prop := $struct.Properties }} {{if not $prop.Silent -}} QObject::connect(this, &{{$struct.CPPBaseName}}::{{$prop.CPPName}}Changed, this, &{{$struct.CPPBaseName}}::{{$prop.CPPName}}OnChanged); {{- end}} {{- end}} } {{/* 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; } {{$slot.CPPRetType}} {{$struct.CPPBaseName}}::{{$slot.CPPName}}({{cppParams $slot.Params true true}}) { {{ if $slot.NoRet -}} {{$struct.CBaseName}}_{{$slot.Name}}_cb(this->goPtr{{cppToCParams $slot.Params false}}); {{- else -}} {{$slot.CRetType}} _r = {{$struct.CBaseName}}_{{$slot.Name}}_cb(this->goPtr{{cppToCParams $slot.Params false}}); {{$slot.CPPRetType}} _r_cpp = {{cToCPPValue $slot.RetType "_r"}}; {{freeCValue $slot.RetType "_r"}} return _r_cpp; {{- end}} } {{end}} {{- /* Properties */ -}} {{- range $prop := $struct.Properties }} {{if not $prop.Silent -}} {{$struct.CBaseName}}_{{$prop.Name}}_changed_cb_t {{$struct.CBaseName}}_{{$prop.Name}}_changed_cb = NULL; void {{$struct.CBaseName}}_{{$prop.Name}}_cb_register({{$struct.CBaseName}}_{{$prop.Name}}_changed_cb_t cb) { {{$struct.CBaseName}}_{{$prop.Name}}_changed_cb = cb; } {{- end}} {{$prop.CType}} {{$struct.CBaseName}}_{{$prop.Name}}_get({{$struct.CBaseName}} c) { auto cc = ({{$struct.CPPBaseName}}*)c; {{$prop.CPPType}} v = cc->{{$prop.CPPName}}Get(); return {{cppToCValue $prop.Type "v"}}; } void {{$struct.CBaseName}}_{{$prop.Name}}_set({{$struct.CBaseName}} c, {{$prop.CType}} v) { auto cc = ({{$struct.CPPBaseName}}*)c; cc->{{$prop.CPPName}}Set({{cToCPPValue $prop.Type "v"}}); } void {{$struct.CPPBaseName}}::{{$prop.CPPName}}Set({{$prop.CPPType}} v) { {{$prop.CPPName}} = v; emit {{$prop.CPPName}}Changed(v); } {{$prop.CPPType}} {{$struct.CPPBaseName}}::{{$prop.CPPName}}Get() { return {{$prop.CPPName}}; } {{if not $prop.Silent}} void {{$struct.CPPBaseName}}::{{$prop.CPPName}}OnChanged() { {{$struct.CBaseName}}_{{$prop.Name}}_changed_cb(this->goPtr); } {{- end}} {{- end}} {{- /* End of struct loop */ -}} {{- end}} `