OBT::TiXmlHandle Class Reference

A TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing. More...

#include <OBTtinyxml.h>

Collaboration diagram for OBT::TiXmlHandle:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 TiXmlHandle (TiXmlNode *_node)
 Create a handle from any node (at any depth of the tree.) This can be a null pointer.
 TiXmlHandle (const TiXmlHandle &ref)
 Copy constructor.
TiXmlHandle operator= (const TiXmlHandle &ref)
TiXmlHandle FirstChild () const
 Return a handle to the first child node.
TiXmlHandle FirstChild (const char *value) const
 Return a handle to the first child node with the given name.
TiXmlHandle FirstChildElement () const
 Return a handle to the first child element.
TiXmlHandle FirstChildElement (const char *value) const
 Return a handle to the first child element with the given name.
TiXmlHandle Child (const char *value, int index) const
 Return a handle to the "index" child with the given name.
TiXmlHandle Child (int index) const
 Return a handle to the "index" child.
TiXmlHandle ChildElement (const char *value, int index) const
 Return a handle to the "index" child element with the given name.
TiXmlHandle ChildElement (int index) const
 Return a handle to the "index" child element.
TiXmlHandle FirstChild (const std::string &_value) const
TiXmlHandle FirstChildElement (const std::string &_value) const
TiXmlHandle Child (const std::string &_value, int index) const
TiXmlHandle ChildElement (const std::string &_value, int index) const
TiXmlNodeToNode () const
 Return the handle as a TiXmlNode.
TiXmlElementToElement () const
 Return the handle as a TiXmlElement.
TiXmlTextToText () const
 Return the handle as a TiXmlText.
TiXmlUnknownToUnknown () const
 Return the handle as a TiXmlUnknown.
TiXmlNodeNode () const
TiXmlElementElement () const
TiXmlTextText () const
TiXmlUnknownUnknown () const
 TiXmlHandle (TiXmlNode *_node)
 Create a handle from any node (at any depth of the tree.) This can be a null pointer.
 TiXmlHandle (const TiXmlHandle &ref)
 Copy constructor.
TiXmlHandle operator= (const TiXmlHandle &ref)
TiXmlHandle FirstChild () const
 Return a handle to the first child node.
TiXmlHandle FirstChild (const char *value) const
 Return a handle to the first child node with the given name.
TiXmlHandle FirstChildElement () const
 Return a handle to the first child element.
TiXmlHandle FirstChildElement (const char *value) const
 Return a handle to the first child element with the given name.
TiXmlHandle Child (const char *value, int index) const
 Return a handle to the "index" child with the given name.
TiXmlHandle Child (int index) const
 Return a handle to the "index" child.
TiXmlHandle ChildElement (const char *value, int index) const
 Return a handle to the "index" child element with the given name.
TiXmlHandle ChildElement (int index) const
 Return a handle to the "index" child element.
TiXmlHandle FirstChild (const std::string &_value) const
TiXmlHandle FirstChildElement (const std::string &_value) const
TiXmlHandle Child (const std::string &_value, int index) const
TiXmlHandle ChildElement (const std::string &_value, int index) const
TiXmlNodeToNode () const
 Return the handle as a TiXmlNode.
TiXmlElementToElement () const
 Return the handle as a TiXmlElement.
TiXmlTextToText () const
 Return the handle as a TiXmlText.
TiXmlUnknownToUnknown () const
 Return the handle as a TiXmlUnknown.
TiXmlNodeNode () const
TiXmlElementElement () const
TiXmlTextText () const
TiXmlUnknownUnknown () const

Private Attributes

TiXmlNodenode
TiXmlNodenode


Detailed Description

A TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing.

Note that TiXmlHandle is not part of the TinyXml DOM structure. It is a separate utility class.

Take an example:

	<Document>
		<Element attributeA = "valueA">
			<Child attributeB = "value1" />
			<Child attributeB = "value2" />
		</Element>
	<Document>
	

Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very easy to write a *lot* of code that looks like:

	TiXmlElement* root = document.FirstChildElement( "Document" );
	if ( root )
	{
		TiXmlElement* element = root->FirstChildElement( "Element" );
		if ( element )
		{
			TiXmlElement* child = element->FirstChildElement( "Child" );
			if ( child )
			{
				TiXmlElement* child2 = child->NextSiblingElement( "Child" );
				if ( child2 )
				{
					// Finally do something useful.
	

And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity of such code. A TiXmlHandle checks for null pointers so it is perfectly safe and correct to use:

	TiXmlHandle docHandle( &document );
	TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
	if ( child2 )
	{
		// do something useful
	

Which is MUCH more concise and useful.

It is also safe to copy handles - internally they are nothing more than node pointers.

	TiXmlHandle handleCopy = handle;
	

What they should not be used for is iteration:

	int i=0; 
	while ( true )
	{
		TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
		if ( !child )
			break;
		// do something
		++i;
	}
	

It seems reasonable, but it is in fact two embedded while loops. The Child method is a linear walk to find the element, so this code would iterate much more than it needs to. Instead, prefer:

	TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();

	for( child; child; child=child->NextSiblingElement() )
	{
		// do something
	}
	

Definition at line 1641 of file OBTtinyxml.h.


Constructor & Destructor Documentation

OBT::TiXmlHandle::TiXmlHandle ( TiXmlNode _node  )  [inline]

Create a handle from any node (at any depth of the tree.) This can be a null pointer.

Definition at line 1645 of file OBTtinyxml.h.

Referenced by Child(), ChildElement(), FirstChild(), and FirstChildElement().

01645 { this->node = _node; }

OBT::TiXmlHandle::TiXmlHandle ( const TiXmlHandle ref  )  [inline]

Copy constructor.

Definition at line 1647 of file OBTtinyxml.h.

References node.

01647 { this->node = ref.node; }

OBT::TiXmlHandle::TiXmlHandle ( TiXmlNode _node  )  [inline]

Create a handle from any node (at any depth of the tree.) This can be a null pointer.

Definition at line 1644 of file tiny/OBTtinyxml.h.

References node.

01644 { this->node = _node; }

OBT::TiXmlHandle::TiXmlHandle ( const TiXmlHandle ref  )  [inline]

Copy constructor.

Definition at line 1646 of file tiny/OBTtinyxml.h.

References node.

01646 { this->node = ref.node; }


Member Function Documentation

TiXmlHandle OBT::TiXmlHandle::operator= ( const TiXmlHandle ref  )  [inline]

Definition at line 1648 of file OBTtinyxml.h.

References node.

01648 { this->node = ref.node; return *this; }

TiXmlHandle OBT::TiXmlHandle::FirstChild (  )  const

Return a handle to the first child node.

Definition at line 1636 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChild(), node, and TiXmlHandle().

Referenced by FirstChild().

01637 {
01638         if ( node )
01639         {
01640                 TiXmlNode* child = node->FirstChild();
01641                 if ( child )
01642                         return TiXmlHandle( child );
01643         }
01644         return TiXmlHandle( 0 );
01645 }

TiXmlHandle OBT::TiXmlHandle::FirstChild ( const char *  value  )  const

Return a handle to the first child node with the given name.

Definition at line 1648 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChild(), node, and TiXmlHandle().

01649 {
01650         if ( node )
01651         {
01652                 TiXmlNode* child = node->FirstChild( value );
01653                 if ( child )
01654                         return TiXmlHandle( child );
01655         }
01656         return TiXmlHandle( 0 );
01657 }

TiXmlHandle OBT::TiXmlHandle::FirstChildElement (  )  const

Return a handle to the first child element.

Definition at line 1660 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChildElement(), node, and TiXmlHandle().

Referenced by FirstChildElement().

01661 {
01662         if ( node )
01663         {
01664                 TiXmlElement* child = node->FirstChildElement();
01665                 if ( child )
01666                         return TiXmlHandle( child );
01667         }
01668         return TiXmlHandle( 0 );
01669 }

TiXmlHandle OBT::TiXmlHandle::FirstChildElement ( const char *  value  )  const

Return a handle to the first child element with the given name.

Definition at line 1672 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChildElement(), node, and TiXmlHandle().

01673 {
01674         if ( node )
01675         {
01676                 TiXmlElement* child = node->FirstChildElement( value );
01677                 if ( child )
01678                         return TiXmlHandle( child );
01679         }
01680         return TiXmlHandle( 0 );
01681 }

TiXmlHandle OBT::TiXmlHandle::Child ( const char *  value,
int  index 
) const

Return a handle to the "index" child with the given name.

The first child is 0, the second 1, etc.

Definition at line 1703 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChild(), OBT::TiXmlNode::NextSibling(), node, and TiXmlHandle().

Referenced by Child().

01704 {
01705         if ( node )
01706         {
01707                 int i;
01708                 TiXmlNode* child = node->FirstChild( value );
01709                 for (   i=0;
01710                                 child && i<count;
01711                                 child = child->NextSibling( value ), ++i )
01712                 {
01713                         // nothing
01714                 }
01715                 if ( child )
01716                         return TiXmlHandle( child );
01717         }
01718         return TiXmlHandle( 0 );
01719 }

TiXmlHandle OBT::TiXmlHandle::Child ( int  index  )  const

Return a handle to the "index" child.

The first child is 0, the second 1, etc.

Definition at line 1684 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChild(), OBT::TiXmlNode::NextSibling(), node, and TiXmlHandle().

01685 {
01686         if ( node )
01687         {
01688                 int i;
01689                 TiXmlNode* child = node->FirstChild();
01690                 for (   i=0;
01691                                 child && i<count;
01692                                 child = child->NextSibling(), ++i )
01693                 {
01694                         // nothing
01695                 }
01696                 if ( child )
01697                         return TiXmlHandle( child );
01698         }
01699         return TiXmlHandle( 0 );
01700 }

TiXmlHandle OBT::TiXmlHandle::ChildElement ( const char *  value,
int  index 
) const

Return a handle to the "index" child element with the given name.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

Definition at line 1741 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChildElement(), OBT::TiXmlNode::NextSiblingElement(), node, and TiXmlHandle().

Referenced by ChildElement().

01742 {
01743         if ( node )
01744         {
01745                 int i;
01746                 TiXmlElement* child = node->FirstChildElement( value );
01747                 for (   i=0;
01748                                 child && i<count;
01749                                 child = child->NextSiblingElement( value ), ++i )
01750                 {
01751                         // nothing
01752                 }
01753                 if ( child )
01754                         return TiXmlHandle( child );
01755         }
01756         return TiXmlHandle( 0 );
01757 }

TiXmlHandle OBT::TiXmlHandle::ChildElement ( int  index  )  const

Return a handle to the "index" child element.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

Definition at line 1722 of file OBTtinyxml.cpp.

References OBT::TiXmlNode::FirstChildElement(), OBT::TiXmlNode::NextSiblingElement(), node, and TiXmlHandle().

01723 {
01724         if ( node )
01725         {
01726                 int i;
01727                 TiXmlElement* child = node->FirstChildElement();
01728                 for (   i=0;
01729                                 child && i<count;
01730                                 child = child->NextSiblingElement(), ++i )
01731                 {
01732                         // nothing
01733                 }
01734                 if ( child )
01735                         return TiXmlHandle( child );
01736         }
01737         return TiXmlHandle( 0 );
01738 }

TiXmlHandle OBT::TiXmlHandle::FirstChild ( const std::string &  _value  )  const [inline]

Definition at line 1679 of file OBTtinyxml.h.

01679 { return FirstChild( _value.c_str() ); }

TiXmlHandle OBT::TiXmlHandle::FirstChildElement ( const std::string &  _value  )  const [inline]

Definition at line 1680 of file OBTtinyxml.h.

01680 { return FirstChildElement( _value.c_str() ); }

TiXmlHandle OBT::TiXmlHandle::Child ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1682 of file OBTtinyxml.h.

01682 { return Child( _value.c_str(), index ); }

TiXmlHandle OBT::TiXmlHandle::ChildElement ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1683 of file OBTtinyxml.h.

01683 { return ChildElement( _value.c_str(), index ); }

TiXmlNode* OBT::TiXmlHandle::ToNode (  )  const [inline]

Return the handle as a TiXmlNode.

This may return null.

Definition at line 1688 of file OBTtinyxml.h.

Referenced by Node().

01688 { return node; } 

TiXmlElement* OBT::TiXmlHandle::ToElement (  )  const [inline]

Return the handle as a TiXmlElement.

This may return null.

Definition at line 1691 of file OBTtinyxml.h.

Referenced by Element().

01691 { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }

TiXmlText* OBT::TiXmlHandle::ToText (  )  const [inline]

Return the handle as a TiXmlText.

This may return null.

Definition at line 1694 of file OBTtinyxml.h.

Referenced by Text().

01694 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }

TiXmlUnknown* OBT::TiXmlHandle::ToUnknown (  )  const [inline]

Return the handle as a TiXmlUnknown.

This may return null.

Definition at line 1697 of file OBTtinyxml.h.

Referenced by Unknown().

01697 { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }

TiXmlNode* OBT::TiXmlHandle::Node (  )  const [inline]

Definition at line 1702 of file OBTtinyxml.h.

01702 { return ToNode(); } 

TiXmlElement* OBT::TiXmlHandle::Element (  )  const [inline]

Definition at line 1706 of file OBTtinyxml.h.

01706 { return ToElement(); }

TiXmlText* OBT::TiXmlHandle::Text (  )  const [inline]

Definition at line 1710 of file OBTtinyxml.h.

01710 { return ToText(); }

TiXmlUnknown* OBT::TiXmlHandle::Unknown (  )  const [inline]

Definition at line 1714 of file OBTtinyxml.h.

01714 { return ToUnknown(); }

TiXmlHandle OBT::TiXmlHandle::operator= ( const TiXmlHandle ref  )  [inline]

Definition at line 1647 of file tiny/OBTtinyxml.h.

References node.

01647 { this->node = ref.node; return *this; }

TiXmlHandle OBT::TiXmlHandle::FirstChild (  )  const

Return a handle to the first child node.

TiXmlHandle OBT::TiXmlHandle::FirstChild ( const char *  value  )  const

Return a handle to the first child node with the given name.

TiXmlHandle OBT::TiXmlHandle::FirstChildElement (  )  const

Return a handle to the first child element.

TiXmlHandle OBT::TiXmlHandle::FirstChildElement ( const char *  value  )  const

Return a handle to the first child element with the given name.

TiXmlHandle OBT::TiXmlHandle::Child ( const char *  value,
int  index 
) const

Return a handle to the "index" child with the given name.

The first child is 0, the second 1, etc.

TiXmlHandle OBT::TiXmlHandle::Child ( int  index  )  const

Return a handle to the "index" child.

The first child is 0, the second 1, etc.

TiXmlHandle OBT::TiXmlHandle::ChildElement ( const char *  value,
int  index 
) const

Return a handle to the "index" child element with the given name.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

TiXmlHandle OBT::TiXmlHandle::ChildElement ( int  index  )  const

Return a handle to the "index" child element.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

TiXmlHandle OBT::TiXmlHandle::FirstChild ( const std::string &  _value  )  const [inline]

Definition at line 1678 of file tiny/OBTtinyxml.h.

References FirstChild().

01678 { return FirstChild( _value.c_str() ); }

TiXmlHandle OBT::TiXmlHandle::FirstChildElement ( const std::string &  _value  )  const [inline]

Definition at line 1679 of file tiny/OBTtinyxml.h.

References FirstChildElement().

01679 { return FirstChildElement( _value.c_str() ); }

TiXmlHandle OBT::TiXmlHandle::Child ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1681 of file tiny/OBTtinyxml.h.

References Child().

01681 { return Child( _value.c_str(), index ); }

TiXmlHandle OBT::TiXmlHandle::ChildElement ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1682 of file tiny/OBTtinyxml.h.

References ChildElement().

01682 { return ChildElement( _value.c_str(), index ); }

TiXmlNode* OBT::TiXmlHandle::ToNode (  )  const [inline]

Return the handle as a TiXmlNode.

This may return null.

Definition at line 1687 of file tiny/OBTtinyxml.h.

References node.

01687 { return node; } 

TiXmlElement* OBT::TiXmlHandle::ToElement (  )  const [inline]

Return the handle as a TiXmlElement.

This may return null.

Definition at line 1690 of file tiny/OBTtinyxml.h.

References node, and OBT::TiXmlNode::ToElement().

01690 { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }

TiXmlText* OBT::TiXmlHandle::ToText (  )  const [inline]

Return the handle as a TiXmlText.

This may return null.

Definition at line 1693 of file tiny/OBTtinyxml.h.

References node, and OBT::TiXmlNode::ToText().

01693 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }

TiXmlUnknown* OBT::TiXmlHandle::ToUnknown (  )  const [inline]

Return the handle as a TiXmlUnknown.

This may return null.

Definition at line 1696 of file tiny/OBTtinyxml.h.

References node, and OBT::TiXmlNode::ToUnknown().

01696 { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }

TiXmlNode* OBT::TiXmlHandle::Node (  )  const [inline]

Definition at line 1701 of file tiny/OBTtinyxml.h.

References ToNode().

01701 { return ToNode(); } 

TiXmlElement* OBT::TiXmlHandle::Element (  )  const [inline]

Definition at line 1705 of file tiny/OBTtinyxml.h.

References ToElement().

01705 { return ToElement(); }

TiXmlText* OBT::TiXmlHandle::Text (  )  const [inline]

Definition at line 1709 of file tiny/OBTtinyxml.h.

References ToText().

01709 { return ToText(); }

TiXmlUnknown* OBT::TiXmlHandle::Unknown (  )  const [inline]

Definition at line 1713 of file tiny/OBTtinyxml.h.

References ToUnknown().

01713 { return ToUnknown(); }


Member Data Documentation

TiXmlNode* OBT::TiXmlHandle::node [private]

Definition at line 1717 of file OBTtinyxml.h.

Referenced by Child(), ChildElement(), FirstChild(), FirstChildElement(), operator=(), TiXmlHandle(), ToElement(), ToNode(), ToText(), and ToUnknown().

TiXmlNode* OBT::TiXmlHandle::node [private]

Definition at line 1716 of file tiny/OBTtinyxml.h.


Generated on Wed Oct 1 11:34:22 2008 for OBT by  doxygen 1.5.3