[xsd-users] polymorphism through xs:choice

Bielik, Robert Robert.Bielik at gyros.com
Tue Jun 11 08:55:51 EDT 2013


One option would be if there is a possibility to assign a GUID to the generated C++ type, and map that GUID to a casting function. I do the last bit today in my code, but I'd be happy to have the GUID be generated via XSD, so that C# clients can send objects to C++ servers.

Would that be simple to do via XSD -> CodeSynthesis ?

Regards,
/Rob

-----Original Message-----
From: xsd-users-bounces at codesynthesis.com [mailto:xsd-users-bounces at codesynthesis.com] On Behalf Of Boris Kolpackov
Sent: den 11 juni 2013 14:52
To: Harg Tholan
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] polymorphism through xs:choice

Hi Harg,

Harg Tholan <hargtholan at gmail.com> writes:

> The question is, how can I retrieve the actual type of the referenced 
> element? I need to know the actual type because I need to "dynamic cast"
> to it, but I can not know beforehand.

You cannot "retrieve" a C++ type at runtime and dynamic_cast to it. C++ is a compiled, statically-type language. What you can do is discover whether an instance is of one of the types from a predefined (at compile
time) set. For example:

Base& x = ... // Gen element from the choice. 

if (DerivedA* a = dynamic_cast<DerivedA> (&x)) {
  // It is DerivedA.
}
else if (DerivedB* b = dynamic_cast<DerivedB> (&x)) {
  // It is DerivedB.
}

Other things that you could do are define a virtual function in Base and then implement it in DerivedX (using type customization) or call
typeid() on the instance to get, for example, the type name.

Boris




More information about the xsd-users mailing list