[xsd-users] xsd inheretance at generated code

Boris Kolpackov boris at codesynthesis.com
Sun Jun 10 04:31:57 EDT 2007


Hi Uri,

Uri Karagila <uri at hyperroll.com> writes:

> 1. xsd abstract type "shape".
> 2. both xsd type "triangle"  and type "square" are an extension of "shape".
> at the generated code will get:
> 1. class shape
> 2. classes triangle and square, both inherited shape.
>
> in case i have a container which holds "shape" class descendant objects.
> what would be the best way to get random element's type. note: both
> "triangle" and "square" does not have and attribute (i.e., class member)
> which holds object type.


To figure out if a particular shape is a triangle or a square, you can
use dynamic_cast. For example, assuming container_type is a sequence
that holds shapes:

container_type& c = ...

for (container_type::iterator i (c.begin ()); i != c.end (); ++i)
{
  if (triangle* t = dynamic_cast<triangle*> (&(*i)))
  {
    // triangle
  }
  else if (square* t = dynamic_cast<square*> (&(*i)))
  {
    // square
  }
  else
  {
    // something else
  }
}

The polymorphism example in the cxx/tree/examples directory also shows how
to do this. If instead you need an object that identifies the type of an
element in the container, then you can use the typeid operator.

Also note that if you want to serialize (and later parse) such a polymorphic
container then you will need to use the --generate-polymorphic option when
translating your schemas.


hth,
-boris




More information about the xsd-users mailing list