diff --git a/tinyxml2.h b/tinyxml2.h index b9cdbd7..5e562c7 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1183,45 +1183,46 @@ private: TiXmlHandle handleCopy = handle; @endverbatim */ -class XMLHandle +template< class T > +class XMLHandleT { public: /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. - XMLHandle( XMLNode* _node ) { node = _node; } + XMLHandleT( T* _node ) { node = _node; } //XMLHandle( const XMLNode* _node ) { node = const_cast(_node); } - XMLHandle( XMLNode& _node ) { node = &_node; } + XMLHandleT( T& _node ) { node = &_node; } //XMLHandle( const XMLNode& _node ) { node = const_cast(&_node); } - XMLHandle( const XMLHandle& ref ) { node = ref.node; } + XMLHandleT( const XMLHandleT& ref ) { node = ref.node; } - XMLHandle operator=( XMLHandle ref ) { node = ref.node; return *this; } + XMLHandleT operator=( XMLHandleT 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 ); } + XMLHandleT FirstChild() { return XMLHandleT( node ? node->FirstChild() : 0 ); } + const XMLHandleT FirstChild() const { return XMLHandleT( 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 ); } + XMLHandleT FirstChildElement( const char* value=0 ) { return XMLHandleT( node ? node->FirstChildElement( value ) : 0 ); } + const XMLHandleT FirstChildElement( const char* value=0 ) const { return XMLHandleT( node ? node->FirstChildElement( value ) : 0 ); } - XMLHandle LastChild() { return XMLHandle( node ? node->LastChild() : 0 ); } - const XMLHandle LastChild() const { return XMLHandle( node ? node->LastChild() : 0 ); } + XMLHandleT LastChild() { return XMLHandleT( node ? node->LastChild() : 0 ); } + const XMLHandleT LastChild() const { return XMLHandleT( 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 ); } + XMLHandleT LastChildElement( const char* _value=0 ) { return XMLHandleT( node ? node->LastChildElement( _value ) : 0 ); } + const XMLHandleT LastChildElement( const char* _value=0 ) const { return XMLHandleT( node ? node->LastChildElement( _value ) : 0 ); } - XMLHandle PreviousSibling() { return XMLHandle( node ? node->PreviousSibling() : 0 ); } - const XMLHandle PreviousSibling() const { return XMLHandle( node ? node->PreviousSibling() : 0 ); } + XMLHandleT PreviousSibling() { return XMLHandleT( node ? node->PreviousSibling() : 0 ); } + const XMLHandleT PreviousSibling() const { return XMLHandleT( 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 ); } + XMLHandleT PreviousSiblingElement( const char* _value=0 ) { return XMLHandleT( node ? node->PreviousSiblingElement( _value ) : 0 ); } + const XMLHandleT PreviousSiblingElement( const char* _value=0 ) const { return XMLHandleT( node ? node->PreviousSiblingElement( _value ) : 0 ); } - XMLHandle NextSibling() { return XMLHandle( node ? node->NextSibling() : 0 ); } - const XMLHandle NextSibling() const { return XMLHandle( node ? node->NextSibling() : 0 ); } + XMLHandleT NextSibling() { return XMLHandleT( node ? node->NextSibling() : 0 ); } + const XMLHandleT NextSibling() const { return XMLHandleT( 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 ); } + XMLHandleT NextSiblingElement( const char* _value=0 ) { return XMLHandleT( node ? node->NextSiblingElement( _value ) : 0 ); } + const XMLHandleT NextSiblingElement( const char* _value=0 ) const { return XMLHandleT( node ? node->NextSiblingElement( _value ) : 0 ); } - XMLNode* ToNode() { return node; } - const XMLNode* ToNode() const { return node; } + T* ToNode() { return node; } + const T* ToNode() const { 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 ); } @@ -1232,9 +1233,12 @@ public: const XMLDeclaration* ToDeclaration() const { return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 0 ); } private: - XMLNode* node; + T* node; }; +typedef XMLHandleT< XMLNode > XMLHandle; +typedef XMLHandleT< const XMLNode > XMLConstHandle; + /** Printing functionality. The XMLPrinter gives you more diff --git a/xmltest.cpp b/xmltest.cpp index 07d593c..ab2d568 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -736,9 +736,8 @@ int main( int /*argc*/, const char ** /*argv*/ ) XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement(); XMLTest( "Handle, success, mutable", ele->Value(), "sub" ); -// ele = 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" ); }