diff --git a/tinyxml2.h b/tinyxml2.h index 8740a7f..9800878 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1187,55 +1187,66 @@ class XMLHandle { public: /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. - XMLHandle( XMLNode* _node ) { node = _node; } - //XMLHandle( const XMLNode* _node ) { node = const_cast(_node); } - XMLHandle( XMLNode& _node ) { node = &_node; } - //XMLHandle( const XMLNode& _node ) { node = const_cast(&_node); } - XMLHandle( const XMLHandle& ref ) { node = ref.node; } + XMLHandle( XMLNode* _node ) { node = _node; } + XMLHandle( XMLNode& _node ) { node = &_node; } + XMLHandle( const XMLHandle& ref ) { node = ref.node; } - XMLHandle operator=( XMLHandle ref ) { node = ref.node; return *this; } - - XMLHandle FirstChild() { return XMLHandle( node ? node->FirstChild() : static_cast(0) ); } - const XMLHandle FirstChild() const { return XMLHandle( node ? node->FirstChild() : 0 ); } - - XMLHandle FirstChildElement( const char* value=0 ) { return XMLHandle( node ? node->FirstChildElement( value ) : 0 ); } - const XMLHandle FirstChildElement( const char* value=0 ) const { return XMLHandle( node ? node->FirstChildElement( value ) : 0 ); } - - XMLHandle LastChild() { return XMLHandle( node ? node->LastChild() : 0 ); } - const XMLHandle LastChild() const { return XMLHandle( node ? node->LastChild() : 0 ); } - - XMLHandle LastChildElement( const char* _value=0 ) { return XMLHandle( node ? node->LastChildElement( _value ) : 0 ); } - const XMLHandle LastChildElement( const char* _value=0 ) const { return XMLHandle( node ? node->LastChildElement( _value ) : 0 ); } - - XMLHandle PreviousSibling() { return XMLHandle( node ? node->PreviousSibling() : 0 ); } - const XMLHandle PreviousSibling() const { return XMLHandle( node ? node->PreviousSibling() : 0 ); } + XMLHandle operator=( const XMLHandle& ref ) { node = ref.node; return *this; } + XMLHandle FirstChild() { return XMLHandle( node ? node->FirstChild() : 0 ); } + XMLHandle FirstChildElement( const char* value=0 ) { return XMLHandle( node ? node->FirstChildElement( value ) : 0 ); } + XMLHandle LastChild() { return XMLHandle( node ? node->LastChild() : 0 ); } + XMLHandle LastChildElement( const char* _value=0 ) { return XMLHandle( node ? node->LastChildElement( _value ) : 0 ); } + XMLHandle PreviousSibling() { return XMLHandle( node ? node->PreviousSibling() : 0 ); } XMLHandle PreviousSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } - const XMLHandle PreviousSiblingElement( const char* _value=0 ) const { return XMLHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } - - XMLHandle NextSibling() { return XMLHandle( node ? node->NextSibling() : 0 ); } - const XMLHandle NextSibling() const { return XMLHandle( node ? node->NextSibling() : 0 ); } - - XMLHandle NextSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->NextSiblingElement( _value ) : 0 ); } - const XMLHandle NextSiblingElement( const char* _value=0 ) const { return XMLHandle( node ? node->NextSiblingElement( _value ) : 0 ); } + XMLHandle NextSibling() { return XMLHandle( node ? node->NextSibling() : 0 ); } + XMLHandle NextSiblingElement( const char* _value=0 ) { return XMLHandle( node ? node->NextSiblingElement( _value ) : 0 ); } - XMLNode* ToNode() { return node; } - const XMLNode* ToNode() const { return node; } + XMLNode* ToNode() { return node; } XMLElement* ToElement() { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } - const XMLElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } XMLText* ToText() { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } - const XMLText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } XMLUnknown* ToUnknown() { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } - const XMLUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } XMLDeclaration* ToDeclaration() { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } - const XMLDeclaration* ToDeclaration() const { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } private: XMLNode* node; }; +/** + A variant of the XMLHandle class for working with const XMLNodes and Documents. +*/ +class XMLConstHandle +{ +public: + XMLConstHandle( const XMLNode* _node ) { node = _node; } + XMLConstHandle( const XMLNode& _node ) { node = &_node; } + XMLConstHandle( const XMLConstHandle& ref ) { node = ref.node; } + + XMLConstHandle operator=( const XMLConstHandle& ref ) { node = ref.node; return *this; } + + const XMLConstHandle FirstChild() const { return XMLConstHandle( node ? node->FirstChild() : 0 ); } + const XMLConstHandle FirstChildElement( const char* value=0 ) const { return XMLConstHandle( node ? node->FirstChildElement( value ) : 0 ); } + const XMLConstHandle LastChild() const { return XMLConstHandle( node ? node->LastChild() : 0 ); } + const XMLConstHandle LastChildElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->LastChildElement( _value ) : 0 ); } + const XMLConstHandle PreviousSibling() const { return XMLConstHandle( node ? node->PreviousSibling() : 0 ); } + const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->PreviousSiblingElement( _value ) : 0 ); } + const XMLConstHandle NextSibling() const { return XMLConstHandle( node ? node->NextSibling() : 0 ); } + const XMLConstHandle NextSiblingElement( const char* _value=0 ) const { return XMLConstHandle( node ? node->NextSiblingElement( _value ) : 0 ); } + + + const XMLNode* ToNode() const { return node; } + const XMLElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); } + const XMLText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); } + const XMLUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); } + const XMLDeclaration* ToDeclaration() const { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } + +private: + const XMLNode* node; +}; + + /** Printing functionality. The XMLPrinter gives you more options than the XMLDocument::Print() method. diff --git a/xmltest.cpp b/xmltest.cpp index a2ebe5a..4fd65c7 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -736,8 +736,8 @@ int main( int /*argc*/, const char ** /*argv*/ ) XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement(); XMLTest( "Handle, success, mutable", ele->Value(), "sub" ); - const XMLElement* eleC = XMLHandle( docC ).FirstChildElement( "element" ).FirstChild().ToElement(); - XMLTest( "Handle, success, mutable", ele->Value(), "sub" ); + const XMLElement* eleC = XMLConstHandle( docC ).FirstChildElement( "element" ).FirstChild().ToElement(); + XMLTest( "Handle, success, mutable", eleC->Value(), "sub" ); }