summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-06-26 01:37:48 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-09-27 18:38:28 +0200
commit51f0c23ac190c83bebeace8a49cd2ecd1143fa8c (patch)
treee45ceab35b6cc419f80be6c86753b6ccd1cd22f0 /src/utils
parent632da15234851f3decdbb29cbeb15e1950e41a0d (diff)
downloadmana-51f0c23ac190c83bebeace8a49cd2ecd1143fa8c.tar.gz
mana-51f0c23ac190c83bebeace8a49cd2ecd1143fa8c.tar.bz2
mana-51f0c23ac190c83bebeace8a49cd2ecd1143fa8c.tar.xz
mana-51f0c23ac190c83bebeace8a49cd2ecd1143fa8c.zip
Replaced for_each_xml_child_node macro with helper class
The new XML::Children class enables using a C++11 range-based for loop to iterate over the children of an xmlNodePtr.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/xml.cpp2
-rw-r--r--src/utils/xml.h32
2 files changed, 30 insertions, 4 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index 252a9bd2..72de10c7 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -168,7 +168,7 @@ namespace XML
xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name)
{
- for_each_xml_child_node(child, parent)
+ for (auto child : XML::Children(parent))
if (xmlStrEqual(child->name, BAD_CAST name))
return child;
diff --git a/src/utils/xml.h b/src/utils/xml.h
index 256a964b..fe15273b 100644
--- a/src/utils/xml.h
+++ b/src/utils/xml.h
@@ -95,9 +95,35 @@ namespace XML
* Initializes the XML engine.
*/
void init();
-}
-#define for_each_xml_child_node(var, parent) \
- for (xmlNodePtr var = parent->xmlChildrenNode; var; var = var->next)
+ /**
+ * Helper class to iterate over the children of a node. Enables range-based
+ * for loops.
+ */
+ class Children
+ {
+ public:
+ class Iterator
+ {
+ public:
+ explicit Iterator(xmlNodePtr node) : mNode(node) {}
+
+ bool operator!=(const Iterator &other) const { return mNode != other.mNode; }
+ void operator++() { mNode = mNode->next; }
+ xmlNodePtr operator*() const { return mNode; }
+
+ private:
+ xmlNodePtr mNode;
+ };
+
+ explicit Children(xmlNodePtr node) : mNode(node) {}
+
+ Iterator begin() const { return Iterator(mNode->children); }
+ Iterator end() const { return Iterator(nullptr); }
+
+ private:
+ xmlNodePtr mNode;
+ };
+}
#endif // XML_H