فهرست منبع

Add the (very handy) QueryAttribute

Lee Thomason (grinliz) 13 سال پیش
والد
کامیت
5efaa5f6bb
2فایلهای تغییر یافته به همراه46 افزوده شده و 3 حذف شده
  1. 39 1
      tinyxml2.h
  2. 7 2
      xmltest.cpp

+ 39 - 1
tinyxml2.h

@@ -1224,7 +1224,45 @@ public:
         return a->QueryFloatValue( value );
     }
 
-    /// Sets the named attribute to value.
+	
+    /** Given an attribute name, QueryAttribute() returns
+    	XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
+    	can't be performed, or XML_NO_ATTRIBUTE if the attribute
+    	doesn't exist. It is overloaded for the primitive types,
+		and is a generally more convenient replacement of
+		QueryIntAttribute() and related functions.
+		
+		If successful, the result of the conversion
+    	will be written to 'value'. If not successful, nothing will
+    	be written to 'value'. This allows you to provide default
+    	value:
+
+    	@verbatim
+    	int value = 10;
+    	QueryAttribute( "foo", &value );		// if "foo" isn't found, value will still be 10
+    	@endverbatim
+    */
+	int QueryAttribute( const char* name, int* value ) const {
+		return QueryIntAttribute( name, value );
+	}
+
+	int QueryAttribute( const char* name, unsigned int* value ) const {
+		return QueryUnsignedAttribute( name, value );
+	}
+
+	int QueryAttribute( const char* name, bool* value ) const {
+		return QueryBoolAttribute( name, value );
+	}
+
+	int QueryAttribute( const char* name, double* value ) const {
+		return QueryDoubleAttribute( name, value );
+	}
+
+	int QueryAttribute( const char* name, float* value ) const {
+		return QueryFloatAttribute( name, value );
+	}
+
+	/// Sets the named attribute to value.
     void SetAttribute( const char* name, const char* value )	{
         XMLAttribute* a = FindOrCreateAttribute( name );
         a->SetAttribute( value );

+ 7 - 2
xmltest.cpp

@@ -505,8 +505,8 @@ int main( int argc, const char ** argv )
 
 		XMLElement* ele = doc.FirstChildElement();
 
-		int iVal;
-		double dVal;
+		int iVal, iVal2;
+		double dVal, dVal2;
 
 		ele->SetAttribute( "str", "strValue" );
 		ele->SetAttribute( "int", 1 );
@@ -516,10 +516,15 @@ int main( int argc, const char ** argv )
 		ele->QueryIntAttribute( "int", &iVal );
 		ele->QueryDoubleAttribute( "double", &dVal );
 
+		ele->QueryAttribute( "int", &iVal2 );
+		ele->QueryAttribute( "double", &dVal2 );
+
 		XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
 		XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
 		XMLTest( "Attribute round trip. int.", 1, iVal );
 		XMLTest( "Attribute round trip. double.", -1, (int)dVal );
+		XMLTest( "Alternate query", true, iVal == iVal2 );
+		XMLTest( "Alternate query", true, dVal == dVal2 );
 	}
 
 	{