[xsd-users] xsd inheretance at generated code

Uri Karagila uri at hyperroll.com
Sun Jun 10 04:53:50 EDT 2007


Hi Boris,

suggested option was already considered; yet, since we are not using RTTI (code performance reasons)and therefore dynamic_cast is forbidden.
can you think of other options (beside updating xsd schema)?

Regards

Uri 

BTW
Tnx for your fast replay.
  

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sent: Sun 6/10/2007 11:31 AM
To: Uri Karagila
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] xsd inheretance at generated code
 
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