From 99cb6cb071648c8ce8ffdd2b825ea71cd3e3a0e3 Mon Sep 17 00:00:00 2001 From: thaven Date: Sat, 4 Nov 2017 15:12:56 +0100 Subject: [PATCH] Allow structs with non-marshaled non-canDBus fields (#28) --- source/ddbus/conv.d | 3 ++- source/ddbus/thin.d | 7 +++++-- source/ddbus/util.d | 12 ++++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/source/ddbus/conv.d b/source/ddbus/conv.d index d3a0f6e..ebc7aa2 100644 --- a/source/ddbus/conv.d +++ b/source/ddbus/conv.d @@ -344,7 +344,8 @@ void readIterTuple(Tup)(DBusMessageIter *iter, ref Tup tuple) if(isTuple!Tup && } } -void readIterStruct(S)(DBusMessageIter *iter, ref S s) if(is(S == struct) && allCanDBus!(Fields!S)) { +void readIterStruct(S)(DBusMessageIter *iter, ref S s) if(is(S == struct) && canDBus!S) +{ foreach(index, T; Fields!S) { static if (isAllowedField!(s.tupleof[index])) { s.tupleof[index] = readIter!T(iter); diff --git a/source/ddbus/thin.d b/source/ddbus/thin.d index 2a78a2f..03f0c43 100644 --- a/source/ddbus/thin.d +++ b/source/ddbus/thin.d @@ -654,6 +654,7 @@ unittest { struct S2 { int h, i; @(Yes.DBusMarshal) int j, k; + int *p; } @dbusMarshaling(MarshalingFlag.includePrivateFields) @@ -667,10 +668,12 @@ unittest { Message msg = Message("org.example.wow", "/wut", "org.test.iface", "meth3"); + __gshared int dummy; + enum testStruct = S3( variant(5), "blah", S1(-7, 63.5, "test"), - S2(84, -123, 78, 432), + S2(84, -123, 78, 432, &dummy), 16 ); @@ -680,7 +683,7 @@ unittest { enum expectedResult = S3( variant(5), "blah", S1(int.init, 63.5, "test"), - S2(int.init, int.init, 78, 432), + S2(int.init, int.init, 78, 432, null), uint.init ); diff --git a/source/ddbus/util.d b/source/ddbus/util.d index a381ca0..d69d541 100644 --- a/source/ddbus/util.d +++ b/source/ddbus/util.d @@ -86,7 +86,7 @@ template canDBus(T) { } else static if(isAssociativeArray!T) { enum canDBus = basicDBus!(KeyType!T) && canDBus!(ValueType!T); } else static if(is(T == struct) && !isInstanceOf!(DictionaryEntry, T)) { - enum canDBus = allCanDBus!(Fields!T); + enum canDBus = allCanDBus!(AllowedFieldTypes!T); } else { enum canDBus = false; } @@ -147,7 +147,7 @@ string typeSig(T)() if(canDBus!T) { return "a{" ~ typeSig!(KeyType!T) ~ typeSig!(ValueType!T) ~ "}"; } else static if(is(T == struct)) { string sig = "("; - foreach(i, S; Fields!T) { + foreach(i, S; AllowedFieldTypes!T) { sig ~= typeSig!S(); } sig ~= ")"; @@ -241,3 +241,11 @@ unittest { sig3.assertEqual("iss"); } +private template AllowedFieldTypes(S) if (is(S == struct)) { + import ddbus.attributes : isAllowedField; + import std.meta : Filter, staticMap; + static alias TypeOf(alias sym) = typeof(sym); + + alias AllowedFieldTypes = + staticMap!(TypeOf, Filter!(isAllowedField, S.tupleof)); +}