safer API with ObjectPath, InterfaceName, BusName

the busName and interfacePath types transparently cast down to a string,
(but not the other way around) so they are a perfect upgrade to
 type-safety. The only downside to them is that templates can often get
them wrong. std.conv.to will return with a cast(T) prefix, which could
slightly break existing code.

I think the type-safety advantages are outweighing this corner case
though. The current API has been fully upgraded and examples still run
and work.
This commit is contained in:
WebFreak001 2019-03-17 10:33:55 +01:00
parent c4de569809
commit db86451c7f
9 changed files with 206 additions and 76 deletions

View file

@ -55,7 +55,7 @@ template allCanDBus(TS...) {
+/
package // Don't add to the API yet, 'cause I intend to move it later
alias BasicTypes = AliasSeq!(bool, byte, short, ushort, int, uint, long, ulong,
double, string, ObjectPath);
double, string, ObjectPath, InterfaceName, BusName);
template basicDBus(T) {
static if (staticIndexOf!(T, BasicTypes) >= 0) {
@ -126,7 +126,7 @@ string typeSig(T)()
return "t";
} else static if (is(T == double)) {
return "d";
} else static if (is(T == string)) {
} else static if (is(T == string) || is(T == InterfaceName) || is(T == BusName)) {
return "s";
} else static if (is(T == ObjectPath)) {
return "o";
@ -208,6 +208,10 @@ int typeCode(T)()
unittest {
import dunit.toolkit;
static assert(canDBus!ObjectPath);
static assert(canDBus!InterfaceName);
static assert(canDBus!BusName);
// basics
typeSig!int().assertEqual("i");
typeSig!bool().assertEqual("b");
@ -276,8 +280,8 @@ unittest {
sig.assertEqual("t");
static string sig2 = typeSig!(Tuple!(int, string, string));
sig2.assertEqual("(iss)");
static string sig3 = typeSigAll!(int, string, string);
sig3.assertEqual("iss");
static string sig3 = typeSigAll!(int, string, InterfaceName, BusName);
sig3.assertEqual("isss");
}
private template AllowedFieldTypes(S)