aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
TiXmlHandle Class Reference

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

#include <tinyxml.h>

Collaboration diagram for TiXmlHandle:

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

Private Attributes

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 1951 of file tinyxml.h.

Constructor & Destructor Documentation

◆ TiXmlHandle() [1/2]

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 1956 of file tinyxml.h.

1956{ this->node = _node; }
TiXmlNode * node
Definition tinyxml.h:2047

References node.

Referenced by TiXmlHandle(), Child(), Child(), Child(), ChildElement(), ChildElement(), ChildElement(), FirstChild(), FirstChild(), FirstChild(), FirstChildElement(), FirstChildElement(), FirstChildElement(), and operator=().

Here is the caller graph for this function:

◆ TiXmlHandle() [2/2]

TiXmlHandle::TiXmlHandle ( const TiXmlHandle & ref)
inline

Copy constructor.

Definition at line 1958 of file tinyxml.h.

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

References TiXmlHandle(), and node.

Here is the call graph for this function:

Member Function Documentation

◆ Child() [1/3]

TiXmlHandle 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 1590 of file tinyxml.cpp.

1590 {
1591 if (node) {
1592 int i;
1593 TiXmlNode* child = node->FirstChild(value);
1594
1595 for (i = 0; child && i < count; child = child->NextSibling(value), ++i) {
1596 // nothing
1597 }
1598
1599 if (child) return TiXmlHandle(child);
1600 }
1601
1602 return TiXmlHandle(0);
1603}
TiXmlHandle(TiXmlNode *_node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
Definition tinyxml.h:1956
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
Definition tinyxml.h:695

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

Referenced by Child().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Child() [2/3]

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

Definition at line 2002 of file tinyxml.h.

2002 {
2003 return Child(_value.c_str(), index);
2004 }
TiXmlHandle Child(const char *value, int index) const
Return a handle to the "index" child with the given name.
Definition tinyxml.cpp:1590

References TiXmlHandle(), and Child().

Here is the call graph for this function:

◆ Child() [3/3]

TiXmlHandle TiXmlHandle::Child ( int index) const

Return a handle to the "index" child.

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

Definition at line 1575 of file tinyxml.cpp.

1575 {
1576 if (node) {
1577 int i;
1578 TiXmlNode* child = node->FirstChild();
1579
1580 for (i = 0; child && i < count; child = child->NextSibling(), ++i) {
1581 // nothing
1582 }
1583
1584 if (child) return TiXmlHandle(child);
1585 }
1586
1587 return TiXmlHandle(0);
1588}

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

Here is the call graph for this function:

◆ ChildElement() [1/3]

TiXmlHandle 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 1620 of file tinyxml.cpp.

1620 {
1621 if (node) {
1622 int i;
1623 TiXmlElement* child = node->FirstChildElement(value);
1624
1625 for (i = 0; child && i < count;
1626 child = child->NextSiblingElement(value), ++i) {
1627 // nothing
1628 }
1629
1630 if (child) return TiXmlHandle(child);
1631 }
1632
1633 return TiXmlHandle(0);
1634}
const TiXmlElement * NextSiblingElement() const
Convenience function to get through elements.
Definition tinyxml.cpp:404

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

Referenced by ChildElement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ChildElement() [2/3]

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

Definition at line 2005 of file tinyxml.h.

2005 {
2006 return ChildElement(_value.c_str(), index);
2007 }
TiXmlHandle ChildElement(const char *value, int index) const
Return a handle to the "index" child element with the given name.
Definition tinyxml.cpp:1620

References TiXmlHandle(), and ChildElement().

Here is the call graph for this function:

◆ ChildElement() [3/3]

TiXmlHandle 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 1605 of file tinyxml.cpp.

1605 {
1606 if (node) {
1607 int i;
1608 TiXmlElement* child = node->FirstChildElement();
1609
1610 for (i = 0; child && i < count; child = child->NextSiblingElement(), ++i) {
1611 // nothing
1612 }
1613
1614 if (child) return TiXmlHandle(child);
1615 }
1616
1617 return TiXmlHandle(0);
1618}

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

Here is the call graph for this function:

◆ Element()

TiXmlElement * TiXmlHandle::Element ( ) const
inline
Deprecated
use ToElement. Return the handle as a TiXmlElement. This may return null.

Definition at line 2036 of file tinyxml.h.

2036{ return ToElement(); }
TiXmlElement * ToElement() const
Return the handle as a TiXmlElement.
Definition tinyxml.h:2015

References ToElement().

Here is the call graph for this function:

◆ FirstChild() [1/3]

TiXmlHandle TiXmlHandle::FirstChild ( ) const

Return a handle to the first child node.

Definition at line 1535 of file tinyxml.cpp.

1535 {
1536 if (node) {
1537 TiXmlNode* child = node->FirstChild();
1538
1539 if (child) return TiXmlHandle(child);
1540 }
1541
1542 return TiXmlHandle(0);
1543}

References TiXmlHandle(), and node.

Referenced by FirstChild().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FirstChild() [2/3]

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

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

Definition at line 1545 of file tinyxml.cpp.

1545 {
1546 if (node) {
1547 TiXmlNode* child = node->FirstChild(value);
1548
1549 if (child) return TiXmlHandle(child);
1550 }
1551
1552 return TiXmlHandle(0);
1553}

References TiXmlHandle(), and node.

Here is the call graph for this function:

◆ FirstChild() [3/3]

TiXmlHandle TiXmlHandle::FirstChild ( const std::string & _value) const
inline

Definition at line 1995 of file tinyxml.h.

1995 {
1996 return FirstChild(_value.c_str());
1997 }
TiXmlHandle FirstChild() const
Return a handle to the first child node.
Definition tinyxml.cpp:1535

References TiXmlHandle(), and FirstChild().

Here is the call graph for this function:

◆ FirstChildElement() [1/3]

TiXmlHandle TiXmlHandle::FirstChildElement ( ) const

Return a handle to the first child element.

Definition at line 1555 of file tinyxml.cpp.

1555 {
1556 if (node) {
1557 TiXmlElement* child = node->FirstChildElement();
1558
1559 if (child) return TiXmlHandle(child);
1560 }
1561
1562 return TiXmlHandle(0);
1563}

References TiXmlHandle(), and node.

Referenced by FirstChildElement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FirstChildElement() [2/3]

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

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

Definition at line 1565 of file tinyxml.cpp.

1565 {
1566 if (node) {
1567 TiXmlElement* child = node->FirstChildElement(value);
1568
1569 if (child) return TiXmlHandle(child);
1570 }
1571
1572 return TiXmlHandle(0);
1573}

References TiXmlHandle(), and node.

Here is the call graph for this function:

◆ FirstChildElement() [3/3]

TiXmlHandle TiXmlHandle::FirstChildElement ( const std::string & _value) const
inline

Definition at line 1998 of file tinyxml.h.

1998 {
1999 return FirstChildElement(_value.c_str());
2000 }
TiXmlHandle FirstChildElement() const
Return a handle to the first child element.
Definition tinyxml.cpp:1555

References TiXmlHandle(), and FirstChildElement().

Here is the call graph for this function:

◆ Node()

TiXmlNode * TiXmlHandle::Node ( ) const
inline
Deprecated
use ToNode. Return the handle as a TiXmlNode. This may return null.

Definition at line 2032 of file tinyxml.h.

2032{ return ToNode(); }
TiXmlNode * ToNode() const
Return the handle as a TiXmlNode.
Definition tinyxml.h:2012

References ToNode().

Here is the call graph for this function:

◆ operator=()

TiXmlHandle TiXmlHandle::operator= ( const TiXmlHandle & ref)
inline

Definition at line 1959 of file tinyxml.h.

1959 {
1960 this->node = ref.node;
1961 return *this;
1962 }

References TiXmlHandle(), and node.

Here is the call graph for this function:

◆ Text()

TiXmlText * TiXmlHandle::Text ( ) const
inline
Deprecated
use ToText() Return the handle as a TiXmlText. This may return null.

Definition at line 2040 of file tinyxml.h.

2040{ return ToText(); }
TiXmlText * ToText() const
Return the handle as a TiXmlText.
Definition tinyxml.h:2020

References ToText().

Here is the call graph for this function:

◆ ToElement()

TiXmlElement * TiXmlHandle::ToElement ( ) const
inline

Return the handle as a TiXmlElement.

This may return null.

Definition at line 2015 of file tinyxml.h.

2015 {
2016 return ((node && node->ToElement()) ? node->ToElement() : 0);
2017 }

References node.

Referenced by Element().

Here is the caller graph for this function:

◆ ToNode()

TiXmlNode * TiXmlHandle::ToNode ( ) const
inline

Return the handle as a TiXmlNode.

This may return null.

Definition at line 2012 of file tinyxml.h.

2012{ return node; }

References node.

Referenced by Node().

Here is the caller graph for this function:

◆ ToText()

TiXmlText * TiXmlHandle::ToText ( ) const
inline

Return the handle as a TiXmlText.

This may return null.

Definition at line 2020 of file tinyxml.h.

2020 {
2021 return ((node && node->ToText()) ? node->ToText() : 0);
2022 }

References node.

Referenced by Text().

Here is the caller graph for this function:

◆ ToUnknown()

TiXmlUnknown * TiXmlHandle::ToUnknown ( ) const
inline

Return the handle as a TiXmlUnknown.

This may return null.

Definition at line 2025 of file tinyxml.h.

2025 {
2026 return ((node && node->ToUnknown()) ? node->ToUnknown() : 0);
2027 }

References node.

Referenced by Unknown().

Here is the caller graph for this function:

◆ Unknown()

TiXmlUnknown * TiXmlHandle::Unknown ( ) const
inline
Deprecated
use ToUnknown() Return the handle as a TiXmlUnknown. This may return null.

Definition at line 2044 of file tinyxml.h.

2044{ return ToUnknown(); }
TiXmlUnknown * ToUnknown() const
Return the handle as a TiXmlUnknown.
Definition tinyxml.h:2025

References ToUnknown().

Here is the call graph for this function:

Member Data Documentation

◆ node


The documentation for this class was generated from the following files: