[xsd-users] Schema and inheritance

Boris Kolpackov boris at codesynthesis.com
Tue Sep 13 12:08:19 EDT 2005


Mickael,

Vera Mickael <vera.mickael at free.fr> writes:

> About polymorphism, BusinessCard::person returning a person
> may be intersting if the code manipulates a person for its
> common attributes. I agree the interest may be limited.

Right. You have "convenient" access to one "half" of the data.
To get to the other you will have to jump through hoops (casting).


> I've been thinking about those problems for a long time. I
> did not found any satisfying solution. I guess you thought
> about a factory provided by the user.
>
> One solution would be to provide a factory at parsing time,
> each time the parser needs to instantiate an obect it asks
> the factory to do it. The user may have its own classes
> instanciated or the factory instanciates the default
> generated classes.

Yes I thought about this.


> User classes inherit from generated classes. The major problem
> is that generated classes use relations to generated classes,
> the user still have to cast to its own classes.

Right. There is also one additional benefit to this approach:
if accessors are made virtual then the user can override them
and get a somewhat crippled behavior polymorphism.

This shows what the real problem with translating schema-level
polymorphism to the target language polymorphism: schema is
about "data" while polymorphism is about "interfaces". With
the current state of the art, the user cannot add to the
interface, they have to program against the data access
API that was generated by the tool from schema. So what would
really be cool is to allow to the user to extend the interface
of the generated classes. One way to achieve this would be via
C++ templates. Here is the "traditional" mapping for the example
discussed previously:

struct Person
{
  string
  name () const;
};

struct Superman: virtual Person
{
  bool
  can_fly () const;
};

struct BusinessCard
{
  Person&
  person ();
};

Here the interface (Person in this case) is "fixed". Now if the generated
code for BusinessCard looked like this:


template <typename P = Person>
struct BusinessCard
{
  virtual P&
  person ();
};


Then we could write:


struct NewPerson: virtual Person
{
  virtual string
  print ()
  {
    return name ();
  }
};

struct NewSuperman: virtual NewPerson,
                    virtual Superman
{
  virtual string
  print ()
  {
    return name () + (can_fly () ? "" : " (flying)");
  }
};


typedef BusinessCard<NewPerson> NewBusinessCard;


Just need to find time to implement all this ;-)

thanks,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20050913/2940d726/attachment.pgp


More information about the xsd-users mailing list