Procházet zdrojové kódy

gave up trying to be clever. split the handles into 2 classes.

Lee Thomason před 14 roky
rodič
revize
8b89981989
2 změnil soubory, kde provedl 45 přidání a 34 odebrání
  1. 43 32
      tinyxml2.h
  2. 2 2
      xmltest.cpp

+ 43 - 32
tinyxml2.h

@@ -1187,52 +1187,63 @@ 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<XMLNode*>(_node); }
-	XMLHandle( XMLNode& _node )								{ node = &_node; }
-	//XMLHandle( const XMLNode& _node )						{ node = const_cast<XMLNode*>(&_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 operator=( const XMLHandle& ref )								{ node = ref.node; return *this; }
 
-	XMLHandle FirstChild() 									{ return XMLHandle( node ? node->FirstChild() : static_cast<XMLNode*>(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 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 ); }
+	XMLHandle NextSibling()													{ return XMLHandle( node ? node->NextSibling() : 0 ); }		
+	XMLHandle NextSiblingElement( const char* _value=0 )					{ return XMLHandle( node ? node->NextSiblingElement( _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 ); }
+	XMLNode* ToNode()							{ return node; } 
+	XMLElement* ToElement() 					{ return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
+	XMLText* ToText() 							{ return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
+	XMLUnknown* ToUnknown() 					{ return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
+	XMLDeclaration* ToDeclaration() 			{ return ( ( node && node->ToDeclaration() ) ? node->ToDeclaration() : 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 ); }
+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 ); }
 
 
-	XMLNode* ToNode() 							{ return node; } 
 	const XMLNode* 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 ); }
 	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;
+	const XMLNode* node;
 };
 
 

+ 2 - 2
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" );
 
 	}