Add handy utilities

This commit is contained in:
Tristan Hume 2015-04-26 19:31:39 -04:00
parent a2ac56b2ef
commit fd19791f8d
5 changed files with 124 additions and 2 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@ docs.json
__dummy.html
*.o
*.obj
ddbus
/ddbus
__test__library__

View file

@ -7,5 +7,6 @@
"authors": ["Tristan Hume"],
"lflags": ["-ldbus-1"],
"dependencies": {
"dunit": "~>1.0.10"
}
}

View file

@ -1,4 +1,6 @@
{
"fileVersion": 1,
"versions": {}
"versions": {
"dunit": "1.0.10"
}
}

16
source/ddbus/thin.d Normal file
View file

@ -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;
}

102
source/ddbus/util.d Normal file
View file

@ -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");
}