Started template support, writing the initial C++ template.

This commit is contained in:
Pietro Gagliardi 2018-08-08 21:31:12 -04:00
parent ecf9efd6d7
commit b3de605d50
4 changed files with 73 additions and 15 deletions

View File

@ -1,14 +1,3 @@
static HRESULT lastErrorToHRESULT(DWORD lasterr, const char *funcname)
{
HRESULT hr;
hr = E_FAIL;
if (lasterr != 0)
hr = HRESULT_FROM_WIN32(lasterr);
uiprivImplBug("error calling %s: last error %I32d\n", funcname, lasterr);
return hr;
}
@BOOL GetWindowPlacement HWND hwnd uiprivLPWWINDOWPLACEMENT wp == 0 @BOOL GetWindowPlacement HWND hwnd uiprivLPWWINDOWPLACEMENT wp == 0
$typedef const WINDOWPLACEMENT *uiprivLPCWINDOWPLACEMENT; $typedef const WINDOWPLACEMENT *uiprivLPCWINDOWPLACEMENT;
@BOOL SetWindowPlacement HWND hwnd uiprivLPCWINDOWPLACEMENT wp == 0 @BOOL SetWindowPlacement HWND hwnd uiprivLPCWINDOWPLACEMENT wp == 0

View File

@ -0,0 +1,29 @@
{{/* 8 august 2018 */}}// this file is generated by tools/hresultwrap and should NOT be modified directly
#include "uipriv_windows.hpp"
static inline HRESULT lastErrorToHRESULT(DWORD lasterr, const char *funcname)
{
HRESULT hr;
hr = E_FAIL;
if (lasterr != 0)
hr = HRESULT_FROM_WIN32(lasterr);
uiprivImplBug("error calling %s: last error %I32d\n", funcname, lasterr);
return hr;
}{{range .}}
{{$narg := len .Arg}}HRESULT {{if .CallingConvention}}{{.CallingConvention}}{{else}}WINAPI{{end}} uiprivHR{{.Name}}({{range $i, $a := .Arg}}{{$a}}{{argcomma $i $narg}}{{end}}{{if .Save}}, _Out_ {{.Ret}} *outRet{{end}})
{
{{.Ret}} xyzret;
DWORD xyzlasterr;
{{if .Save}} if (outRet == NULL)
return E_POINTER;
{{end}} SetLastError(0);
xyzret = {{.Name}}({{range $i, $a := .Arg}}{{argname $a}}{{argcomma $i $narg}}{{end}});
xyzlasterr = GetLastError();
{{if .Save}} *outRet = xyzret;
{{end}} if (xyzret != {{.Failval}})
return S_OK;
return lastErrToHRESULT(xyzlasterr, "{{.Name}}()");
}{{end}}

View File

@ -1 +1 @@
./hresultwrap funclist.textpb ./hresultwrap funclist.textpb cpp.template

View File

@ -6,13 +6,43 @@ import (
"fmt" "fmt"
"os" "os"
"io/ioutil" "io/ioutil"
"text/template"
"strings"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
) )
func argname(arg string) string {
fields := strings.Fields(arg)
last := fields[len(fields) - 1]
start := strings.LastIndexFunc(last, func(r rune) bool {
return !(r >= 'A' && r <= 'Z') &&
!(r >= 'a' && r <= 'z') &&
!(r >= '0' && r <= '9') &&
r != '_'
})
if start == -1 {
return last
}
// TODO replace + 1 with + len of that last rune
return last[start + 1:]
}
func argcomma(n, len int) string {
if n == len - 1 {
return ""
}
return ", "
}
var templateFuncs = template.FuncMap{
"argname": argname,
"argcomma": argcomma,
}
func main() { func main() {
if len(os.Args) != 2 { if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: %s funclist\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s funclist template\n", os.Args[0])
os.Exit(1) os.Exit(1)
} }
b, err := ioutil.ReadFile(os.Args[1]) b, err := ioutil.ReadFile(os.Args[1])
@ -26,5 +56,15 @@ func main() {
fmt.Fprintf(os.Stderr, "error parsing %s: %v\n", os.Args[1], err) fmt.Fprintf(os.Stderr, "error parsing %s: %v\n", os.Args[1], err)
os.Exit(1) os.Exit(1)
} }
fmt.Println(f)
tmpl, err := template.New(os.Args[2]).Funcs(templateFuncs).ParseFiles(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing %s: %v\n", os.Args[2], err)
os.Exit(1)
}
err = tmpl.Execute(os.Stdout, f.Func)
if err != nil {
fmt.Fprintf(os.Stderr, "error executing template: %v\n", err)
os.Exit(1)
}
} }