You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Michael Kochetkov <Mi...@synartra.com> on 2005/03/29 13:32:42 UTC

Usage of C-style cast while downcasting.

Hello,
I have Xerces pre-built Win32 library with VC6/VC7.1.
You use C-style cast notation in your samples. For example, see
File: xerces-c-src_2_6_0\samples\PSVIWriter\PSVIWriterHandlers.cpp
Method: void PSVIWriterHandlers::processDOMElement
Code inside:
for (unsigned int i=0; i < elems->getLength(); i++) {
	DOMElement* elem = (DOMElement*)elems->item(i);

Would not it be more reliable to make use of dynamic_cast? If you believe
that it is then would not it be logical if the pre-built distribution
packages contain the RTTI information? MS VC compilers family have the RTTI
option turned off by default and as far as we do not build Xerces ourselves
I am to use the same potentially unsafe cast.

Thank you,
--
Michael Kochetkov.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Michael Kochetkov <Mi...@synartra.com>.
> At 17.03 29/03/2005 +0400, Michael Kochetkov wrote:
> > > >BTW, I have noticed that
> > > >for (XMLSize_t i = 0; i < len; ++i) {
> > > >         XS::DOMNode* curNode = myNodes->item(i);
> > > >         XS::DOMElement* curElement = 
> > > >reinterpret_cast<XS::DOMElement*>(curNode);
> > > >         XS::DOMNodeList *anotherNodeList = 
> > > >elem.getElementsByTagName(wsAgent.c_str());
> > > >
> > > >works pretty well indeed even if curNode does not contain
> > > child nodes
> > > >(XS stands for the Xerces long namespace name). Does Xerces
> > > ever return
> > > >pure
> > > >DOMNode* pointers?
> > >
> > > I don't get the issue: what do you mean by "works pretty well 
> > > indeed"? I cannot even guess what this code should do, as you
> >It does not crash.
> 
> Why should it? you are not using curElement, but an "elem" 
> object that I don't know where it comes from.
Well, it shell read curElement->getElementsByTagName indeed. Copy/paste and
later editing issue.

[...]
> >DOMElement* elem = (DOMElement*)elems->item(i); is reliable 
> and will remain
> >to be reliable in the future.
> 
> You can be sure that myNodes->item() call will always return 
> DOMElement 
> object if it is the return value of a getElementsByTagName. 
> But if it has 
> been returned by a call to getChildNodes it will contains DOMText, 
> DOMComment, etc...
> The correct programming would be checking for getNodeType to 
> be equal to 
> ELEMENT_NODE before casting to a DOMElement.
I have made use of hasChildNodes but I believe you are right -- ELEMENT_NODE
shall be more sutable. Though I consider it as a non-language workaround. I
do understand -- standard, portability issues, etc. So, please treat it as a
kind of a pedant's grumbling -- I do not argue.
Thanks a lot for your help.

--
Michael Kochetkov.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Alberto Massari <am...@datadirect.com>.
At 17.03 29/03/2005 +0400, Michael Kochetkov wrote:
> > >BTW, I have noticed that
> > >for (XMLSize_t i = 0; i < len; ++i) {
> > >         XS::DOMNode* curNode = myNodes->item(i);
> > >         XS::DOMElement* curElement =
> > >reinterpret_cast<XS::DOMElement*>(curNode);
> > >         XS::DOMNodeList *anotherNodeList =
> > >elem.getElementsByTagName(wsAgent.c_str());
> > >
> > >works pretty well indeed even if curNode does not contain
> > child nodes
> > >(XS stands for the Xerces long namespace name). Does Xerces
> > ever return
> > >pure
> > >DOMNode* pointers?
> >
> > I don't get the issue: what do you mean by "works pretty well
> > indeed"? I cannot even guess what this code should do, as you
>It does not crash.

Why should it? you are not using curElement, but an "elem" object that I 
don't know where it comes from.


> > are invoking getElementsByTagName on the "elem" variable but
> > you just initialized "curElement".
> > As for the "pure DOMNode* pointers", I guess you are asking
> > if a DOMNode object can ever exist; in this case, the answer
> > is no, as it is a pure virtual interface.
>Well, I do not know the Xerces internals, so I will try to elaborate my
>question. I was going to use dynamic_cast to be sure the pointer
>myNodes->item(i) returns has getElementsByTagName method. As far as DOMNode
>itself does not have getElementsByTagName and the "blind" cast is used in
>original Xerces example there is a chance that such downcasting may stop
>working in the future. A misbehavior may be caused with multiple
>inheritance, for example. As far as I am not a Xerces expert but I believe I
>know programming in C++ :) so while studying examples I begin to think about
>some non-language conventions. Such a convention may be the prohibition of
>multiple inheritence, definite class order in multiple inheritence or
>item(i) always return DOMElements, etc. So, I just want to be sure the
>DOMElement* elem = (DOMElement*)elems->item(i); is reliable and will remain
>to be reliable in the future.

You can be sure that myNodes->item() call will always return DOMElement 
object if it is the return value of a getElementsByTagName. But if it has 
been returned by a call to getChildNodes it will contains DOMText, 
DOMComment, etc...
The correct programming would be checking for getNodeType to be equal to 
ELEMENT_NODE before casting to a DOMElement.

Alberto 



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Michael Kochetkov <Mi...@synartra.com>.
> >BTW, I have noticed that
> >for (XMLSize_t i = 0; i < len; ++i) {
> >         XS::DOMNode* curNode = myNodes->item(i);
> >         XS::DOMElement* curElement =
> >reinterpret_cast<XS::DOMElement*>(curNode);
> >         XS::DOMNodeList *anotherNodeList = 
> >elem.getElementsByTagName(wsAgent.c_str());
> >
> >works pretty well indeed even if curNode does not contain 
> child nodes 
> >(XS stands for the Xerces long namespace name). Does Xerces 
> ever return 
> >pure
> >DOMNode* pointers?
> 
> I don't get the issue: what do you mean by "works pretty well 
> indeed"? I cannot even guess what this code should do, as you 
It does not crash.

> are invoking getElementsByTagName on the "elem" variable but 
> you just initialized "curElement".
> As for the "pure DOMNode* pointers", I guess you are asking 
> if a DOMNode object can ever exist; in this case, the answer 
> is no, as it is a pure virtual interface.
Well, I do not know the Xerces internals, so I will try to elaborate my
question. I was going to use dynamic_cast to be sure the pointer
myNodes->item(i) returns has getElementsByTagName method. As far as DOMNode
itself does not have getElementsByTagName and the "blind" cast is used in
original Xerces example there is a chance that such downcasting may stop
working in the future. A misbehavior may be caused with multiple
inheritance, for example. As far as I am not a Xerces expert but I believe I
know programming in C++ :) so while studying examples I begin to think about
some non-language conventions. Such a convention may be the prohibition of
multiple inheritence, definite class order in multiple inheritence or
item(i) always return DOMElements, etc. So, I just want to be sure the
DOMElement* elem = (DOMElement*)elems->item(i); is reliable and will remain
to be reliable in the future.

Thank you,
--
Michael Kochetkov.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Alberto Massari <am...@datadirect.com>.
At 15.56 29/03/2005 +0400, Michael Kochetkov wrote:
> > Hi Michael,
>Hi, Alberto
> > unfortunately RTTI is not available on all the platforms
> > where Xerces compiles, so the sample code cannot make use of
> > dynamic_cast.
>Well, I only asked for /GR switch while building Xerces for VC6/7.X.
>
> > Clearly you can download the source distribution of Xerces,
> > turn on RTTI and use dynamic_cast in your code.
>Our management prefer to have "official" builds.

We'll think about it; the downside would be that 1) RTTI makes the DLL 
bigger, and 2) having it enabled only on some platforms could lead to 
coding errors.


>BTW, I have noticed that
>for (XMLSize_t i = 0; i < len; ++i) {
>         XS::DOMNode* curNode = myNodes->item(i);
>         XS::DOMElement* curElement =
>reinterpret_cast<XS::DOMElement*>(curNode);
>         XS::DOMNodeList *anotherNodeList =
>elem.getElementsByTagName(wsAgent.c_str());
>
>works pretty well indeed even if curNode does not contain child nodes (XS
>stands for the Xerces long namespace name). Does Xerces ever return pure
>DOMNode* pointers?

I don't get the issue: what do you mean by "works pretty well indeed"? I 
cannot even guess what this code should do, as you are invoking 
getElementsByTagName on the "elem" variable but you just initialized 
"curElement".
As for the "pure DOMNode* pointers", I guess you are asking if a DOMNode 
object can ever exist; in this case, the answer is no, as it is a pure 
virtual interface.

Alberto 



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Usage of C-style cast while downcasting.

Posted by da...@us.ibm.com.
> Hi,
> 
> I was trying to determine how to turn RTTI on for xerces C++. I did a 
find/grep 
> in the xerces files and couldn't find any references that looked 
meaningful for 
> me to tweak. Also, I was under the impression that g++ would, by 
default, have 
> RTTI enabled (and that it had to be disabled explicitly with the 
-fno-rtti flag) 
> - so I am not clear where it is turned off and how to turn it on for my 
local 
> build. Sorry for such a simple question...

GCC does have RTTI enabled by default, unlike the Microsoft compiler, 
which does not.  If you're using GCC, you don't have to do anything.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


Re: Usage of C-style cast while downcasting.

Posted by Steve Harrington <sh...@aoc.nrao.edu>.
Hi,

I was trying to determine how to turn RTTI on for xerces C++. I did a find/grep 
in the xerces files and couldn't find any references that looked meaningful for 
me to tweak. Also, I was under the impression that g++ would, by default, have 
RTTI enabled (and that it had to be disabled explicitly with the -fno-rtti flag) 
- so I am not clear where it is turned off and how to turn it on for my local 
build. Sorry for such a simple question...

Thanks,

Steve Harrington
National Radio Astronomy Observatory

Alberto Massari wrote:
> Hi Michael,
> unfortunately RTTI is not available on all the platforms where Xerces 
> compiles, so the sample code cannot make use of dynamic_cast.
> Clearly you can download the source distribution of Xerces, turn on RTTI 
> and use dynamic_cast in your code.
> 
> Alberto
> 
> At 15.32 29/03/2005 +0400, Michael Kochetkov wrote:
> 
>> Hello,
>> I have Xerces pre-built Win32 library with VC6/VC7.1.
>> You use C-style cast notation in your samples. For example, see
>> File: xerces-c-src_2_6_0\samples\PSVIWriter\PSVIWriterHandlers.cpp
>> Method: void PSVIWriterHandlers::processDOMElement
>> Code inside:
>> for (unsigned int i=0; i < elems->getLength(); i++) {
>>         DOMElement* elem = (DOMElement*)elems->item(i);
>>
>> Would not it be more reliable to make use of dynamic_cast? If you believe
>> that it is then would not it be logical if the pre-built distribution
>> packages contain the RTTI information? MS VC compilers family have the 
>> RTTI
>> option turned off by default and as far as we do not build Xerces 
>> ourselves
>> I am to use the same potentially unsafe cast.
>>
>> Thank you,
>> -- 
>> Michael Kochetkov.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Michael Kochetkov <Mi...@synartra.com>.
> Hi Michael,
Hi, Axel,

> I use to take 'static_cast' in order to obtain a 
> DOMElement-pointer from a DOMNode-pointer. But usually I 
> check the type of the node actually to be 
> DOMNode::ELEMENT_NODE before performing the cast.
I use reinterpret_cast in my code sometimes just because it is ugly enough
to draw attention to itself. I almost always consider it as a matter to
change either implementation or architecture.

> static_cast is a weaker concept than dynamic_cast, sure. 
> However, static_cast is a stronger concept than 
> reinterpret_cast, since the compiler can check if there is a 
> descent relationship between the types.
You are right. It shall do the job provided there are no virtual bases and
no pointers that point the other objects we believe.

> BTW: do there exist any C++-compilers which do not understand 
> static_cast?
I believe all modern (and not that modern) compilers understand xxx_cast
expressions. But there likely to be some bugs in implementations (off the
top of my head I can recall some known problems with exact type
determination in exception specifications). So, for general purpose
libraries like Xerces is it is considered to be OK not to rely upon the
language (at least for C++) built-in dynamic types support.

Thanks a lot for your help.
--
Michael Kochetkov.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Usage of C-style cast while downcasting.

Posted by Axel Weiß <aw...@informatik.hu-berlin.de>.
Am Dienstag, 29. März 2005 13:56 schrieb Michael Kochetkov:
> > Clearly you can download the source distribution of Xerces,
> > turn on RTTI and use dynamic_cast in your code.
>
> Our management prefer to have "official" builds.
>
> BTW, I have noticed that
> for (XMLSize_t i = 0; i < len; ++i) {
>         XS::DOMNode* curNode = myNodes->item(i);
>         XS::DOMElement* curElement =
> reinterpret_cast<XS::DOMElement*>(curNode);
>         XS::DOMNodeList *anotherNodeList =
> elem.getElementsByTagName(wsAgent.c_str());
>
> works pretty well indeed even if curNode does not contain child nodes
> (XS stands for the Xerces long namespace name). Does Xerces ever
> return pure DOMNode* pointers?

Hi Michael,

I use to take 'static_cast' in order to obtain a DOMElement-pointer from 
a DOMNode-pointer. But usually I check the type of the node actually to 
be DOMNode::ELEMENT_NODE before performing the cast.

static_cast is a weaker concept than dynamic_cast, sure. However, 
static_cast is a stronger concept than reinterpret_cast, since the 
compiler can check if there is a descent relationship between the 
types.

BTW: do there exist any C++-compilers which do not understand 
static_cast?

Cheers,
			Axel

-- 
Humboldt-Universität zu Berlin
Institut für Informatik
Signalverarbeitung und Mustererkennung
Dipl.-Inf. Axel Weiß
Rudower Chaussee 25
12489 Berlin-Adlershof
+49-30-2093-3050
** www.freesp.de **

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Usage of C-style cast while downcasting.

Posted by Michael Kochetkov <Mi...@synartra.com>.
> Hi Michael,
Hi, Alberto
> unfortunately RTTI is not available on all the platforms 
> where Xerces compiles, so the sample code cannot make use of 
> dynamic_cast.
Well, I only asked for /GR switch while building Xerces for VC6/7.X.

> Clearly you can download the source distribution of Xerces, 
> turn on RTTI and use dynamic_cast in your code.
Our management prefer to have "official" builds.

BTW, I have noticed that 
for (XMLSize_t i = 0; i < len; ++i) {
	XS::DOMNode* curNode = myNodes->item(i);
	XS::DOMElement* curElement =
reinterpret_cast<XS::DOMElement*>(curNode);
	XS::DOMNodeList *anotherNodeList =
elem.getElementsByTagName(wsAgent.c_str());

works pretty well indeed even if curNode does not contain child nodes (XS
stands for the Xerces long namespace name). Does Xerces ever return pure
DOMNode* pointers?

Thank you,
--
Michael Kochetkov.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Usage of C-style cast while downcasting.

Posted by Alberto Massari <am...@datadirect.com>.
Hi Michael,
unfortunately RTTI is not available on all the platforms where Xerces 
compiles, so the sample code cannot make use of dynamic_cast.
Clearly you can download the source distribution of Xerces, turn on RTTI 
and use dynamic_cast in your code.

Alberto

At 15.32 29/03/2005 +0400, Michael Kochetkov wrote:
>Hello,
>I have Xerces pre-built Win32 library with VC6/VC7.1.
>You use C-style cast notation in your samples. For example, see
>File: xerces-c-src_2_6_0\samples\PSVIWriter\PSVIWriterHandlers.cpp
>Method: void PSVIWriterHandlers::processDOMElement
>Code inside:
>for (unsigned int i=0; i < elems->getLength(); i++) {
>         DOMElement* elem = (DOMElement*)elems->item(i);
>
>Would not it be more reliable to make use of dynamic_cast? If you believe
>that it is then would not it be logical if the pre-built distribution
>packages contain the RTTI information? MS VC compilers family have the RTTI
>option turned off by default and as far as we do not build Xerces ourselves
>I am to use the same potentially unsafe cast.
>
>Thank you,
>--
>Michael Kochetkov.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org