[libxml++] Node::get_children(): Propagate const qualifier to children



commit 8c3b88c5cd013ee9cc83f28f99e38821df99e177
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Thu Sep 17 09:43:37 2015 +0200

    Node::get_children(): Propagate const qualifier to children
    
    * libxml++/nodes/node.[h|cc]: Add const_NodeList. The const version of
    get_children() returns const_NodeList instead of const NodeList.
    * examples/dom_parser/main.cc: Remove unused variable.  Bug #338907.

 examples/dom_parser/main.cc |    4 ---
 libxml++/nodes/node.cc      |   56 +++++++++++++++++++++++--------------------
 libxml++/nodes/node.h       |    3 +-
 3 files changed, 32 insertions(+), 31 deletions(-)
---
diff --git a/examples/dom_parser/main.cc b/examples/dom_parser/main.cc
index 8e9371e..c81e3d6 100644
--- a/examples/dom_parser/main.cc
+++ b/examples/dom_parser/main.cc
@@ -1,5 +1,3 @@
-// -*- C++ -*-
-
 /* main.cc
  *
  * Copyright (C) 2002 The libxml++ development team
@@ -105,7 +103,6 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
   if(!nodeContent)
   {
     //Recurse through child nodes:
-    auto list = node->get_children();
     for(const auto& child : node->get_children())
     {
       print_node(child, indentation + 2); //recursive
@@ -190,4 +187,3 @@ int main(int argc, char* argv[])
 
   return EXIT_SUCCESS;
 }
-
diff --git a/libxml++/nodes/node.cc b/libxml++/nodes/node.cc
index cd3263e..5042a39 100644
--- a/libxml++/nodes/node.cc
+++ b/libxml++/nodes/node.cc
@@ -26,6 +26,33 @@
 
 namespace // anonymous
 {
+xmlpp::Node* _convert_node(xmlNode* node)
+{
+  xmlpp::Node* res = nullptr;
+  if (node)
+  {
+    xmlpp::Node::create_wrapper(node);
+    res = static_cast<xmlpp::Node*>(node->_private);
+  }
+  return res;
+}
+
+// Common part of const and non-const get_children()
+template <typename Tlist>
+Tlist get_children_common(const Glib::ustring& name, xmlNode* child)
+{
+  Tlist children;
+
+  while (child)
+  {
+    if (name.empty() || name == (const char*)child->name)
+      children.push_back(_convert_node(child));
+
+    child = child->next;
+  }
+  return children;
+}
+
 // Common part of xmlpp::Node::eval_to_[boolean|number|string]
 xmlXPathObject* eval_common(const Glib::ustring& xpath,
   const xmlpp::Node::PrefixNsMap* namespaces,
@@ -182,17 +209,6 @@ Node* Node::get_previous_sibling()
   return static_cast<Node*>(cobj()->prev->_private);
 }
 
-static Node* _convert_node(xmlNode* node)
-{
-  Node* res = nullptr;
-  if(node)
-  {
-    Node::create_wrapper(node);
-    res = static_cast<Node*>(node->_private);
-  }
-  return res;
-}
-
 Node* Node::get_first_child(const Glib::ustring& name)
 {
   auto child = impl_->children;
@@ -216,24 +232,12 @@ const Node* Node::get_first_child(const Glib::ustring& name) const
 
 Node::NodeList Node::get_children(const Glib::ustring& name)
 {
-   auto child = impl_->children;
-   if(!child)
-     return NodeList();
-
-   NodeList children;
-   do
-   {
-      if(name.empty() || name == (const char*)child->name)
-        children.push_back(_convert_node(child));
-   }
-   while((child = child->next));
-   
-   return children;
+  return get_children_common<NodeList>(name, impl_->children);
 }
 
-const Node::NodeList Node::get_children(const Glib::ustring& name) const
+Node::const_NodeList Node::get_children(const Glib::ustring& name) const
 {
-  return const_cast<Node*>(this)->get_children(name);
+  return get_children_common<const_NodeList>(name, impl_->children);
 }
 
 Element* Node::add_child(const Glib::ustring& name,
diff --git a/libxml++/nodes/node.h b/libxml++/nodes/node.h
index 74d0f0e..825a103 100644
--- a/libxml++/nodes/node.h
+++ b/libxml++/nodes/node.h
@@ -54,6 +54,7 @@ class Node : public NonCopyable
 {
 public:
   typedef std::list<Node*> NodeList;
+  typedef std::list<const Node*> const_NodeList;
 
   /** @throws xmlpp::internal_error If @a node is <tt>0</tt>.
    */
@@ -150,7 +151,7 @@ public:
    * @param name The names of the child nodes to get. If you do not specify a name, then the list will 
contain all nodes, regardless of their names.
    * @returns The list of child nodes.
    */
-  const NodeList get_children(const Glib::ustring& name = Glib::ustring()) const;
+  const_NodeList get_children(const Glib::ustring& name = Glib::ustring()) const;
 
   /** Add a child element to this node.
    * This node must be an element node.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]