[xsd-users] Re: cxx-tree --variant-type= ?

Mario Lang mlang at delysid.org
Tue Dec 16 16:15:00 EST 2014


Boris Kolpackov <boris at codesynthesis.com> writes:

> Mario Lang <mlang at delysid.org> writes:
>
>> For my MusicXML library, I now rewrote my two --ordered-type(s) with
>> boost::variant as a --custom-type.  I find this much more convenient to
>> use on the client side, especially since container manipulation (insert,
>> push_back, push_front, etc.) functions do not have to be manually implemented.  As
>> an added benefit, I get a compile-time checked visitor interface.  The
>> transformation was quite mechanical though:
>> 
>> 1. Remove all direct sequence accessors.
>> 2. Redefine content_oder_ and its accessors as variant_sequence, a
>> std::vector<boost::variant<...>>.
>> 3. Write a serialization_visitor to handle serialization from a
>> container of variants.  Something like this:
>> 
>> class serialization_visitor : public boost::static_visitor<void> {
>>   ::xercesc::DOMElement &e;
>> 
>> public:
>>   serialization_visitor(::xercesc::DOMElement &e) : e(e) {}
>> 
>>   void operator()(const ::musicxml::note &x) const {
>>     ::xsd::cxx::xml::dom::create_element("note", e) << x;
>>   }
>>   void operator()(const ::musicxml::backup &x) const {
>>     ::xsd::cxx::xml::dom::create_element("backup", e) << x;
>>   }
>>   void operator()(const ::musicxml::forward &x) const {
>>     ::xsd::cxx::xml::dom::create_element("forward", e) << x;
>>   }
>>   //...
>> };
>> 
>> 4. Rewrite the parsing code to *not* use element_traits::create,
>> instead, push_back directly to variant_.
>> 
>> This works WONDERFUL.  I really love it.  I wonder, have you ever
>> considered to implement this as an alternative for --ordered-type?
>> Maybe --variant-type= ?  --variant-type=class[/accessor] maybe?
>> So the user could override the name of the variant() accessor.
>
> No, I haven't considered it. To do that would require quite a bit
> more thinking because while you only needed to handle this particular
> case, if we were to implement this in XSD, it would have to work with
> all reasonable use-cases (wildcards, mixed content, polymorphic
> types are the ones that came to mind; there are most likely others).

True.  I haven't played with wildcards and mixed content at all, but I
can immediately see how polymorphic types would be a problem for this.
OTOH, a variant is sort of a different approach to polymorphism.
In this particular case, it solves exactly the problem at hand, namely,
that the types inside of the container do not have a common base.

> At the same time it should be very easy to implement a class
> template (using variadic template arguments, if using C++11) that
> would allow you to achieve pretty much what you want:
>
> --custom-type=foo=ordered_as_variant<foo_base, bar, baz, fox>/foo_base

That, is a very neat idea!  Thanks.  I will certainly use that if I end
up having to customize more types in this way.

-- 
CYa,
  ⡍⠁⠗⠊⠕



More information about the xsd-users mailing list