-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-bridge.py
executable file
·118 lines (98 loc) · 2.79 KB
/
generate-bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#! /usr/bin/env python3
import argparse
import builtins
import collections
import datetime
import jinja2
import json
import os
import sys
func_fields = [
'object_type',
'return',
'args',
]
parser = argparse.ArgumentParser()
parser.add_argument('-i', type=str, help='input json file', required=True)
parser.add_argument('-o', type=str, help='output c++ file', required=True)
args = parser.parse_args()
with open(args.i) as json_file:
funcs = json.load(json_file, object_pairs_hook=collections.OrderedDict)
for func in funcs.values():
for field in func.keys():
if not field in func_fields:
raise RuntimeError("unknown field %s" % field)
env = jinja2.Environment(
trim_blocks=True,
lstrip_blocks=True,
undefined = jinja2.StrictUndefined)
template = env.from_string('''
// THIS FILE IS AUTO-GENERATED. DO NOT EDIT!
// Generator: {{ generator_script }}
// Source: {{ generator_input }}
// Timestamp: {{ timestamp }}
// Copyright (c) libASPL authors
// Licensed under MIT
#include <aspl/Driver.hpp>
#include <aspl/Tracer.hpp>
#include "Bridge.hpp"
namespace aspl {
{% for func_name, func in funcs.items() %}
{% for arg_num, arg_name in enumerate(func.args) %}
{% set arg_type = func.args[arg_name] %}
{% if arg_num == 0 %}
{{ func.return }} Bridge::{{ func_name }}({{ arg_type }} {{ arg_name }},
{% elif arg_num != len(func.args) - 1 %}
{{ arg_type }} {{ arg_name }},
{% else %}
{{ arg_type }} {{ arg_name }})
{% endif %}
{% endfor %}
{
const auto driver = Driver::GetDriver(driverRef);
{% if func.return == 'OSStatus' %}
if (!driver) {
return kAudioHardwareUnspecifiedError;
}
{% else %}
if (!driver) {
return {};
}
{% endif %}
{{ func.return }} result = {};
const auto object = driver->GetContext()->Dispatcher->FindObject(objectID);
if (!object) {
driver->GetContext()->Tracer->Message(
"Bridge::{{ func_name }}() objectID=%u object not registered",
unsigned(objectID));
{% if func.return == 'OSStatus' %}
result = kAudioHardwareBadObjectError;
{% endif %}
goto end;
}
{% for arg_num, arg_name in enumerate(func.args) %}
{% if arg_num == 0 %}
{% elif arg_num == 1 %}
result = std::static_pointer_cast<{{ func.object_type }}>(object)->{{ func_name }}(
{{ arg_name }},
{% elif arg_num != len(func.args) - 1 %}
{{ arg_name }},
{% else %}
{{ arg_name }});
{% endif %}
{% endfor %}
end:
return result;
}
{% endfor %}
} // namespace aspl
'''.strip())
text = template.render(
**builtins.__dict__,
funcs=funcs,
generator_script=os.path.basename(__file__),
generator_input=os.path.basename(args.i),
timestamp=datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S %Y UTC"),
)
with open(args.o, 'w') as out:
print(text, file=out)