|
1 | 1 | import 'dart:ffi'; |
| 2 | + |
2 | 3 | import 'package:ffi/ffi.dart' show allocate, free; |
3 | 4 |
|
| 5 | +import '../modelinfo/entity_definition.dart'; |
4 | 6 | import '../store.dart'; |
5 | 7 | import 'bindings.dart'; |
6 | | -import '../modelinfo/entity_definition.dart'; |
| 8 | + |
| 9 | +// ignore_for_file: public_member_api_docs |
7 | 10 |
|
8 | 11 | /// This file implements C call forwarding using a trampoline approach. |
9 | 12 | /// |
10 | | -/// When you want to pass a dart callback to a C function you cannot use lambdas and instead the callback must be |
11 | | -/// a static function, otherwise `Pointer.fromFunction()` called with your function won't compile. |
12 | | -/// Since static functions don't have any state, you must either rely on a global state or use a "userData" pointer |
13 | | -/// pass-through functionality provided by a C function. |
| 13 | +/// When you want to pass a dart callback to a C function you cannot use lambdas |
| 14 | +/// and instead the callback must be a static function, otherwise |
| 15 | +/// [Pointer.fromFunction()] called with your function won't compile. |
| 16 | +/// Since static functions don't have any state, you must either rely on a |
| 17 | +/// global state or use a "userData" pointer pass-through functionality provided |
| 18 | +/// by a C function. |
14 | 19 | /// |
15 | | -/// The DataVisitor class tries to alleviate the burden of managing this and instead allows using lambdas from |
16 | | -/// user-code, internally mapping the C calls to the appropriate lambda. |
| 20 | +/// The DataVisitor class tries to alleviate the burden of managing this and |
| 21 | +/// instead allows using lambdas from user-code, internally mapping the C calls |
| 22 | +/// to the appropriate lambda. |
17 | 23 | /// |
18 | 24 | /// Sample usage: |
19 | 25 | /// final results = <T>[]; |
20 | 26 | /// final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) { |
21 | 27 | /// final bytes = dataPtr.asTypedList(length); |
22 | 28 | /// results.add(_fbManager.unmarshal(bytes)); |
23 | | -/// return true; // return value usually indicates to the C function whether it should continue. |
| 29 | +/// return true; // return value usually indicates whether to continue. |
24 | 30 | /// }); |
25 | 31 | /// |
26 | | -/// final err = bindings.obx_query_visit(_cQuery, visitor.fn, visitor.userData, offset, limit); |
27 | | -/// visitor.close(); // make sure to close the visitor, unregistering the callback it from the forwarder |
| 32 | +/// final err = bindings.obx_query_visit(_cQuery, visitor.fn, |
| 33 | +/// visitor.userData, offset, limit); |
| 34 | +/// visitor.close(); // make sure to close the visitor |
28 | 35 | /// checkObx(err); |
29 | 36 |
|
30 | 37 | int _lastId = 0; |
31 | 38 | final _callbacks = <int, bool Function(Pointer<Uint8> dataPtr, int length)>{}; |
32 | 39 |
|
33 | | -// called from C, forwards calls to the actual callback registered at the given ID |
| 40 | +// Called from C, forwards to the actual callback registered at the given ID. |
34 | 41 | int _forwarder(Pointer<Void> callbackId, Pointer<Void> dataPtr, int size) { |
35 | 42 | if (callbackId == null || callbackId.address == 0) { |
36 | 43 | throw Exception( |
|
0 commit comments