aGrUM  0.20.3
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. More...
 
 TiXmlHandle (const TiXmlHandle &ref)
 Copy constructor. More...
 
TiXmlHandle operator= (const TiXmlHandle &ref)
 
TiXmlHandle FirstChild () const
 Return a handle to the first child node. More...
 
TiXmlHandle FirstChild (const char *value) const
 Return a handle to the first child node with the given name. More...
 
TiXmlHandle FirstChildElement () const
 Return a handle to the first child element. More...
 
TiXmlHandle FirstChildElement (const char *value) const
 Return a handle to the first child element with the given name. More...
 
TiXmlHandle Child (const char *value, int index) const
 Return a handle to the "index" child with the given name. More...
 
TiXmlHandle Child (int index) const
 Return a handle to the "index" child. More...
 
TiXmlHandle ChildElement (const char *value, int index) const
 Return a handle to the "index" child element with the given name. More...
 
TiXmlHandle ChildElement (int index) const
 Return a handle to the "index" child element. More...
 
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. More...
 
TiXmlElementToElement () const
 Return the handle as a TiXmlElement. More...
 
TiXmlTextToText () const
 Return the handle as a TiXmlText. More...
 
TiXmlUnknownToUnknown () const
 Return the handle as a TiXmlUnknown. More...
 
TiXmlNodeNode () const
 
TiXmlElementElement () const
 
TiXmlTextText () const
 
TiXmlUnknownUnknown () const
 

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.

References node.

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

1956 { this->node = _node; }
TiXmlNode * node
Definition: tinyxml.h:2047
+ 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.

References node.

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

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.

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

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
TiXmlNode * node
Definition: tinyxml.h:2047
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:552
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:454
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
Definition: tinyxml.h:695
+ Here is the call graph for this function:

◆ Child() [2/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.

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

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 }
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
TiXmlNode * node
Definition: tinyxml.h:2047
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:552
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:454
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
Definition: tinyxml.h:695
+ Here is the call graph for this function:

◆ Child() [3/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

◆ 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.

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

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 }
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 TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:384
const TiXmlElement * NextSiblingElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:404
TiXmlNode * node
Definition: tinyxml.h:2047
The element is a container class.
Definition: tinyxml.h:1095
+ Here is the call graph for this function:

◆ ChildElement() [2/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.

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

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 }
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 TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:384
const TiXmlElement * NextSiblingElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:404
TiXmlNode * node
Definition: tinyxml.h:2047
The element is a container class.
Definition: tinyxml.h:1095
+ Here is the call graph for this function:

◆ ChildElement() [3/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

◆ 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.

References ToElement().

2036 { return ToElement(); }
TiXmlElement * ToElement() const
Return the handle as a TiXmlElement.
Definition: tinyxml.h:2015
+ 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.

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

1535  {
1536  if (node) {
1537  TiXmlNode* child = node->FirstChild();
1538 
1539  if (child) return TiXmlHandle(child);
1540  }
1541 
1542  return TiXmlHandle(0);
1543 }
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
TiXmlNode * node
Definition: tinyxml.h:2047
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:552
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:454
+ Here is the call 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.

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

1545  {
1546  if (node) {
1547  TiXmlNode* child = node->FirstChild(value);
1548 
1549  if (child) return TiXmlHandle(child);
1550  }
1551 
1552  return TiXmlHandle(0);
1553 }
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
TiXmlNode * node
Definition: tinyxml.h:2047
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:552
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:454
+ 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

◆ FirstChildElement() [1/3]

TiXmlHandle TiXmlHandle::FirstChildElement ( ) const

Return a handle to the first child element.

Definition at line 1555 of file tinyxml.cpp.

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

1555  {
1556  if (node) {
1557  TiXmlElement* child = node->FirstChildElement();
1558 
1559  if (child) return TiXmlHandle(child);
1560  }
1561 
1562  return TiXmlHandle(0);
1563 }
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 TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:384
TiXmlNode * node
Definition: tinyxml.h:2047
The element is a container class.
Definition: tinyxml.h:1095
+ Here is the call 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.

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

1565  {
1566  if (node) {
1567  TiXmlElement* child = node->FirstChildElement(value);
1568 
1569  if (child) return TiXmlHandle(child);
1570  }
1571 
1572  return TiXmlHandle(0);
1573 }
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 TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:384
TiXmlNode * node
Definition: tinyxml.h:2047
The element is a container class.
Definition: tinyxml.h:1095
+ 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

◆ 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.

References ToNode().

2032 { return ToNode(); }
TiXmlNode * ToNode() const
Return the handle as a TiXmlNode.
Definition: tinyxml.h:2012
+ Here is the call graph for this function:

◆ operator=()

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

Definition at line 1959 of file tinyxml.h.

References node.

1959  {
1960  this->node = ref.node;
1961  return *this;
1962  }
TiXmlNode * node
Definition: tinyxml.h:2047

◆ 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.

References ToText().

2040 { return ToText(); }
TiXmlText * ToText() const
Return the handle as a TiXmlText.
Definition: tinyxml.h:2020
+ 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.

References node, and TiXmlNode::ToElement().

Referenced by Element().

2015  {
2016  return ((node && node->ToElement()) ? node->ToElement() : 0);
2017  }
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:788
TiXmlNode * node
Definition: tinyxml.h:2047
+ Here is the call graph for this function:
+ 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.

References node.

Referenced by Node().

2012 { return node; }
TiXmlNode * node
Definition: tinyxml.h:2047
+ 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.

References node, and TiXmlNode::ToText().

Referenced by Text().

2020  {
2021  return ((node && node->ToText()) ? node->ToText() : 0);
2022  }
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:800
TiXmlNode * node
Definition: tinyxml.h:2047
+ Here is the call graph for this function:
+ 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.

References node, and TiXmlNode::ToUnknown().

Referenced by Unknown().

2025  {
2026  return ((node && node->ToUnknown()) ? node->ToUnknown() : 0);
2027  }
TiXmlNode * node
Definition: tinyxml.h:2047
virtual const TiXmlUnknown * ToUnknown() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:796
+ Here is the call graph for this function:
+ 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.

References ToUnknown().

2044 { return ToUnknown(); }
TiXmlUnknown * ToUnknown() const
Return the handle as a TiXmlUnknown.
Definition: tinyxml.h:2025
+ Here is the call graph for this function:

Member Data Documentation

◆ node

TiXmlNode* TiXmlHandle::node
private

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