From b6b4e82626724e648212b813c55412751f3bc63c Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 27 Aug 2014 17:17:47 +0400 Subject: [PATCH] Remove repeated virtual calls --- tinyxml2.cpp | 31 ++++++++++++++++++------------- tinyxml2.h | 16 ++++++++-------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index df345f9..c273ebe 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -773,10 +773,11 @@ const XMLElement* XMLNode::LastChildElement( const char* value ) const const XMLElement* XMLNode::NextSiblingElement( const char* value ) const { - for( XMLNode* element=this->_next; element; element = element->_next ) { - if ( element->ToElement() - && (!value || XMLUtil::StringEqual( value, element->Value() ))) { - return element->ToElement(); + for( XMLNode* node=this->_next; node; node = node->_next ) { + const XMLElement* element = node->ToElement(); + if ( element + && (!value || XMLUtil::StringEqual( value, node->Value() ))) { + return element; } } return 0; @@ -785,10 +786,11 @@ const XMLElement* XMLNode::NextSiblingElement( const char* value ) const const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const { - for( XMLNode* element=_prev; element; element = element->_prev ) { - if ( element->ToElement() - && (!value || XMLUtil::StringEqual( value, element->Value() ))) { - return element->ToElement(); + for( XMLNode* node=_prev; node; node = node->_prev ) { + const XMLElement* element = node->ToElement(); + if ( element + && (!value || XMLUtil::StringEqual( value, node->Value() ))) { + return element; } } return 0; @@ -833,8 +835,9 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd ) break; } + XMLElement* ele = node->ToElement(); // We read the end tag. Return it to the parent. - if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) { + if ( ele && ele->ClosingType() == XMLElement::CLOSING ) { if ( parentEnd ) { *parentEnd = static_cast(node)->_value; } @@ -845,7 +848,6 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd ) // Handle an end tag returned to this level. // And handle a bunch of annoying errors. - XMLElement* ele = node->ToElement(); if ( ele ) { if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) { _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); @@ -961,7 +963,8 @@ XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const bool XMLComment::ShallowEqual( const XMLNode* compare ) const { - return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() )); + const XMLComment* comment = compare->ToComment(); + return ( comment && XMLUtil::StringEqual( comment->Value(), Value() )); } @@ -1008,7 +1011,8 @@ XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const { - return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() )); + const XMLDeclaration* declaration = compare->ToDeclaration(); + return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() )); } @@ -1055,7 +1059,8 @@ XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const { - return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() )); + const XMLUnknown* unknown = compare->ToUnknown(); + return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() )); } diff --git a/tinyxml2.h b/tinyxml2.h index f4503fd..9864380 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1819,19 +1819,19 @@ public: } /// Safe cast to XMLElement. This can return null. XMLElement* ToElement() { - return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToElement() ); } /// Safe cast to XMLText. This can return null. XMLText* ToText() { - return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToText() ); } /// Safe cast to XMLUnknown. This can return null. XMLUnknown* ToUnknown() { - return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToUnknown() ); } /// Safe cast to XMLDeclaration. This can return null. XMLDeclaration* ToDeclaration() { - return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() ); } private: @@ -1891,16 +1891,16 @@ public: return _node; } const XMLElement* ToElement() const { - return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToElement() ); } const XMLText* ToText() const { - return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToText() ); } const XMLUnknown* ToUnknown() const { - return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToUnknown() ); } const XMLDeclaration* ToDeclaration() const { - return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 ); + return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() ); } private: