This commit is contained in:
Chris Josten 2021-01-22 00:30:12 +01:00
commit 95a29d0578
5 changed files with 114 additions and 55 deletions

View file

@ -54,10 +54,11 @@ template allCanDBus(TS...) {
AliasSeq of all basic types in terms of the DBus typesystem
+/
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,
alias BasicTypes = AliasSeq!(bool, ubyte, short, ushort, int, uint, long, ulong,
double, string, ObjectPath, InterfaceName, BusName, FileDescriptor);
template basicDBus(T) {
template basicDBus(U) {
alias T = Unqual!U;
static if (staticIndexOf!(T, BasicTypes) >= 0) {
enum basicDBus = true;
} else static if (is(T B == enum)) {
@ -70,7 +71,8 @@ template basicDBus(T) {
}
}
template canDBus(T) {
template canDBus(U) {
alias T = Unqual!U;
static if (basicDBus!T || is(T == DBusAny)) {
enum canDBus = true;
} else static if (isInstanceOf!(Variant, T)) {
@ -107,9 +109,10 @@ unittest {
(canDBus!FileDescriptor).assertTrue();
}
string typeSig(T)()
if (canDBus!T) {
static if (is(T == byte)) {
string typeSig(U)()
if (canDBus!U) {
alias T = Unqual!U;
static if (is(T == ubyte)) {
return "y";
} else static if (is(T == bool)) {
return "b";
@ -208,6 +211,33 @@ int typeCode(T)()
return 'e';
}
/**
Params:
type = the type code of a type (first character in a type string)
Returns: true if the given type is an integer.
*/
bool dbusIsIntegral(int type) @property {
return type == 'y' || type == 'n' || type == 'q' || type == 'i' || type == 'u' || type == 'x' || type == 't';
}
/**
Params:
type = the type code of a type (first character in a type string)
Returns: true if the given type is a floating point value.
*/
bool dbusIsFloating(int type) @property {
return type == 'd';
}
/**
Params:
type = the type code of a type (first character in a type string)
Returns: true if the given type is an integer or a floating point value.
*/
bool dbusIsNumeric(int type) @property {
return dbusIsIntegral(type) || dbusIsFloating(type);
}
unittest {
import dunit.toolkit;
@ -219,16 +249,20 @@ unittest {
typeSig!int().assertEqual("i");
typeSig!bool().assertEqual("b");
typeSig!string().assertEqual("s");
typeSig!InterfaceName().assertEqual("s");
typeSig!(immutable(InterfaceName))().assertEqual("s");
typeSig!ObjectPath().assertEqual("o");
typeSig!(immutable(ObjectPath))().assertEqual("o");
typeSig!(Variant!int)().assertEqual("v");
typeSig!FileDescriptor().assertEqual("h");
// enums
enum E : byte {
enum E : ubyte {
a,
b,
c
}
typeSig!E().assertEqual(typeSig!byte());
typeSig!E().assertEqual(typeSig!ubyte());
enum U : string {
One = "One",
Two = "Two"
@ -267,7 +301,7 @@ unittest {
// arrays
typeSig!(int[]).assertEqual("ai");
typeSig!(Variant!int[]).assertEqual("av");
typeSig!(Tuple!(byte)[][]).assertEqual("aa(y)");
typeSig!(Tuple!(ubyte)[][]).assertEqual("aa(y)");
// dictionaries
typeSig!(int[string]).assertEqual("a{si}");
typeSig!(DictionaryEntry!(string, int)[]).assertEqual("a{si}");