45 #define TIXML_USE_TICPP 46 #ifdef 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()); \ 106 class StylesheetReference;
130 virtual bool Visit(
const TiXmlText& text);
170 std::stringstream convert;
173 if (convert.fail()) {
174 TICPPTHROW(
"Could not convert value to text");
177 return convert.str();
180 std::string
ToString(
const std::string& value)
const {
return value; }
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 {
209 int Row()
const {
return GetBasePointer()->Row(); }
215 int Column()
const {
return GetBasePointer()->Column(); }
239 std::ostringstream full_message;
240 #ifndef TICPP_NO_RTTI 248 full_message <<
"\nDescription: " << doc->
ErrorDesc() <<
"\nFile: " 258 return full_message.str();
283 TICPPTHROW(
"Internal TiXml Pointer is nullptr");
291 virtual TiXmlBase* GetBasePointer()
const = 0;
302 return m_tiXmlPointer;
317 Attribute(
const std::string& name,
const std::string& value);
338 FromString(m_tiXmlPointer->
ValueStr(), value);
347 std::string Value()
const;
359 m_tiXmlPointer->
SetValue(ToString(value));
372 FromString(m_tiXmlPointer->
Name(), name);
381 std::string Name()
const;
393 m_tiXmlPointer->
SetName(ToString(name));
416 Attribute* Next(
bool throwIfNoAttribute =
true)
const;
421 Attribute* Previous(
bool throwIfNoAttribute =
true)
const;
430 void IterateNext(
const std::string&,
Attribute** next)
const;
439 void IteratePrevious(
const std::string&,
Attribute** previous)
const;
444 virtual void Print(FILE* file,
int depth)
const;
471 FromString(GetTiXmlPointer()->ValueStr(), value);
480 std::string Value()
const;
491 GetTiXmlPointer()->SetValue(ToString(value));
510 Node* Parent(
bool throwIfNoParent =
true)
const;
525 Node* FirstChild(
bool throwIfNoChildren =
true)
const;
539 Node* FirstChild(
const char* value,
bool throwIfNoChildren =
true)
const;
552 Node* FirstChild(
const std::string& value,
553 bool throwIfNoChildren =
true)
const;
568 Node* LastChild(
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;
604 Node* IterateChildren(
Node* previous)
const;
615 Node* IterateChildren(
const std::string& value,
Node* previous)
const;
629 Node* InsertEndChild(
Node& addThis);
641 Node* LinkEndChild(
Node* childNode);
654 Node* InsertBeforeChild(
Node* beforeThis,
Node& addThis);
689 void RemoveChild(
Node* removeThis);
703 Node* PreviousSibling(
bool throwIfNoSiblings =
true)
const;
716 Node* PreviousSibling(
const std::string& value,
717 bool throwIfNoSiblings =
true)
const;
731 Node* PreviousSibling(
const char* value,
bool throwIfNoSiblings =
true)
const;
745 Node* NextSibling(
bool throwIfNoSiblings =
true)
const;
758 Node* NextSibling(
const std::string& value,
759 bool throwIfNoSiblings =
true)
const;
773 Node* NextSibling(
const char* value,
bool throwIfNoSiblings =
true)
const;
786 for (
Node* child = FirstChild(value,
false); child;
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)) {
814 *next =
dynamic_cast< T*
>(sibling);
827 Node* sibling = PreviousSibling(value,
false);
828 *previous =
dynamic_cast< T*
>(sibling);
830 while ((0 != sibling) && (0 == *previous)) {
832 *previous =
dynamic_cast< T*
>(sibling);
848 Element* NextSiblingElement(
bool throwIfNoSiblings =
true)
const;
860 Element* NextSiblingElement(
const std::string& value,
861 bool throwIfNoSiblings =
true)
const;
875 Element* NextSiblingElement(
const char* value,
876 bool throwIfNoSiblings =
true)
const;
892 Element* FirstChildElement(
bool throwIfNoChildren =
true)
const;
906 Element* FirstChildElement(
const char* value,
907 bool throwIfNoChildren =
true)
const;
920 Element* FirstChildElement(
const std::string& value,
921 bool throwIfNoChildren =
true)
const;
940 Document* GetDocument(
bool throwIfNoDocument =
true)
const;
947 bool NoChildren()
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)
1007 Text* ToText()
const;
1042 std::unique_ptr< Node > Clone()
const;
1071 virtual TiXmlNode* GetTiXmlPointer()
const = 0;
1080 bool throwIfNull =
true,
1081 bool rememberSpawnedWrapper =
true)
const;
1111 template <
class T = Node >
1164 , m_value(it.m_value) {}
1170 T*
Get()
const {
return m_p; }
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 >
1273 return m_tiXmlPointer;
1284 m_tiXmlPointer = newPointer;
1285 SetImpRC(newPointer);
1294 if (0 == tiXmlPointer) {
1295 #ifdef TICPP_NO_RTTI 1298 TICPPTHROW(
"Can not create a " <<
typeid(T).name());
1302 SetTiXmlPointer(tiXmlPointer);
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);
1452 void SaveFile()
const;
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 >
1538 Attribute* FirstAttribute(
bool throwIfNoAttributes =
true)
const;
1549 Attribute* LastAttribute(
bool throwIfNoAttributes =
true)
const;
1561 for (
Attribute* child = FirstAttribute(
false); child;
1562 child = child->
Next(
false)) {
1563 *first =
dynamic_cast< Attribute*
>(child);
1580 template <
class T >
1583 m_tiXmlPointer->SetAttribute(name, ToString(value));
1600 std::string
GetText(
bool throwIfNotFound =
true)
const {
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);
1773 std::string GetAttributeOrDefault(
const std::string& name,
1774 const std::string& defaultValue)
const;
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);
1852 std::string GetAttribute(
const std::string& name)
const;
1860 bool HasAttribute(
const std::string& name)
const;
1867 void RemoveAttribute(
const std::string& name);
1875 bool GetAttributeImp(
const std::string& name, std::string* value)
const;
1882 bool GetTextImp(std::string* value)
const;
1902 const std::string& encoding,
1903 const std::string& standalone);
1908 std::string Version()
const;
1913 std::string Encoding()
const;
1918 std::string Standalone()
const;
1942 std::string Type()
const;
1947 std::string Href()
const;
1951 #endif // TICPP_INCLUDED 1953 #endif // TIXML_USE_TICPP
Wrapper around TiXmlNode.
Iterator & operator=(const Iterator &it)
Sets internal pointer.
void IteratePrevious(const std::string &value, T **previous) const
int ErrorCol() const
The column where the error occured. See ErrorRow()
An attribute is a name-value pair.
If you call the Accept() method, it requires being passed a TiXmlVisitor class to handle callbacks...
Node * PreviousSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
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.
virtual void operator=(const NodeImp< T > ©)
void IterateNext(const std::string &value, T **next) const
virtual bool VisitExit(const Document &)
Visit a document.
const char * Name() const
Return the name of this attribute.
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
#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 Text &)
Visit a text node.
Iterator & operator=(T *p)
Sets internal pointer.
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.
NodeImp(const NodeImp< T > ©)
virtual TiXmlBase * GetBasePointer() const =0
bool Error() const
If an error occurs, Error will be set to true.
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 SetName(const char *_name)
Set the name of this attribute.
Implementation of Node wrapper.
std::string GetText(bool throwIfNotFound=true) const
Gets the text of an Element.
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.
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.
const std::string & ValueStr() const
Return the value of this attribute.
int Row() const
Return the position, in the original source file, of this node or attribute.
bool operator!=(const T *p) const
Compares internal pointer.
virtual TiXmlNode * GetTiXmlPointer() const =0
A stylesheet reference looks like this:
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)
std::string m_value
Value for NextSibling calls.
Any tag that tinyXml doesn't recognize is saved as an unknown.
T * Get() const
Gets internal pointer.
const TiXmlEncoding TIXML_DEFAULT_ENCODING
virtual bool VisitEnter(const Element &, const Attribute *)
Visit an element.
virtual bool VisitExit(const Element &)
Visit an element.
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...
const char * Value() const
The meaning of 'value' changes for the specific type of TiXmlNode.
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.
const char * what() const
Override std::exception::what() to return m_details.
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 SetValue(const T &value)
Set the value of this node.
This is a ticpp exception class.
Wrapper around TiXmlDeclaration.
TiXmlNode * GetTiXmlPointer() const
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.
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...
int ErrorRow() const
Returns the location (if known) of the error.
void IterateFirst(const std::string &value, T **first) const
const TiXmlDocument * GetDocument() const
Return a pointer to the Document this node lives in.
void SetImpRC(TiXmlBase *nodeBase)
TiXmlBase is a base class for every class in TinyXml.
std::string m_details
Exception Details.
TiXmlAttribute * m_tiXmlPointer
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.
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...
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.
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
T & operator*() const
So Iterator behaves like a STL iterator.
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.
Wrapper around TiXmlDocument.
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...
virtual ~Base()
Destructor.
T GetAttribute(const std::string &name, bool throwIfNotFound=true) const
Returns an attribute of name from an element.
void SetValue(const char *_value)
Set the value.
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.
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 VisitEnter(const Document &)
Visit a document.
bool operator!=(const Iterator &it) const
Compares internal pointer.
Wrapper around TiXmlAttribute.
Iterator(T *node, const std::string &value="")
Constructor.
The element is a container class.
virtual bool Visit(const StylesheetReference &)
Visit a stylesheet reference.
bool operator==(T *p) const
Compares internal pointer*.