Xml.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef ANKI_XML_H
  2. #define ANKI_XML_H
  3. #include "anki/util/Exception.h"
  4. #include <tinyxml2.h>
  5. #if !ANKI_TINYXML2
  6. # error "Wrong tinyxml2 included"
  7. #endif
  8. #include <string>
  9. #include "anki/Math.h"
  10. namespace anki {
  11. /// XML element
  12. struct XmlElement
  13. {
  14. friend class XmlDocument;
  15. public:
  16. XmlElement()
  17. : el(nullptr)
  18. {}
  19. XmlElement(const XmlElement& b)
  20. : el(b.el)
  21. {}
  22. /// If element has something return true
  23. operator bool() const
  24. {
  25. return el != nullptr;
  26. }
  27. /// Copy
  28. XmlElement& operator=(const XmlElement& b)
  29. {
  30. el = b.el;
  31. return *this;
  32. }
  33. /// Return the text inside a tag
  34. const char* getText() const
  35. {
  36. check();
  37. return el->GetText();
  38. }
  39. /// Return the text inside as an int
  40. I getInt() const;
  41. /// Return the text inside as a float
  42. F64 getFloat() const;
  43. /// Return the text inside as a Mat4
  44. Mat4 getMat4() const;
  45. /// Return the text inside as a Vec3
  46. Vec3 getVec3() const;
  47. /// Return the text inside as a Vec4
  48. Vec4 getVec4() const;
  49. /// Get a child element quietly
  50. XmlElement getChildElementOptional(const char* name) const;
  51. /// Get a child element and throw exception if not found
  52. XmlElement getChildElement(const char* name) const;
  53. /// Get the next element with the same name. Returns empty XmlElement if
  54. /// it reached the end of the list
  55. XmlElement getNextSiblingElement(const char* name) const;
  56. private:
  57. tinyxml2::XMLElement* el;
  58. void check() const
  59. {
  60. if(el == nullptr)
  61. {
  62. throw ANKI_EXCEPTION("Empty element");
  63. }
  64. }
  65. };
  66. /// XML document
  67. class XmlDocument
  68. {
  69. public:
  70. void loadFile(const char* filename);
  71. XmlElement getChildElement(const char* name)
  72. {
  73. XmlElement el;
  74. el.el = doc.FirstChildElement(name);
  75. return el;
  76. }
  77. private:
  78. tinyxml2::XMLDocument doc;
  79. };
  80. } // end namespace anki
  81. #endif