|
1 | | -{%- macro capi_for_method(cls, method, mapping) -%} |
2 | | -{%- set overrides = mapping.options.symbol_overrides if mapping.options and mapping.options.symbol_overrides else {} -%} |
3 | | -{%- set key = ((cls.raw.qualified_name if cls.raw and cls.raw.qualified_name else cls.raw.name) ~ '::' ~ method.raw.name) -%} |
4 | | -{%- if key and overrides.get(key) -%} |
5 | | -{{ overrides.get(key) }} |
6 | | -{%- else -%} |
7 | | -native_{{ cls.raw.name | snake_case }}_{{ method.raw.name | snake_case }} |
8 | | -{%- endif -%} |
| 1 | +{%- macro render_method_call(method) -%} |
| 2 | +{%- if method.return_bridge == 'string' %} |
| 3 | + final cString = bindings.{{ method.call_symbol }}( |
| 4 | +{%- if method.needs_instance_handle %} |
| 5 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 6 | +{%- endif %} |
| 7 | +{%- for arg in method.call_args %} |
| 8 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 9 | +{%- endfor %} |
| 10 | + ); |
| 11 | + if (cString == ffi.nullptr) return ''; |
| 12 | + final value = cString.cast<pkgffi.Utf8>().toDartString(); |
| 13 | + bindings.{{ method.string_free_symbol }}(cString); |
| 14 | + return value; |
| 15 | +{%- elif method.return_bridge == 'offset' %} |
| 16 | + final nativeValue = bindings.{{ method.call_symbol }}( |
| 17 | +{%- if method.needs_instance_handle %} |
| 18 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 19 | +{%- endif %} |
| 20 | +{%- for arg in method.call_args %} |
| 21 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 22 | +{%- endfor %} |
| 23 | + ); |
| 24 | + return ui.Offset(nativeValue.x, nativeValue.y); |
| 25 | +{%- elif method.return_bridge == 'size' %} |
| 26 | + final nativeValue = bindings.{{ method.call_symbol }}( |
| 27 | +{%- if method.needs_instance_handle %} |
| 28 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 29 | +{%- endif %} |
| 30 | +{%- for arg in method.call_args %} |
| 31 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 32 | +{%- endfor %} |
| 33 | + ); |
| 34 | + return ui.Size(nativeValue.width, nativeValue.height); |
| 35 | +{%- elif method.return_bridge == 'rect' %} |
| 36 | + final nativeValue = bindings.{{ method.call_symbol }}( |
| 37 | +{%- if method.needs_instance_handle %} |
| 38 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 39 | +{%- endif %} |
| 40 | +{%- for arg in method.call_args %} |
| 41 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 42 | +{%- endfor %} |
| 43 | + ); |
| 44 | + return ui.Rect.fromLTWH(nativeValue.x, nativeValue.y, nativeValue.width, nativeValue.height); |
| 45 | +{%- elif method.return_bridge == 'void' %} |
| 46 | + bindings.{{ method.call_symbol }}( |
| 47 | +{%- if method.needs_instance_handle %} |
| 48 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 49 | +{%- endif %} |
| 50 | +{%- for arg in method.call_args %} |
| 51 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 52 | +{%- endfor %} |
| 53 | + ); |
| 54 | +{%- else %} |
| 55 | + return bindings.{{ method.call_symbol }}( |
| 56 | +{%- if method.needs_instance_handle %} |
| 57 | + _nativeHandle{% if method.call_args|length > 0 %},{% endif %} |
| 58 | +{%- endif %} |
| 59 | +{%- for arg in method.call_args %} |
| 60 | + {{ arg }}{% if not loop.last %}, {% endif %} |
| 61 | +{%- endfor %} |
| 62 | + ); |
| 63 | +{%- endif %} |
9 | 64 | {%- endmacro -%} |
10 | 65 |
|
11 | 66 | {%- macro render_class(item, mapping) -%} |
12 | | -class {{ item.name }} { |
13 | | - {{ item.name }}(this._bindings, this.handle); |
| 67 | +class {{ item.name }} with CNativeApiBindingsMixin implements NativeHandleWrapper<{{ item.handle_alias }}> { |
| 68 | + {%- if item.is_singleton %} |
| 69 | + static final {{ item.name }} instance = {{ item.name }}._(); |
| 70 | + |
| 71 | + late final {{ item.handle_alias }} _nativeHandle; |
14 | 72 |
|
15 | | - final {{ mapping.options.bindings_class }} _bindings; |
16 | | - final ffi.Pointer<ffi.Void> handle; |
| 73 | + {{ item.name }}._() { |
| 74 | + _nativeHandle = bindings.{{ item.singleton_symbol }}(); |
| 75 | + } |
| 76 | + {%- else %} |
| 77 | + final {{ item.handle_alias }} _nativeHandle; |
| 78 | + |
| 79 | + {{ item.name }}(this._nativeHandle); |
| 80 | + {%- endif %} |
| 81 | + |
| 82 | + @override |
| 83 | + {{ item.handle_alias }} get nativeHandle => _nativeHandle; |
17 | 84 |
|
18 | 85 | {%- for method in item.methods %} |
19 | | -{%- if not (method.raw and (method.raw.name == item.raw.name or method.raw.name.startswith('~'))) %} |
20 | | - {% set ret = method.return_type.mapped if method.return_type else 'void' %}{% if '::' in ret %}{% set ret = 'dynamic' %}{% endif %}{{ ret }} {{ method.name }}( |
| 86 | +{%- if not method.skip %} |
| 87 | + {%- if method.is_property %} |
| 88 | + {{ 'static ' if method.static else '' }}{{ method.api_return_type }} get {{ method.property_name }} { |
| 89 | +{{ render_method_call(method) }} |
| 90 | + } |
| 91 | + {%- else %} |
| 92 | + {{ 'static ' if method.static else '' }}{{ method.api_return_type }} {{ method.api_name }}( |
21 | 93 | {%- for param in method.params %} |
22 | | - {% set ptype = param.type.mapped %}{% if '::' in ptype %}dynamic{% else %}{{ ptype }}{% endif %} {{ param.name }}{% if not loop.last %}, {% endif %} |
| 94 | + {{ param.api_type }} {{ param.name }}{% if not loop.last %}, {% endif %} |
23 | 95 | {%- endfor %} |
24 | 96 | ) { |
25 | | - {% if method.return_type and method.return_type.kind != 'void' %}return {% endif %}_bindings.{{ capi_for_method(item, method, mapping) }}( |
26 | | - {%- if not method.static %} |
27 | | - handle{% if method.params|length > 0 %}, {% endif %} |
28 | | - {%- endif %} |
29 | | - {%- for param in method.params %} |
30 | | - {{ param.name }}{% if not loop.last %}, {% endif %} |
31 | | - {%- endfor %} |
32 | | - ); |
| 97 | +{{ render_method_call(method) }} |
33 | 98 | } |
| 99 | + {%- endif %} |
34 | 100 |
|
35 | 101 | {%- endif %} |
36 | 102 | {%- endfor %} |
| 103 | + |
| 104 | + @override |
| 105 | + void dispose() { |
| 106 | + // Generated wrappers are non-owning by default. |
| 107 | + } |
37 | 108 | } |
38 | 109 | {%- endmacro -%} |
0 commit comments