[xsd-users] Visitor pattern automatically generated/ and/or included

Boris Kolpackov boris at codesynthesis.com
Mon Jul 20 23:19:52 EDT 2009


Hi Sumant,

Sumant Tambe <sutambe at dre.vanderbilt.edu> writes:
 
> > The problem with automatically generating the Visitor mechanism is
> > that the Visitor class needs to define the visit() function for each
> > type in the object model. When you have separate compilation of
> > schemas that are included or imported, this becomes a very tricky
> > task.
>
> Can you please elaborate the difficulty here? What do you mean by separate
> compilation of schemas?

Say you have schema a.xsd which defines type A and schema b.xsd which
defines type B. The resulting visitor class for this object model would
look like this:

struct Visitor
{
  virtual void visit (A&) = 0;
  virtual void visit (B&) = 0;
};

Because when the XSD compiler is running on a.xsd it doesn't know 
anything about b.xsd (and vice versa), it is not clear when and 
how the above interface could be generated.

We could do some tricks like generating a visitor class per schema
(and, perhaps, inherit it from other visitor classes that correspond
to included schemas), but it gets quite hairy, as you can see.

 
> > So it looks like the best bet to get this automated would be to
> > define the Visitor class manually and then customize the code
> > generator in XSD to emit the accept() function implementation
> > for each object model class. If you would like, I can give you
> > some pointers on how to do the customization.
>
> Is such customization possible without recompiling the code generator?

No, you would need to customize the code generator to generate the
accept() functions.


> Anyways, I'm interested in learning more on how to do this.

In a nutshell, you would add the generation of the accept()
declarations in tree-header.cxx. The implementations can be
generated in tree-source.cxx. You can also add an option,
say --generate-visitor <visitor-name>, which would trigger
the generation as well as provide the visitor class name.
You can look at existing options for an example on how to
do this. The header that defines the visitor class can be
included in to the generated code using the --hxx-prologue
option.


> Finally, I' extremely interested in the functionality! 

Are you interested in it from a practical or purely academic
perspective? ;-)

Boris




More information about the xsd-users mailing list