summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
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