AW: [xsd-users] how to get type information from tree nodes

Klima Georg G.Klima at durst-online.at
Thu Aug 22 12:28:51 EDT 2013


Hi Lena!

Eventually this could also be a solution for you!

http://www.artima.com/cppsource/cooperative_visitor.html

Read the Section about "Generating tags".

This or a modified version that introduces static (runtime) tagging with unique integer IDs could be of interest for you.

Also this could be of interest for xsd in general. Since this method introduces lesser overhead that rtti and isn't that cumbersome as trying one dynamic_cast<XYZ> after another!


Mit freundlichen Grüßen / Best regards
Georg Klima

Ps: eventually there are other/better sources concering tagging but this was the first in reach I could remember!

-----Ursprüngliche Nachricht-----
Von: xsd-users-bounces at codesynthesis.com [mailto:xsd-users-bounces at codesynthesis.com] Im Auftrag von Lena Herscheid
Gesendet: Donnerstag, 22. August 2013 00:06
An: xsd-users at codesynthesis.com
Betreff: *****POSSIBLE SPAM***** AW: [xsd-users] how to get type information from tree nodes

Thanks for the quick answer!

When saying that RTTI was not portable, I was referring to platform differences with typeidx().name().
But it surely is possible to compare to the typeid of a "fresh class instance" instead of hard coded strings. 
So I'll certainly find a solution.

Cheers
Lena

-----Ursprüngliche Nachricht-----
Von: Boris Kolpackov [mailto:boris at codesynthesis.com]
Gesendet: 21 August 2013 20:16
An: Lena Herscheid
Cc: xsd-users at codesynthesis.com
Betreff: Re: [xsd-users] how to get type information from tree nodes

Hi Lena,

Lena Herscheid <lenaherscheid at gmail.com> writes:

> Is it possible with cxx-tree to generate member functions returning 
> the typename of the current node (as string)?

No, there is no such support.


> I'm asking because the alternative way of efficiently determining the 
> type of any during runtime requires some RTTI which is not portable.

I am not sure what you mean by "some RTTI", but standard C++ RTTI (i.e., dynamic_cast, typeid) are very much portable. Some projects may choose to disable this support in order to minimize code size, but that's another story.

So if you need a type name in C++/Tree, then typeid(x).name () is the recommended way. If, however, you want to find whether x is of a particular dynamic type, then use dynamic_cast, it will be much faster than any custom RTTI with string comparison:

base& x = ...

if (derived1* d1 = dynamic_cast<derived1*> (&x)) {
  // x is derived1
}
else if (derived2* d2 = dynamic_cast<derived2*> (&x)) {
  // x is derived2
}

...

else
{
  // x is something else
}

Boris





More information about the xsd-users mailing list