Proxy class to conveniently convert XML structure to user data. More...
#include <bind.h>
Public Member Functions | |
XmlIn (const XmlDoc &doc) | |
Construct an input proxy for an XML document. More... | |
XmlIn (const XmlElement *element) | |
Construct an input proxy for a single XML element, may be nullptr. More... | |
XmlIn (const XmlElement &element) | |
Construct an input proxy for a single XML element. More... | |
template<class String > | |
XmlIn | operator[] (const String &name) const |
Retrieve a handle to an XML child element for reading. More... | |
void | next () |
Refer to next sibling element with the same name. More... | |
template<class T > | |
bool | operator() (T &value) const |
Read user data from the underlying XML element. More... | |
template<class String , class T > | |
bool | attribute (const String &name, T &value) const |
Read user data from an XML attribute. More... | |
const XmlElement * | get () const |
Return a pointer to the underlying Xml element, may be nullptr. | |
operator bool () const | |
Test whether the underlying XML element exists. More... | |
bool | errorsOccured () const |
Notifies errors while mapping the XML to user data. More... | |
template<class String > | |
std::vector< String > | getErrorsAs () const |
Get a list of XML element and attribute names which failed to convert to user data. More... | |
Proxy class to conveniently convert XML structure to user data.
zen::XmlIn::XmlIn | ( | const XmlDoc & | doc | ) |
Construct an input proxy for an XML document.
zen::XmlIn::XmlIn | ( | const XmlElement * | element | ) |
Construct an input proxy for a single XML element, may be nullptr.
zen::XmlIn::XmlIn | ( | const XmlElement & | element | ) |
Construct an input proxy for a single XML element.
bool zen::XmlIn::attribute | ( | const String & | name, |
T & | value | ||
) | const |
Read user data from an XML attribute.
This conversion requires a specialization of zen::readText() for type T.
String | Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... |
T | String-convertible user data type: e.g. any string-like type, all built-in arithmetic numbers |
bool zen::XmlIn::errorsOccured | ( | ) | const |
Notifies errors while mapping the XML to user data.
Error logging is shared by each hiearchy of XmlIn proxy instances that are created from each other. Consequently it doesn't matter which instance you query for errors:
Note that error logging is NOT global, but owned by all instances of a hierarchy of XmlIn proxies. Therefore it's safe to use unrelated XmlIn proxies in multiple threads.
However be aware that the chain of connected proxy instances will be broken once you call XmlIn::get() to retrieve the underlying pointer. Errors that occur when working with this pointer are not logged by the original set of related instances.
std::vector<String> zen::XmlIn::getErrorsAs | ( | ) | const |
Get a list of XML element and attribute names which failed to convert to user data.
String | Arbitrary string class: e.g. std::string, std::wstring, wxString, MyStringClass, ... |
void zen::XmlIn::next | ( | ) |
Refer to next sibling element with the same name.
Example: Loop over all XML child elements named "Item"
<?xml version="1.0" encoding="UTF-8"?> <Root> <Item>1</Item> <Item>3</Item> <Item>5</Item> </Root>
|
explicit |
bool zen::XmlIn::operator() | ( | T & | value | ) | const |
Read user data from the underlying XML element.
This conversion requires a specialization of zen::readText() or zen::readStruc() for type T.
T | User type that receives the data |
XmlIn zen::XmlIn::operator[] | ( | const String & | name | ) | const |
Retrieve a handle to an XML child element for reading.
It is not an error if the child element does not exist, but only later if a conversion to user data is attempted.
String | Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... |
name | The name of the child element |