|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'dart:ffi'; |
| 6 | + |
| 7 | +import 'c_bindings_generated.dart' as c; |
5 | 8 | import 'objective_c_bindings_generated.dart' as objc;
|
6 |
| -import 'internal.dart' |
7 |
| - show ObjCBlockBase, ObjCProtocolMethod, ObjCProtocolListenableMethod; |
| 9 | +import 'internal.dart' show ObjCBlockBase; |
8 | 10 |
|
9 | 11 | /// Helper class for building Objective C objects that implement protocols.
|
10 | 12 | class ObjCProtocolBuilder {
|
11 | 13 | final _builder = objc.DartProxyBuilder.new1();
|
12 | 14 |
|
13 |
| - /// Implement an ObjC protocol [method] using a Dart [function]. |
| 15 | + /// Add a method implementation to the protocol. |
| 16 | + /// |
| 17 | + /// It is not recommended to call this method directly. Instead, use the |
| 18 | + /// implement methods on [ObjCProtocolMethod] and its subclasses. |
| 19 | + void implementMethod(Pointer<c.ObjCSelector> sel, |
| 20 | + objc.NSMethodSignature signature, ObjCBlockBase block) => |
| 21 | + _builder.implementMethod_withSignature_andBlock_( |
| 22 | + sel, signature, block.pointer.cast()); |
| 23 | + |
| 24 | + /// Builds the object. |
| 25 | + /// |
| 26 | + /// This can be called multiple times to construct multiple object instances |
| 27 | + /// that all implement the same protocol methods using the same functions. |
| 28 | + objc.DartProxy build() => objc.DartProxy.newFromBuilder_(_builder); |
| 29 | +} |
| 30 | + |
| 31 | +/// A method in an ObjC protocol. |
| 32 | +/// |
| 33 | +/// Do not try to construct this class directly. The recommended way of getting |
| 34 | +/// a method object is to use ffigen to generate bindings for the protocol you |
| 35 | +/// want to implement. The generated bindings will include a |
| 36 | +/// [ObjCProtocolMethod] for each method of the protocol. |
| 37 | +class ObjCProtocolMethod<T extends Function> { |
| 38 | + final Pointer<c.ObjCSelector> _sel; |
| 39 | + final objc.NSMethodSignature _signature; |
| 40 | + final ObjCBlockBase Function(T) _createBlock; |
| 41 | + |
| 42 | + /// Only for use by ffigen bindings. |
| 43 | + ObjCProtocolMethod(this._sel, this._signature, this._createBlock); |
| 44 | + |
| 45 | + /// Implement this method on the protocol [builder] using a Dart [function]. |
14 | 46 | ///
|
15 | 47 | /// The implemented method must be invoked by ObjC code running on the same
|
16 | 48 | /// thread as the isolate that called [implementMethod]. Invoking the method
|
17 | 49 | /// on the wrong thread will result in a crash.
|
18 |
| - /// |
19 |
| - /// The recommended way of getting the [method] object is to use ffigen to |
20 |
| - /// generate bindings for the protocol you want to implement. The generated |
21 |
| - /// bindings will include a [ObjCProtocolMethod] for each method of the |
22 |
| - /// protocol. |
23 |
| - void implementMethod(ObjCProtocolMethod method, Function? function) => |
24 |
| - _implement(method, function, method.createBlock); |
25 |
| - |
26 |
| - /// Implement an ObjC protocol [method] as a listener using a Dart [function]. |
| 50 | + void implement(ObjCProtocolBuilder builder, T? function) { |
| 51 | + if (function != null) { |
| 52 | + builder.implementMethod(_sel, _signature, _createBlock(function)); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// A method in an ObjC protocol that can be implemented as a listener. |
| 58 | +/// |
| 59 | +/// Do not try to construct this class directly. The recommended way of getting |
| 60 | +/// a method object is to use ffigen to generate bindings for the protocol you |
| 61 | +/// want to implement. The generated bindings will include a |
| 62 | +/// [ObjCProtocolMethod] for each method of the protocol. |
| 63 | +class ObjCProtocolListenableMethod<T extends Function> |
| 64 | + extends ObjCProtocolMethod<T> { |
| 65 | + final ObjCBlockBase Function(T) _createListenerBlock; |
| 66 | + |
| 67 | + /// Only for use by ffigen bindings. |
| 68 | + ObjCProtocolListenableMethod(super._sel, super._signature, super._createBlock, |
| 69 | + this._createListenerBlock); |
| 70 | + |
| 71 | + /// Implement this method on the protocol [builder] as a listener using a Dart |
| 72 | + /// [function]. |
27 | 73 | ///
|
28 | 74 | /// This is based on FFI's NativeCallable.listener, and has the same
|
29 | 75 | /// capabilities and limitations. This method can be invoked by ObjC from any
|
30 | 76 | /// thread, but only supports void functions, and is not run synchronously.
|
31 | 77 | /// See NativeCallable.listener for more details.
|
32 |
| - /// |
33 |
| - /// The recommended way of getting the [method] object is to use ffigen to |
34 |
| - /// generate bindings for the protocol you want to implement. The generated |
35 |
| - /// bindings will include a [ObjCProtocolMethod] for each method of the |
36 |
| - /// protocol. If that method can be implemented as a listener, the [method] |
37 |
| - /// object will be a [ObjCProtocolListenableMethod]. |
38 |
| - void implementMethodAsListener( |
39 |
| - ObjCProtocolListenableMethod method, Function? function) => |
40 |
| - _implement(method, function, method.createListenerBlock); |
41 |
| - |
42 |
| - /// Builds the object. |
43 |
| - /// |
44 |
| - /// This can be called multiple times to construct multiple object instances |
45 |
| - /// that all implement the same protocol methods using the same functions. |
46 |
| - objc.DartProxy build() => objc.DartProxy.newFromBuilder_(_builder); |
47 |
| - |
48 |
| - void _implement(ObjCProtocolMethod method, Function? function, |
49 |
| - ObjCBlockBase Function(Function) blockMaker) { |
| 78 | + void implementAsListener(ObjCProtocolBuilder builder, T? function) { |
50 | 79 | if (function != null) {
|
51 |
| - assert(method.isCorrectFunctionType(function)); |
52 |
| - _builder.implementMethod_withSignature_andBlock_( |
53 |
| - method.sel, method.signature, blockMaker(function).pointer.cast()); |
| 80 | + builder.implementMethod(_sel, _signature, _createListenerBlock(function)); |
54 | 81 | }
|
55 | 82 | }
|
56 | 83 | }
|
0 commit comments