From fd19791f8dc609c06a0d41f098205bdd27dbe1b7 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Sun, 26 Apr 2015 19:31:39 -0400 Subject: [PATCH] Add handy utilities --- .gitignore | 3 +- dub.json | 1 + dub.selections.json | 4 +- source/ddbus/thin.d | 16 +++++++ source/ddbus/util.d | 102 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 source/ddbus/thin.d create mode 100644 source/ddbus/util.d diff --git a/.gitignore b/.gitignore index b3f9a48..ed1c4c5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ docs.json __dummy.html *.o *.obj -ddbus +/ddbus +__test__library__ diff --git a/dub.json b/dub.json index c29dae0..808df83 100644 --- a/dub.json +++ b/dub.json @@ -7,5 +7,6 @@ "authors": ["Tristan Hume"], "lflags": ["-ldbus-1"], "dependencies": { + "dunit": "~>1.0.10" } } diff --git a/dub.selections.json b/dub.selections.json index 22b6c56..a115f1d 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -1,4 +1,6 @@ { "fileVersion": 1, - "versions": {} + "versions": { + "dunit": "1.0.10" + } } \ No newline at end of file diff --git a/source/ddbus/thin.d b/source/ddbus/thin.d new file mode 100644 index 0000000..2e56e68 --- /dev/null +++ b/source/ddbus/thin.d @@ -0,0 +1,16 @@ +module ddbus.thin; + +import ddbus.c_lib; +import std.string; + +struct Message { + this(string dest, string path, string iface, string method) { + msg = dbus_message_new_method_call(dest.toStringz(), path.toStringz(), iface.toStringz(), method.toStringz()); + } + + ~this() { + dbus_message_unref(msg); + } + + DBusMessage *msg; +} diff --git a/source/ddbus/util.d b/source/ddbus/util.d new file mode 100644 index 0000000..29ccb12 --- /dev/null +++ b/source/ddbus/util.d @@ -0,0 +1,102 @@ +module ddbus.util; +import std.typecons; +import std.range; + +template allCanDBus(TS...) { + static if (TS.length == 0) { + enum allCanDBus = true; + } else static if(!canDBus!(TS[0])) { + enum allCanDBus = false; + } else { + enum allCanDBus = allCanDBus!(TS[1..$]); + } +} + +template canDBus(T) { + static if(is(T == byte) || is(T == short) || is (T == ushort) || is (T == int) + || is (T == uint) || is (T == long) || is (T == ulong) + || is (T == double) || is (T == string) || is(T == bool)) { + enum canDBus = true; + + } else static if(isTuple!T) { + enum canDBus = allCanDBus!(T.Types); + } else static if(isInputRange!T) { + enum canDBus = canDBus!(ElementType!T); + } else { + enum canDBus = false; + } +} +unittest { + import dunit.toolkit; + (canDBus!int).assertTrue(); + (canDBus!(int[])).assertTrue(); + (allCanDBus!(int,string,bool)).assertTrue(); + (canDBus!(Tuple!(int[],bool))).assertTrue(); + (canDBus!(Tuple!(int[],int[string]))).assertFalse(); + (canDBus!(int[string])).assertFalse(); +} + +string typeSig(T)() if(canDBus!T) { + static if(is(T == byte)) { + return "y"; + } else static if(is(T == bool)) { + return "b"; + } else static if(is(T == short)) { + return "n"; + } else static if(is(T == ushort)) { + return "q"; + } else static if(is(T == int)) { + return "i"; + } else static if(is(T == uint)) { + return "u"; + } else static if(is(T == long)) { + return "x"; + } else static if(is(T == ulong)) { + return "t"; + } else static if(is(T == double)) { + return "d"; + } else static if(is(T == string)) { + return "s"; + } else static if(isTuple!T) { + string sig = "("; + foreach(i, S; T.Types) { + sig ~= typeSig!S(); + } + sig ~= ")"; + return sig; + } else static if(isInputRange!T) { + return "a" ~ typeSig!(ElementType!T)(); + } +} + +string typeSigAll(TS...)() if(allCanDBus!TS) { + string sig = ""; + foreach(i,T; TS) { + sig ~= typeSig!T(); + } + return sig; +} + +unittest { + import dunit.toolkit; + // basics + typeSig!int().assertEqual("i"); + typeSig!bool().assertEqual("b"); + typeSig!string().assertEqual("s"); + // structs + typeSig!(Tuple!(int,string,string)).assertEqual("(iss)"); + typeSig!(Tuple!(int,string,Tuple!(int,"k",double,"x"))).assertEqual("(is(id))"); + // arrays + typeSig!(int[]).assertEqual("ai"); + typeSig!(Tuple!(byte)[][]).assertEqual("aa(y)"); + // multiple arguments + typeSigAll!(int,bool).assertEqual("ib"); + // ctfe-capable + static string sig = typeSig!ulong(); + sig.assertEqual("t"); + static string sig2 = typeSig!(Tuple!(int,string,string)); + sig2.assertEqual("(iss)"); + static string sig3 = typeSigAll!(int,string,string); + sig3.assertEqual("iss"); +} +