Add get!T method to DBusAny (#27)
* Add ddbus.util.BasicTypes * Implement DBusAny.get and DBusAny.typeSig * Add unittest for DBusAny.get * Make TypeMismatchException constructor public
This commit is contained in:
parent
99cb6cb071
commit
ce7716463d
3 changed files with 153 additions and 19 deletions
|
@ -3,7 +3,10 @@ module ddbus.thin;
|
|||
|
||||
import ddbus.c_lib;
|
||||
import ddbus.conv;
|
||||
import ddbus.exception : TypeMismatchException;
|
||||
import ddbus.util;
|
||||
|
||||
import std.meta : staticIndexOf;
|
||||
import std.string;
|
||||
import std.typecons;
|
||||
import std.exception;
|
||||
|
@ -127,7 +130,7 @@ struct DBusAny {
|
|||
/// DBus type of the value (never 'v'), see typeSig!T
|
||||
int type;
|
||||
/// Child signature for Arrays & Tuples
|
||||
const(char)[] signature;
|
||||
string signature;
|
||||
/// If true, this value will get serialized as variant value, otherwise it is serialized like it wasn't in a DBusAny wrapper.
|
||||
/// Same functionality as Variant!T but with dynamic types if true.
|
||||
bool explicitVariant;
|
||||
|
@ -159,7 +162,7 @@ struct DBusAny {
|
|||
///
|
||||
DBusAny[] array;
|
||||
///
|
||||
DBusAny[] tuple;
|
||||
alias tuple = array;
|
||||
///
|
||||
DictionaryEntry!(DBusAny, DBusAny)* entry;
|
||||
///
|
||||
|
@ -167,7 +170,7 @@ struct DBusAny {
|
|||
}
|
||||
|
||||
/// Manually creates a DBusAny object using a type, signature and implicit specifier.
|
||||
this(int type, const(char)[] signature, bool explicit) {
|
||||
this(int type, string signature, bool explicit) {
|
||||
this.type = type;
|
||||
this.signature = signature;
|
||||
this.explicitVariant = explicit;
|
||||
|
@ -246,8 +249,8 @@ struct DBusAny {
|
|||
} else static if(isInputRange!T) {
|
||||
this.type = 'a';
|
||||
static assert(!is(ElementType!T == DBusAny), "Array must consist of the same type, use Variant!DBusAny or DBusAny(tuple(...)) instead");
|
||||
static assert(typeSig!(ElementType!T) != "y");
|
||||
this.signature = typeSig!(ElementType!T);
|
||||
static assert(.typeSig!(ElementType!T) != "y");
|
||||
this.signature = .typeSig!(ElementType!T);
|
||||
this.explicitVariant = false;
|
||||
foreach(elem; value)
|
||||
array ~= DBusAny(elem);
|
||||
|
@ -319,7 +322,7 @@ struct DBusAny {
|
|||
valueStr = '[' ~ array.map!(a => a.toString).join(", ") ~ ']';
|
||||
break;
|
||||
case 'r':
|
||||
valueStr = '(' ~ array.map!(a => a.toString).join(", ") ~ ')';
|
||||
valueStr = '(' ~ tuple.map!(a => a.toString).join(", ") ~ ')';
|
||||
break;
|
||||
case 'e':
|
||||
valueStr = entry.key.toString ~ ": " ~ entry.value.toString;
|
||||
|
@ -334,6 +337,71 @@ struct DBusAny {
|
|||
~ ", " ~ valueStr ~ ")";
|
||||
}
|
||||
|
||||
/++
|
||||
Get the value stored in the DBusAny object.
|
||||
|
||||
Parameters:
|
||||
T = The requested type. The currently stored value must match the
|
||||
requested type exactly.
|
||||
|
||||
Returns:
|
||||
The current value of the DBusAny object.
|
||||
|
||||
Throws:
|
||||
TypeMismatchException if the DBus type of the current value of the
|
||||
DBusAny object is not the same as the DBus type used to represent T.
|
||||
+/
|
||||
T get(T)() @property const
|
||||
if(staticIndexOf!(T, BasicTypes) >= 0)
|
||||
{
|
||||
enforce(type == typeCode!T,
|
||||
new TypeMismatchException(
|
||||
"Cannot get a " ~ T.stringof ~ " from a DBusAny with"
|
||||
~ " a value of DBus type '" ~ typeSig ~ "'.", typeCode!T, type));
|
||||
|
||||
static if(isIntegral!T) {
|
||||
enum memberName =
|
||||
(isUnsigned!T ? "uint" : "int") ~ (T.sizeof * 8).to!string;
|
||||
return __traits(getMember, this, memberName);
|
||||
} else static if(is(T == double)) {
|
||||
return float64;
|
||||
} else static if(is(T == string)) {
|
||||
return str;
|
||||
} else static if(is(T == ObjectPath)) {
|
||||
return obj;
|
||||
} else static if(is(T == bool)) {
|
||||
return boolean;
|
||||
} else {
|
||||
static assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T get(T)() @property const
|
||||
if(is(T == const(DBusAny)[]))
|
||||
{
|
||||
enforce((type == 'a' && signature != "y") || type == 'r',
|
||||
new TypeMismatchException(
|
||||
"Cannot get a " ~ T.stringof ~ " from a DBusAny with"
|
||||
~ " a value of DBus type '" ~ this.typeSig ~ "'.",
|
||||
typeCode!T, type));
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T get(T)() @property const
|
||||
if (is(T == const(ubyte)[]))
|
||||
{
|
||||
enforce(type == 'a' && signature == "y",
|
||||
new TypeMismatchException(
|
||||
"Cannot get a " ~ T.stringof ~ " from a DBusAny with"
|
||||
~ " a value of DBus type '" ~ this.typeSig ~ "'.",
|
||||
typeCode!T, type));
|
||||
|
||||
return binaryData;
|
||||
}
|
||||
|
||||
/// If the value is an array of DictionaryEntries this will return a HashMap
|
||||
DBusAny[DBusAny] toAA() {
|
||||
enforce(type == 'a' && signature && signature[0] == '{');
|
||||
|
@ -345,6 +413,27 @@ struct DBusAny {
|
|||
return aa;
|
||||
}
|
||||
|
||||
/++
|
||||
Get the DBus type signature of the value stored in the DBusAny object.
|
||||
|
||||
Returns:
|
||||
The type signature of the value stored in this DBusAny object.
|
||||
+/
|
||||
string typeSig() @property const pure nothrow @safe
|
||||
{
|
||||
if(type == 'a') {
|
||||
return "a" ~ signature;
|
||||
} else if(type == 'r') {
|
||||
return signature;
|
||||
} else if(type == 'e') {
|
||||
return () @trusted {
|
||||
return "{" ~ entry.key.signature ~ entry.value.signature ~ "}";
|
||||
} ();
|
||||
} else {
|
||||
return [ cast(char) type ];
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a basic type, a tuple or an array to the D type with type checking. Tuples can get converted to an array too.
|
||||
T to(T)() {
|
||||
static if(is(T == Variant!R, R)) {
|
||||
|
@ -457,6 +546,15 @@ unittest {
|
|||
|
||||
void test(T)(T value, DBusAny b) {
|
||||
assertEqual(DBusAny(value), b);
|
||||
|
||||
static if(is(T == Variant!R, R)) {
|
||||
static if(__traits(compiles, b.get!R))
|
||||
assertEqual(b.get!R, value.data);
|
||||
} else {
|
||||
static if(__traits(compiles, b.get!T))
|
||||
assertEqual(b.get!T, value);
|
||||
}
|
||||
|
||||
assertEqual(b.to!T, value);
|
||||
b.toString();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue