Added support for object path type

This commit is contained in:
Harry T. Vennik 2017-06-04 18:00:25 +02:00
parent 56f90644ce
commit 0439a1e8db
3 changed files with 57 additions and 3 deletions

View file

@ -30,6 +30,28 @@ T wrapErrors(T)(T delegate(DBusError *err) del) {
return ret;
}
struct ObjectPath {
private string _value;
this(string objPath) {
enforce(_isValid(objPath));
_value = objPath;
}
string toString() const {
return _value;
}
bool opEquals(const ObjectPath b) const {
return _value == b._value;
}
private static bool _isValid(string objPath) {
import std.regex : matchFirst, ctRegex;
return cast(bool) objPath.matchFirst(ctRegex!("^((/[0-9A-Za-z_]+)+|/)$"));
}
}
/// Structure allowing typeless parameters
struct DBusAny {
/// DBus type of the value (never 'v'), see typeSig!T
@ -63,6 +85,8 @@ struct DBusAny {
///
bool boolean;
///
ObjectPath obj;
///
DBusAny[] array;
///
DBusAny[] tuple;
@ -112,6 +136,9 @@ struct DBusAny {
} else static if(is(T == bool)) {
this(typeCode!bool, null, false);
boolean = cast(bool) value;
} else static if(is(T == ObjectPath)) {
this(typeCode!ObjectPath, null, false);
obj = value;
} else static if(is(T == Variant!R, R)) {
static if(is(R == DBusAny)) {
type = value.data.type;
@ -207,6 +234,9 @@ struct DBusAny {
case typeCode!string:
valueStr = '"' ~ str ~ '"';
break;
case typeCode!ObjectPath:
valueStr = '"' ~ obj.to!string ~ '"';
break;
case typeCode!bool:
valueStr = boolean ? "true" : "false";
break;
@ -285,6 +315,13 @@ struct DBusAny {
} else static if(isSomeString!T) {
if(type == 's')
return str.to!T;
else if(type == 'o')
return obj.toString();
else
throw new Exception("Can't convert type " ~ cast(char) type ~ " to " ~ T.stringof);
} else static if(is(T == ObjectPath)) {
if(type == 'o')
return obj;
else
throw new Exception("Can't convert type " ~ cast(char) type ~ " to " ~ T.stringof);
} else static if(isDynamicArray!T) {
@ -332,6 +369,8 @@ struct DBusAny {
return tuple == b.tuple;
else if(type == 's')
return str == b.str;
else if(type == 'o')
return obj == b.obj;
else if(type == 'e')
return entry == b.entry || (entry && b.entry && *entry == *b.entry);
else