Merge pull request #6 from WebFreak001/multi-return

Multi return
This commit is contained in:
Tristan Hume 2017-04-22 23:38:36 -04:00 committed by GitHub
commit 6d95527516
2 changed files with 18 additions and 3 deletions

View file

@ -99,6 +99,9 @@ class MessageRouter {
auto retMsg = call.createReturn(); auto retMsg = call.createReturn();
static if(!is(Ret == void)) { static if(!is(Ret == void)) {
Ret ret = handler(args.expand); Ret ret = handler(args.expand);
static if (is(Ret == Tuple!T, T...))
retMsg.build!T(ret.expand);
else
retMsg.build(ret); retMsg.build(ret);
} else { } else {
handler(args.expand); handler(args.expand);
@ -110,7 +113,7 @@ class MessageRouter {
static if(is(Ret==void)) { static if(is(Ret==void)) {
static string[] ret = []; static string[] ret = [];
} else { } else {
static string[] ret = [typeSig!Ret]; static string[] ret = typeSigReturn!Ret;
} }
MessageHandler handleStruct = {func: &handlerWrapper, argSig: args, retSig: ret}; MessageHandler handleStruct = {func: &handlerWrapper, argSig: args, retSig: ret};
callTable[patt] = handleStruct; callTable[patt] = handleStruct;
@ -199,12 +202,17 @@ unittest{
router.setHandler!(void,int,string)(patt,(int p, string p2) {}); router.setHandler!(void,int,string)(patt,(int p, string p2) {});
patt = MessagePattern("/root/wat","ca.thume.tester","lolwut"); patt = MessagePattern("/root/wat","ca.thume.tester","lolwut");
router.setHandler!(int,int)(patt,(int p) {return 6;}); router.setHandler!(int,int)(patt,(int p) {return 6;});
patt = MessagePattern("/root/foo","ca.thume.tester","lolwut");
router.setHandler!(Tuple!(string,string,int),int)(patt,(int p) {Tuple!(string,string,int) ret; ret[0] = "a"; ret[1] = "b"; ret[2] = p; return ret;});
patt = MessagePattern("/troll","ca.thume.tester","wow"); patt = MessagePattern("/troll","ca.thume.tester","wow");
router.setHandler!(void)(patt,{return;}); router.setHandler!(void)(patt,{return;});
// TODO: these tests rely on nondeterministic hash map ordering // TODO: these tests rely on nondeterministic hash map ordering
static string introspectResult = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> static string introspectResult = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/root"><interface name="ca.thume.test"><method name="test"><arg type="i" direction="in"/><arg type="i" direction="out"/></method></interface><interface name="ca.thume.tester"><method name="lolwut"><arg type="i" direction="in"/><arg type="s" direction="in"/></method></interface><node name="wat"/></node>`; <node name="/root"><interface name="ca.thume.test"><method name="test"><arg type="i" direction="in"/><arg type="i" direction="out"/></method></interface><interface name="ca.thume.tester"><method name="lolwut"><arg type="i" direction="in"/><arg type="s" direction="in"/></method></interface><node name="foo"/><node name="wat"/></node>`;
router.introspectXML("/root").assertEqual(introspectResult); router.introspectXML("/root").assertEqual(introspectResult);
static string introspectResult2 = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/root/foo"><interface name="ca.thume.tester"><method name="lolwut"><arg type="i" direction="in"/><arg type="s" direction="out"/><arg type="s" direction="out"/><arg type="i" direction="out"/></method></interface></node>`;
router.introspectXML("/root/foo").assertEqual(introspectResult2);
router.introspectXML("/").assertEndsWith(`<node name="/"><node name="root"/><node name="troll"/></node>`); router.introspectXML("/").assertEndsWith(`<node name="/"><node name="root"/><node name="troll"/></node>`);
} }

View file

@ -98,6 +98,13 @@ string typeSig(T)() if(canDBus!T) {
} }
} }
string[] typeSigReturn(T)() if(canDBus!T) {
static if(is(T == Tuple!TS, TS...))
return typeSigArr!TS;
else
return [typeSig!T];
}
string typeSigAll(TS...)() if(allCanDBus!TS) { string typeSigAll(TS...)() if(allCanDBus!TS) {
string sig = ""; string sig = "";
foreach(i,T; TS) { foreach(i,T; TS) {