Future<int> onRun(Foo foo);
}
-typedef InstanceBuilder = ServiceBase Function(String sender, String instance);
+typedef ServiceBuilder = ServiceBase Function(String sender, String instance);
class Runnable extends StubBase {
- Runnable({required InstanceBuilder instanceBuilder})
- : _instanceBuilder = instanceBuilder,
+ Runnable({required ServiceBuilder serviceBuilder})
+ : _serviceBuilder = serviceBuilder,
super('Runnable');
final List<ServiceBase> services = <ServiceBase>[];
final Map<int, dynamic> _methodHandlers = <int, dynamic>{};
- final InstanceBuilder _instanceBuilder;
+ final ServiceBuilder _serviceBuilder;
}
```
GenDelegateId(stream);
GenMethodId(stream);
GenStructures(stream);
- GenOnDisconnected(stream);
GenInterfaces(stream);
}
void DartProxyGen::OnFiniGen(std::ofstream& stream) {}
-void DartProxyGen::GenOnDisconnected(std::ofstream& stream) {
- stream << CB_ON_DISCONNECTED;
-}
-
void DartProxyGen::GenInterfaces(std::ofstream& stream) {
for (auto& i : GetDocument().GetBlocks()) {
if (i->GetType() != Block::TYPE_INTERFACE)
code += ReplaceAll(CB_METHOD_BASE, {
{ "<METHOD_NAME>", d->GetID() },
{ "<LOWER_METHOD_NAME>", std::move(method_name) },
- { "<RETURN_TYPE>", d->GetType().ToString() },
+ { "<RETURN_TYPE>", GenReturnType(*d) },
{ "<ARGS>", args },
+ { "<ASYNC>", GenAsync(*d) },
{ "<METHOD_PARCEL_WRITE>", GenMethodParcelWrite(*d) },
{ "<METHOD_PARCEL_READ>", GenMethodParcelRead(*d) },
});
return RemoveLine(code);
}
+std::string DartProxyGen::GenReturnType(const Declaration& decl) {
+ if (decl.GetMethodType() == Declaration::MethodType::ASYNC)
+ return "Future<" + decl.GetType().ToString() + ">";
+
+ return decl.GetType().ToString();
+}
+
+std::string DartProxyGen::GenAsync(const Declaration& decl) {
+ if (decl.GetMethodType() == Declaration::MethodType::ASYNC)
+ return "async ";
+
+ return "";
+}
+
std::string DartProxyGen::GenMethodParcelWrite(const Declaration& decl) {
std::string code;
for (const auto& p : decl.GetParameters()) {
{ "<DELEGATE_TYPE>", ConvertTypeToString(type) },
{ "<DELEGATE_NAME>", p->GetID() }
});
- code += NLine(1);
} else if (type.IsUserDefinedType()) {
code += ReplaceAll(CB_USER_DEFINED_PARCEL_WRITE, {
{ "<PARCEL>", "parcel" },
{ "<NAME>", p->GetID() }
});
- code += NLine(1);
} else if (type.ToString() == "list" || type.ToString() == "array") {
code += ReplaceAll(CB_LIST_PARCEL_WRITE, {
{ "<PARCEL>", "parcel" },
{ "<NAME>", p->GetID() }
});
- code += NLine(1);
} else {
code += ReplaceAll(CB_BASE_PARCEL_WRITE, {
{ "<PARCEL>", "parcel" },
{ "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
{ "<NAME>" , p->GetID() }
});
- code += NLine(1);
}
+ code += NLine(1);
auto private_sharing_code =
GetPrivateSharingString(type, "port", p->GetID());
if (!private_sharing_code.empty())
{ "<PARCEL>", "parcelReceived" },
{ "<NAME>", p->GetID() }
});
- code += NLine(1);
} else if (type.ToString() == "list" || type.ToString() == "array") {
code += ReplaceAll(CB_LIST_PARCEL_READ, {
{ "<PARCEL>", "parcelReceived" },
{ "<NAME>", p->GetID() }
});
- code += NLine(1);
} else {
code += ReplaceAll(CB_BASE_PARCEL_READ, {
{ "<PARCEL>", "parcelReceived" },
{ "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
{ "<NAME>" , p->GetID() }
});
- code += NLine(1);
}
}
+ code += NLine(1);
return code;
}
std::string GenInterfaceMethods(const Interface& iface);
std::string GenMethodParcelRead(const Declaration& decl);
std::string GenMethodInvoke(const Declaration& decl);
+ std::string GenReturnType(const Declaration& decl);
+ std::string GenAsync(const Declaration& decl);
std::string GenMethodParcelWrite(const Declaration& decl);
std::string GenMethodResultParcelRead(const Declaration& decl);
std::string GenMethodOutParamParcelRead(const Declaration& decl);
}
)__dart_cb";
-constexpr const char CB_ON_DISCONNECTED[] =
-R"__dart_cb(
-/// Called when the disconnected event is received on rpc port.
-///
-/// This is used by the connect() of interfaces.
-typedef OnDisconnected = void Function();
-)__dart_cb";
-
/**
* <INTERFACE_NAME> The name of the interface.
*/
constexpr const char CB_METHOD_BASE[] =
R"__dart_cb(
/// This method is used to send '<METHOD_NAME>' request to the stub app.
-Future<<RETURN_TYPE>> <LOWER_METHOD_NAME>(<ARGS>) async {
+<RETURN_TYPE> <LOWER_METHOD_NAME>(<ARGS>) <ASYNC>{
<METHOD_PARCEL_WRITE>
<METHOD_PARCEL_READ>
}
/// Invokes the delegate method of the client.
Future<void> invoke(<DELEGATE_PARAMS>) async {
if (_port == null) {
- throw Exception('NotConnectedSocketException');
+ throw StateError('Must be connected first');
}
if (once && _wasInvoked) {
- throw Exception('InvalidCallbackException');
+ throw Exception('Can be invoked only once');
}
final Parcel parcel = Parcel();
constexpr const char CB_INTERFACE_BASE[] =
R"__dart_cb(
/// This is used when creating a service instance.
-typedef InstanceBuilder = ServiceBase Function(String sender, String instance);
+typedef ServiceBuilder = ServiceBase Function(String sender, String instance);
/// [<INTERFACE_NAME>] class for RPC.
class <INTERFACE_NAME> extends StubBase {
/// Constructor for this class.
- <INTERFACE_NAME>({required InstanceBuilder instanceBuilder})
- : _instanceBuilder = instanceBuilder,
+ <INTERFACE_NAME>({required ServiceBuilder serviceBuilder})
+ : _serviceBuilder = serviceBuilder,
super('<INTERFACE_NAME>') {
<CTOR>
}
/// The indexable collection of [ServiceBase] class.
final List<ServiceBase> services = <ServiceBase>[];
final Map<int, dynamic> _methodHandlers = <int, dynamic>{};
- final InstanceBuilder _instanceBuilder;
+ final ServiceBuilder _serviceBuilder;
@override
@visibleForOverriding
@nonVirtual
Future<void> onConnectedEvent(String sender, String instance) async {
- final ServiceBase service = _instanceBuilder(sender, instance);
+ final ServiceBase service = _serviceBuilder(sender, instance);
service._port = getPort(instance, PortType.callback);
await service.onCreate();
services.add(service);