From fafc01123d53370f7744596fadd4e77212bc55fa Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Sat, 28 Oct 2017 12:27:21 +0300 Subject: [PATCH] add dbus marshalling/unmarshalling example --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/README.md b/README.md index 2601ed4..fe48dbd 100644 --- a/README.md +++ b/README.md @@ -93,3 +93,61 @@ template Result::Content print_example(IResult r) return r.type(); } ``` + +### DBus serialization example + +This code example is copied from [queued](http://github.com/arcan1s/queued) and +uses QtDBus classes. + +```cpp +template +inline QDBusArgument &operator<<(QDBusArgument &_argument, + const Result::Result &_arg) +{ + // HACK we are using explicit cast to QString here to make sure of valid + // marshalling + _argument.beginStructure(); + switch (_arg.type()) { + case Result::Content::Value: + _argument << QString("v"); + _argument << _arg.get(); + _argument << Result::Error(); + break; + case Result::Content::Error: + _argument << QString("e"); + _argument << T(); + _argument << _arg.error(); + break; + case Result::Content::Empty: + _argument << QString("n"); + _argument << T(); + _argument << Result::Error(); + break; + } + _argument.endStructure(); + + return _argument; +}; +template +inline const QDBusArgument &operator>>(const QDBusArgument &_argument, + Result::Result &_arg) +{ + QString type; + T value; + Result::Error error; + + _argument.beginStructure(); + _argument >> type; + _argument >> value; + _argument >> error; + _argument.endStructure(); + + if (type == "v") + _arg = value; + else if (type == "e") + _arg = error; + else if (type == "n") + _arg = Result::Result(); + return _argument; +}; +```