Hi Patrick,
On 01/27/2012 09:26 PM, Patrick Ohly wrote:
On Fr, 2012-01-27 at 14:55 +0100, Mikel Astiz wrote:
> +template<class V1, class V2 = append_visitor_dummy_type> struct
append_visitor : public boost::static_visitor<>
> +{
> + GVariantBuilder&builder;
> + append_visitor(GVariantBuilder&b) : builder(b) {}
> + void operator()(const V1&v) const
> + {
> + dbus_traits<V1>::append(builder, v);
> + }
> + void operator()(const V2&v) const
> + {
> + dbus_traits<V2>::append(builder, v);
> + }
> +};
I think this could be simplified to
struct append_visitor : public boost::static_visitor<>
{
GVariantBuilder&builder;
append_visitor(GVariantBuilder&b) : builder(b) {}
template<class V> void operator()(const V&v) const
{
dbus_traits<V>::append(builder, v);
}
};
That's right. No need to complicate further.
I'm wondering how it works, though. In libdbus, the iterator for
the
variant must be opened with the type of the *value* in the variant. Here
the GVariantBuilder is opened with G_VARIANT_TYPE("v"), the type of the
*variant* itself:
static std::string getType() { return "v"; }
...
static void append(GVariantBuilder&builder, const boost::variant<V>
&value)
{
g_variant_builder_open(&builder, G_VARIANT_TYPE(getType().c_str()));
boost::apply_visitor(append_visitor<V>(builder), value);
g_variant_builder_close(&builder);
}
I checked with dbus-monitor, the resulting D-Bus message has "variant
string" as type of value, as intended.
Well, I just guessed that g_variant_builder_open would accept the "v" as
well, and then tested. If you prefer we could use the type of the
variant value, which would involve opening the builder inside the
append_visitor. As you prefer.
Cheers,
Mikel