45 #define TIXML_USE_TICPP 48 #ifndef TICPP_INCLUDED 49 #define TICPP_INCLUDED 83 const char*
what()
const throw();
92 #define TICPPTHROW(message) 94 std::ostringstream full_message; 95 std::string file( __FILE__); 96 file = file.substr(file.find_last_of("\\/") + 1
); 97 full_message << message << " <" << file << "@" << __LINE__ << ">"; 98 full_message << BuildDetailedErrorString(); 99 throw Exception(full_message.str()); 170 std::stringstream convert;
173 if (convert.fail()) {
174 TICPPTHROW(
"Could not convert value to text");
177 return convert.str();
190 std::istringstream val(temp);
194 TICPPTHROW(
"Could not convert \"" << temp <<
"\" to target type");
201 void FromString(
const std::string& temp, std::string* out)
const {
239 std::ostringstream full_message;
240 #ifndef TICPP_NO_RTTI 248 full_message <<
"\nDescription: " << doc->ErrorDesc() <<
"\nFile: " 249 << (strlen(doc->Value()) > 0 ? doc->Value()
251 <<
"\nLine: " << doc->ErrorRow()
252 <<
"\nColumn: " << doc->ErrorCol();
258 return full_message.str();
283 TICPPTHROW(
"Internal TiXml Pointer is nullptr");
302 return m_tiXmlPointer;
317 Attribute(
const std::string& name,
const std::string& value);
338 FromString(m_tiXmlPointer->ValueStr(), value);
359 m_tiXmlPointer->SetValue(ToString(value));
372 FromString(m_tiXmlPointer->Name(), name);
393 m_tiXmlPointer->SetName(ToString(name));
444 virtual void Print(FILE* file,
int depth)
const;
471 FromString(GetTiXmlPointer()->ValueStr(), value);
491 GetTiXmlPointer()->SetValue(ToString(value));
510 Node*
Parent(
bool throwIfNoParent =
true)
const;
539 Node*
FirstChild(
const char* value,
bool throwIfNoChildren =
true)
const;
553 bool throwIfNoChildren =
true)
const;
582 Node*
LastChild(
const char* value,
bool throwIfNoChildren =
true)
const;
595 Node*
LastChild(
const std::string& value,
bool throwIfNoChildren =
true)
const;
717 bool throwIfNoSiblings =
true)
const;
759 bool throwIfNoSiblings =
true)
const;
773 Node*
NextSibling(
const char* value,
bool throwIfNoSiblings =
true)
const;
786 for (
Node* child = FirstChild(value,
false); child;
787 child = child->NextSibling(value,
false)) {
788 *first =
dynamic_cast< T* >(child);
797 TICPPTHROW(
"Attributes can only be iterated with Elements.")
809 Node* sibling = NextSibling(value,
false);
810 *next =
dynamic_cast< T* >(sibling);
812 while ((0 != sibling) && (0 == *next)) {
813 sibling = sibling->NextSibling(value,
false);
814 *next =
dynamic_cast< T* >(sibling);
827 Node* sibling = PreviousSibling(value,
false);
828 *previous =
dynamic_cast< T* >(sibling);
830 while ((0 != sibling) && (0 == *previous)) {
831 sibling = sibling->PreviousSibling(value,
false);
832 *previous =
dynamic_cast< T* >(sibling);
861 bool throwIfNoSiblings =
true)
const;
876 bool throwIfNoSiblings =
true)
const;
907 bool throwIfNoChildren =
true)
const;
921 bool throwIfNoChildren =
true)
const;
949 #ifndef TICPP_NO_RTTI 962 T* pointer =
dynamic_cast< T* >(
this);
965 std::string thisType =
typeid(
this).name();
966 std::string targetType =
typeid(T).name();
967 std::string thatType =
typeid(*
this).name();
968 TICPPTHROW(
"The " << thisType.substr(6) <<
" could not be casted to a " 969 << targetType.substr(6)
970 <<
" *, because the target object is not a " 971 << targetType.substr(6)
973 << thatType.substr(6)
1054 in >> *base.GetTiXmlPointer();
1062 out << *base.GetTiXmlPointer();
1080 bool throwIfNull =
true,
1081 bool rememberSpawnedWrapper =
true)
const;
1111 template <
class T =
Node >
1129 parent->IterateFirst(m_value, &pointer);
1175 m_value = it.m_value;
1189 m_p->IterateNext(m_value, &m_p);
1206 m_p->IteratePrevious(m_value, &m_p);
1225 if (0 ==
m_p || 0 == p) {
1241 if (0 ==
m_p || 0 == p) {
1259 template <
class T >
1294 if (0 == tiXmlPointer) {
1295 #ifdef TICPP_NO_RTTI 1296 TICPPTHROW(
"Can not create TinyXML objext");
1298 TICPPTHROW(
"Can not create a " <<
typeid(T).name());
1315 this->m_impRC->DecRef();
1321 this->m_impRC->IncRef();
1337 this->m_impRC->IncRef();
1364 Comment(
const std::string& comment);
1385 Text(
const std::string& value);
1396 template <
class T >
1420 Document(
const char* documentName);
1434 Document(
const std::string& documentName);
1462 void LoadFile(
const std::string& filename,
1468 void LoadFile(
const char* filename,
1477 void SaveFile(
const std::string& filename)
const;
1487 void Parse(
const std::string& xml,
1488 bool throwIfParseError =
true,
1504 Element(
const std::string& value);
1522 template <
class T >
1563 *first =
dynamic_cast<
Attribute* >(child);
1580 template <
class T >
1583 m_tiXmlPointer->SetAttribute(name, ToString(value));
1604 if (!GetTextImp(&temp)) {
1605 if (throwIfNotFound) {
1606 TICPPTHROW(
"Text does not exists in the current element");
1630 if (!GetTextImp(&temp)) {
1631 return defaultValue;
1655 template <
class T,
class DefaultT >
1660 if (!GetTextImp(&temp)) {
1662 *value = defaultValue;
1667 FromString(temp, value);
1689 template <
class T >
1690 void GetText(T* value,
bool throwIfNotFound =
true)
const {
1694 if (!GetTextImp(&temp)) {
1695 if (throwIfNotFound) {
1696 TICPPTHROW(
"Text does not exists in the current element");
1703 FromString(temp, value);
1713 template <
class T >
1716 std::string temp = ToString(value);
1718 if (m_tiXmlPointer->NoChildren()) {
1719 m_tiXmlPointer->LinkEndChild(
new TiXmlText(temp));
1721 if (0 == m_tiXmlPointer->GetText()) {
1722 m_tiXmlPointer->InsertBeforeChild(m_tiXmlPointer->FirstChild(),
1726 m_tiXmlPointer->FirstChild()->SetValue(temp);
1745 template <
class T,
class DefaulT >
1748 const DefaulT& defaultValue)
const {
1752 if (!GetAttributeImp(name, &temp)) {
1754 *value = defaultValue;
1759 FromString(temp, value);
1788 template <
class T >
1789 T
GetAttribute(
const std::string& name,
bool throwIfNotFound =
true)
const {
1794 if (!GetAttributeImp(name, &temp)) {
1795 if (throwIfNotFound) {
1796 const std::string error(std::string(
"Attribute '") + name +
1797 std::string(
"' does not exist"));
1802 FromString(temp, &value);
1822 template <
class T >
1825 bool throwIfNotFound =
true)
const {
1829 if (!GetAttributeImp(name, &temp)) {
1830 if (throwIfNotFound) {
1831 const std::string error(std::string(
"Attribute '") + name +
1832 std::string(
"' does not exist"));
1840 FromString(temp, value);
1875 bool GetAttributeImp(
const std::string& name, std::string* value)
const;
1902 const std::string& encoding,
1903 const std::string& standalone);
Wrapper around TiXmlNode.
std::unique_ptr< Node > Clone() const
Create an exact duplicate of this node and return it.
void RemoveChild(Node *removeThis)
Delete a child of this node.
Iterator & operator=(const Iterator &it)
Sets internal pointer.
void IteratePrevious(const std::string &value, T **previous) const
Document(TiXmlDocument *document)
Constructor.
An attribute is a name-value pair.
void operator=(const Attribute ©)
Node * FirstChild(const char *value, bool throwIfNoChildren=true) const
If you call the Accept() method, it requires being passed a TiXmlVisitor class to handle callbacks...
Node * PreviousSibling(const char *value, bool throwIfNoSiblings=true) const
Node * PreviousSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
void Clear()
Clear all Nodes below this.
Text(const T &value)
Streams value into a string and creates a Text with it.
Iterator operator--(int)
Sets internal pointer to the Previous Sibling, or Iterator::END, if there are no prior siblings...
Wrapper around TiXmlElement.
void IteratePrevious(const std::string &, Attribute **previous) const
virtual void operator=(const NodeImp< T > ©)
void IterateNext(const std::string &value, T **next) const
Comment * ToComment() const
Pointer conversion - replaces TiXmlNode::ToComment.
Document * ToDocument() const
Pointer conversion - replaces TiXmlNode::ToDocument.
virtual bool VisitExit(const Document &)
Visit a document.
Text(const std::string &value)
Constructor.
StylesheetReference(const std::string &type, const std::string &href)
Constructor.
virtual bool Visit(const TiXmlDeclaration &declaration)
Visit a declaration.
#define TICPPTHROW(message)
This allows you to stream your exceptions in.
void GetAttributeOrDefault(const std::string &name, T *value, const DefaulT &defaultValue) const
Gets an attribute of name from an element, if it doesn't exist it will return the defaultValue...
std::string ToString(const std::string &value) const
virtual bool Visit(const TiXmlText &text)
Visit a text node.
virtual bool Visit(const Text &)
Visit a text node.
StylesheetReference()
Default Constructor.
bool Accept(TiXmlVisitor *visitor) const
Accept a hierchical visit the nodes in the TinyXML DOM.
Iterator & operator=(T *p)
Sets internal pointer.
Declaration(const std::string &version, const std::string &encoding, const std::string &standalone)
Constructor.
void FromString(const std::string &temp, std::string *out) const
Specialization for std::string.
void SetName(const T &name)
Set the value of this attribute.
Node * NextSibling(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling node with the given value.
bool NoChildren() const
Check if this node has no children.
NodeImp(const NodeImp< T > ©)
virtual TiXmlBase * GetBasePointer() const =0
bool Error() const
If an error occurs, Error will be set to true.
Attribute * Previous(bool throwIfNoAttribute=true) const
Get the previous sibling attribute in the DOM.
int Column() const
See Row()
virtual bool Visit(const Declaration &)
Visit a declaration.
void ValidatePointer() const
std::string GetTextOrDefault(const std::string &defaultValue) const
Gets the text of an Element, if it doesn't exist it will return the defaultValue. ...
void SetTiXmlPointer(TiXmlAttribute *newPointer)
void SaveFile(const std::string &filename) const
Save a file using the given filename.
Declaration()
Default Constructor.
Document()
Default Constructor.
bool GetTextImp(std::string *value) const
Node * LastChild(bool throwIfNoChildren=true) const
The last child of this node.
Implementation of Node wrapper.
std::string GetAttribute(const std::string &name) const
Gets an attribute of name from an element.
std::string GetText(bool throwIfNotFound=true) const
Gets the text of an Element.
Attribute(const std::string &name, const std::string &value)
Construct an attribute with name and value.
void GetValue(T *value) const
Get the value of this node Uses Base::FromString to convert TiXmlNode::ValueStr from a std::string...
void SetValue(const T &value)
Set the value of this node.
virtual void IterateFirst(const std::string &, Attribute **) const
bool operator==(const Iterator &it) const
Compares internal pointer.
void LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
bool IsNull()
Returns state of internal pointer - will be null if the object was deleted.
Iterator operator++(int)
Sets internal pointer to the Next Sibling, or Iterator::END, if there are no more siblings...
Wrapper around TiXmlText.
StylesheetReference * ToStylesheetReference() const
Pointer conversion - replaces TiXmlNode::ToStylesheetReference.
Element(const std::string &value)
Default Constructor.
std::string Standalone() const
StandAlone.
Node * FirstChild(const std::string &value, bool throwIfNoChildren=true) const
The first child of this node with the matching value.
int Row() const
Return the position, in the original source file, of this node or attribute.
void LoadFile(const std::string &filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the given filename.
void SaveFile() const
Save a file using the current document value.
bool operator!=(const T *p) const
Compares internal pointer.
Document(const std::string &documentName)
Constructor.
Text(TiXmlText *text)
Constructor.
virtual TiXmlNode * GetTiXmlPointer() const =0
A stylesheet reference looks like this:
Element * FirstChildElement(const std::string &value, bool throwIfNoChildren=true) const
The first child element of this node with the matching value.
void FromString(const std::string &temp, T *out) const
Converts a std::string to any class with a proper overload of the >> opertor.
In correct XML the declaration is the first entry in the file.
void SetTiXmlPointer(T *newPointer)
Element * ToElement() const
Pointer conversion - replaces TiXmlNode::ToElement.
std::string m_value
Value for NextSibling calls.
std::string Value() const
Get the value of this node.
std::string Href() const
Href.
void Parse(const std::string &xml, bool throwIfParseError=true, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given xml data.
Element()
Default Constructor.
Any tag that tinyXml doesn't recognize is saved as an unknown.
void IterateNext(const std::string &, Attribute **next) const
T * Get() const
Gets internal pointer.
const TiXmlEncoding TIXML_DEFAULT_ENCODING
virtual bool VisitEnter(const Element &, const Attribute *)
Visit an element.
int Type() const
Query the type (as TiXmlNode::NodeType ) of this node.
virtual bool VisitExit(const Element &)
Visit an element.
Node * ReplaceChild(Node *replaceThis, Node &withThis)
Replace a child of this node.
std::string ToString(const T &value) const
Converts any class with a proper overload of the << opertor to a std::string.
bool operator!=(const Base &rhs) const
Compare internal TiXml pointers to determine is both are wrappers around the same node...
T * begin(const Node *parent) const
For for loop comparisons.
void GetAttribute(const std::string &name, T *value, bool throwIfNotFound=true) const
Gets an attribute of name from an element.
bool GetAttributeImp(const std::string &name, std::string *value) const
const char * what() const
Override std::exception::what() to return m_details.
void RemoveAttribute(const std::string &name)
Removes attribute from element.
void SetText(const T &value)
Convenience function to set the text of an element.
friend std::istream & operator>>(std::istream &in, Node &base)
Stream input operator.
void IncRef()
Increment Reference Count.
void SetValue(const T &value)
Set the value of this node.
Node * IterateChildren(Node *previous) const
An alternate way to walk the children of a node.
TiCppRCImp * m_tiRC
Pointer to reference counter.
This is a ticpp exception class.
Attribute * FirstAttribute(bool throwIfNoAttributes=true) const
Access the first attribute in this element.
Wrapper around TiXmlDeclaration.
TiXmlNode * GetTiXmlPointer() const
friend class TiXmlElement
Wrapper around TiXmlBase.
Always the top level node.
T * operator->() const
So Iterator behaves like a STL iterator.
void SetAttribute(const std::string &name, const T &value)
Sets an attribute of name to a given value.
TiXmlBase * GetBasePointer() const
Iterator(const Iterator &it)
Constructor.
void GetText(T *value, bool throwIfNotFound=true) const
Gets the text of an Element.
Node * IterateChildren(const std::string &value, Node *previous) const
This flavor of IterateChildren searches for children with a particular value.
virtual bool Visit(const Comment &)
Visit a comment node.
Iterator & operator--()
Sets internal pointer to the Previous Sibling, or Iterator::END, if there are no prior siblings...
std::string Type() const
Type.
Node * NextSibling(const char *value, bool throwIfNoSiblings=true) const
Node * LastChild(const std::string &value, bool throwIfNoChildren=true) const
The last child of this node with the matching value.
void IterateFirst(const std::string &value, T **first) const
int Row() const
Return the position, in the original source file, of this node or attribute.
std::string GetAttributeOrDefault(const std::string &name, const std::string &defaultValue) const
Gets an attribute of name from an element, if it doesn't exist it will return the defaultValue...
std::string Name() const
Get the value of this attribute.
StylesheetReference(TiXmlStylesheetReference *stylesheetReference)
Constructor.
Node * Parent(bool throwIfNoParent=true) const
The Parent of this Node.
void SetImpRC(TiXmlBase *nodeBase)
virtual bool VisitExit(const TiXmlDocument &doc)
Visit a document.
std::string Encoding() const
Encoding.
TiXmlBase is a base class for every class in TinyXml.
std::string m_details
Exception Details.
Element(TiXmlElement *element)
Constructor.
TiXmlAttribute * m_tiXmlPointer
Element * NextSiblingElement(bool throwIfNoSiblings=true) const
Navigate to a sibling element.
bool operator==(const Base &rhs) const
Compare internal TiXml pointers to determine is both are wrappers around the same node...
Wrapper around TiXmlVisitor.
friend std::ostream & operator<<(std::ostream &out, const Node &base)
Stream output operator.
Element * FirstChildElement(const char *value, bool throwIfNoChildren=true) const
Iterator & operator++()
Sets internal pointer to the Next Sibling, or Iterator::END, if there are no more siblings...
Element(const std::string &value, const T &text)
Constructor that allows you to set the element text.
The parent class for everything in the Document Object Model.
void GetName(T *name) const
Get the value of this attribute Uses Base::FromString to convert TiXmlAttribute::Name from a std::str...
Node * PreviousSibling(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling node with the given value.
Iterator(const std::string &value="")
Constructor.
T * To() const
Pointer conversion ( NOT OBJECT CONVERSION ) - replaces TiXmlNode::ToElement, TiXmlNode::ToDocument, TiXmlNode::ToComment, etc.
T * m_tiXmlPointer
Internal pointer to the TiXml Class which is being wrapped.
std::string Version() const
Version.
virtual bool Visit(const TiXmlComment &comment)
Visit a comment node.
T * end() const
For for loop comparisons.
Node * NextSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
void IterateFirst(const std::string &, Attribute **first) const
Node * InsertEndChild(Node &addThis)
Adds a child past the LastChild.
T & operator*() const
So Iterator behaves like a STL iterator.
void LoadFile(const char *filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the given filename.
virtual bool Visit(const TiXmlStylesheetReference &stylesheet)
Visit a stylesheet reference.
void GetTextOrDefault(T *value, const DefaultT &defaultValue) const
Gets the text value of an Element, if it doesn't exist it will return the defaultValue.
virtual void Print(FILE *file, int depth) const
All TinyXml classes can print themselves to a filestream.
Text * ToText() const
Pointer conversion - replaces TiXmlNode::ToText.
Node * InsertAfterChild(Node *afterThis, Node &addThis)
Adds a child after the specified child.
Document(const char *documentName)
Constructor.
Wrapper around TiXmlDocument.
std::string Value() const
Get the value of this attribute.
Attribute * Next(bool throwIfNoAttribute=true) const
Get the next sibling attribute in the DOM.
TiCppRCImp * m_impRC
Holds status of internal TiXmlPointer - use this to determine if object has been deleted already...
Node * LastChild(const char *value, bool throwIfNoChildren=true) const
virtual bool VisitExit(const TiXmlElement &element)
Visit an element.
Attribute()
Construct an empty attribute.
Node * LinkEndChild(Node *childNode)
Adds a child past the LastChild.
virtual ~Base()
Destructor.
T GetAttribute(const std::string &name, bool throwIfNotFound=true) const
Returns an attribute of name from an element.
virtual bool VisitEnter(const TiXmlDocument &doc)
Visit a document.
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
void DecRef()
Decrement Reference Count.
Node * FirstChild(bool throwIfNoChildren=true) const
The first child of this node.
void GetValue(T *value) const
Get the value of this attribute Uses Base::FromString to convert TiXmlAttribute::ValueStr from a std:...
int Column() const
Return the position, in the original source file, of this node or attribute.
Wrapper around TiXmlStylesheetReference.
Iterator for conveniently stepping through Nodes and Attributes.
Attribute(const Attribute ©)
Element(const char *value)
Default Constructor.
Node * InsertBeforeChild(Node *beforeThis, Node &addThis)
Adds a child before the specified child.
Attribute * LastAttribute(bool throwIfNoAttributes=true) const
Access the last attribute in this element.
std::string BuildDetailedErrorString() const
Builds detailed error string using TiXmlDocument::Error() and others.
TiXmlBase * GetBasePointer() const
Exception(const std::string &details)
Construct an exception with a message.
virtual bool Visit(const TiXmlUnknown &)
Visit an unknow node.
virtual bool VisitEnter(const Document &)
Visit a document.
Attribute(TiXmlAttribute *attribute)
Element * NextSiblingElement(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling element with the given value.
Declaration(TiXmlDeclaration *declaration)
Constructor.
bool operator!=(const Iterator &it) const
Compares internal pointer.
Node * NodeFactory(TiXmlNode *tiXmlNode, bool throwIfNull=true, bool rememberSpawnedWrapper=true) const
Element * NextSiblingElement(const char *value, bool throwIfNoSiblings=true) const
Declaration * ToDeclaration() const
Pointer conversion - replaces TiXmlNode::ToDeclaration.
Wrapper around TiXmlAttribute.
virtual bool VisitEnter(const TiXmlElement &element, const TiXmlAttribute *firstAttribute)
Visit an element.
Document * GetDocument(bool throwIfNoDocument=true) const
Return a pointer to the Document this node lives in.
Iterator(T *node, const std::string &value="")
Constructor.
The element is a container class.
bool HasAttribute(const std::string &name) const
Returns true, if attribute exists.
virtual bool Visit(const StylesheetReference &)
Visit a stylesheet reference.
bool operator==(T *p) const
Compares internal pointer*.