tools/navgenproj/XmlNode.h Source File

XmlNode.h
Go to the documentation of this file.
1 /*
2 * Copyright 2017 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
16 #include <string>
17 #include <vector>
18 #include "navgenproj/TiXml.h"
19 
20 namespace Kaim
21 {
22 
23 class XmlNode
24 {
25 public:
26  explicit XmlNode(TiXmlNode* node) : m_node(node) {}
27 
28  // usage: for (XmlNode child = node.FirstChild(); child; child = child.NextSibling())
29  XmlNode FirstChild() const { return XmlNode(TiXml::FirstChild(m_node)); }
30  XmlNode NextSibling() const { return XmlNode(TiXml::NextSibling(m_node)); }
31 
32  bool IsValid() const { return m_node != nullptr; }
33  operator bool() const { return m_node != nullptr; }
34 
35  bool IsElement() const { return TiXml::IsElement(m_node); }
36 
37  bool HasName(const char* name) const { return m_node ? TiXml::HasName(m_node, name) : false; }
38 
39  const char* GetName() const { return m_node ? TiXml::GetName(m_node) : nullptr; }
40 
41  bool HasValue() const { return TiXml::HasValue(m_node); }
42 
43  bool HasValue(const char* name) const { return TiXml::HasValue(m_node, name); }
44 
45  const char* GetValue() const { return TiXml::GetValue(m_node); }
46 
47  void CreateValue(XmlNode node, const char* value) { TiXml::CreateValue(node.m_node, value); }
48 
49  XmlNode CreateChild(const char* name) { return XmlNode(TiXml::CreateChild(m_node, name)); }
50 
51  XmlNode GetChild(const char* name) { return m_node ? XmlNode(TiXml::GetChild(m_node, name)) : XmlNode(nullptr); }
52 
53  template<typename T>
54  bool ReadNode(T& value) { return m_node ? Read(*this, value) : false; }
55 
56  template<typename T, typename U>
57  bool ReadNode(T& value, const U& enumNames) { return m_node ? Read(*this, value, enumNames) : false; }
58 
59  template<typename T>
60  void WriteNode(const T& value) { Write(*this, value); }
61 
62  template<typename T, typename U>
63  void WriteNode(const T& value, const U& enumNames) { Write(*this, value, enumNames); }
64 
65  template<typename T>
66  bool ReadChild(const char* name, T& value) { return GetChild(name).ReadNode(value); }
67 
68  template<typename T, typename U>
69  bool ReadChild(const char* name, T& value, const U& enumNames) { return GetChild(name).ReadNode(value, enumNames); }
70 
71  template<typename T>
72  void WriteChild(const char* name, const T& value) { CreateChild(name).WriteNode(value); }
73 
74  template<typename T, typename U>
75  void WriteChild(const char* name, const T& value, const U& enumNames) { CreateChild(name).WriteNode(value, enumNames); }
76 
77 public:
78  TiXmlNode* m_node;
79 };
80 
81 
82 #define KY_READ_CHILD_VALUE(child_, name_, value_) \
83 { \
84  if (child_.HasName(name_)) \
85  { \
86  if (Kaim::Read(child_, value_) == false) \
87  { \
88  KY_LOG_ERROR(("[NavGenProj] Read <%s> FAILED", name_)); \
89  return false; \
90  } \
91  continue; \
92  } \
93 }
94 
95 
96 #define KY_READ_CHILD_ENUM(child_, name_, value_, enumNames_) \
97 { \
98  if (child_.HasName(name_)) \
99  { \
100  if (Kaim::Read(child_, value_, enumNames_) == false) \
101  { \
102  KY_LOG_ERROR(("[NavGenProj] Read <%s> FAILED", name_)); \
103  return false; \
104  } \
105  continue; \
106  } \
107 }
108 
109 #define KY_READ_CHILD_FUNC(child_, name_, func_) \
110 { \
111  if (child_.HasName(name_)) \
112  { \
113  if ((func_) == false) \
114  { \
115  KY_LOG_ERROR(("[NavGenProj] Read <%s> FAILED", name_)); \
116  return false; \
117  } \
118  continue; \
119  } \
120 }
121 
122 
123 class XmlDoc
124 {
125 public:
126  XmlDoc(const char* path) { m_doc = TiXml::CreateDoc(path); }
127  XmlNode Root() { return XmlNode((TiXmlNode*)m_doc); }
128  bool Load() { return TiXml::LoadDoc(m_doc); }
129  bool Save() { return TiXml::SaveDoc(m_doc); }
130  ~XmlDoc() { TiXml::DestroyDoc(m_doc); }
131 public:
132  TiXmlDocument* m_doc;
133 };
134 
135 }
136 
137 
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17