Skip to content

Commit 713d59a

Browse files
author
Ivan Dlugos
committed
DataVisitor docs and minor formatting changes
1 parent 3cf705d commit 713d59a

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

lib/src/bindings/data_visitor.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ import 'dart:ffi';
22
import 'signatures.dart';
33
import "package:ffi/ffi.dart" show allocate, free;
44

5+
/// This file implements C call forwarding using a trampoline approach.
6+
///
7+
/// When you want to pass a dart callback to a C function you cannot use lambdas and instead the callback must be
8+
/// a static function, otherwise `Pointer.fromFunction()` called with your function won't compile.
9+
/// Since static functions don't have any state, you must either rely on a global state or use a "userData" pointer
10+
/// pass-through functionality provided by a C function.
11+
///
12+
/// The DataVisitor class tries to alleviate the burden of managing this and instead allows using lambdas from
13+
/// user-code, internally mapping the C calls to the appropriate lambda.
14+
///
15+
/// Sample usage:
16+
/// final results = <T>[];
17+
/// final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) {
18+
/// final bytes = dataPtr.asTypedList(length);
19+
/// results.add(_fbManager.unmarshal(bytes));
20+
/// return true; // return value usually indicates to the C function whether it should continue.
21+
/// });
22+
///
23+
/// final err = bindings.obx_query_visit(_cQuery, visitor.fn, visitor.userData, offset, limit);
24+
/// visitor.close(); // make sure to close the visitor, unregistering the callback it from the forwarder
25+
/// checkObx(err);
26+
527
int _lastId = 0;
628
final _callbacks = <int, bool Function(Pointer<Uint8> dataPtr, int length)>{};
729

lib/src/box.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,29 @@ class Box<T> {
172172
} finally {
173173
bindings.obx_bytes_array_free(bytesArray);
174174
}
175-
}
176-
177-
final results = <T>[];
178-
final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) {
179-
if (dataPtr == null || dataPtr.address == 0 || length == 0) {
180-
if (allowMissing) {
181-
results.add(null);
182-
return true;
183-
} else {
184-
throw Exception('Object not found');
175+
} else {
176+
final results = <T>[];
177+
final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) {
178+
if (dataPtr == null || dataPtr.address == 0 || length == 0) {
179+
if (allowMissing) {
180+
results.add(null);
181+
return true;
182+
} else {
183+
throw Exception('Object not found');
184+
}
185185
}
186-
}
187-
final bytes = dataPtr.asTypedList(length);
188-
results.add(_fbManager.unmarshal(bytes));
189-
return true;
190-
});
186+
final bytes = dataPtr.asTypedList(length);
187+
results.add(_fbManager.unmarshal(bytes));
188+
return true;
189+
});
191190

192-
try {
193-
cVisit(visitor);
194-
} finally {
195-
visitor.close();
191+
try {
192+
cVisit(visitor);
193+
} finally {
194+
visitor.close();
195+
}
196+
return results;
196197
}
197-
return results;
198198
});
199199
}
200200

0 commit comments

Comments
 (0)