[libxml++] Element::get_attributes(): Propagate const qualifier to attributes



commit 5a2ed1d8e21caa5dab9dcbd655ddfe0f047419f2
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Thu Sep 17 11:01:59 2015 +0200

    Element::get_attributes(): Propagate const qualifier to attributes
    
    * libxml++/nodes/element.[h|cc]: Add const_AttributeList. The const version
    of get_attributes() returns const_AttributeList instead of const AttributeList.
    * examples/dom_parser/main.cc:
    * examples/dom_xinclude/main.cc: Use "for(const auto&". Bug #338907.

 examples/dom_parser/main.cc   |    4 +---
 examples/dom_xinclude/main.cc |    4 +---
 libxml++/nodes/element.cc     |   13 +++++++++----
 libxml++/nodes/element.h      |    3 ++-
 4 files changed, 13 insertions(+), 11 deletions(-)
---
diff --git a/examples/dom_parser/main.cc b/examples/dom_parser/main.cc
index c81e3d6..26b46d8 100644
--- a/examples/dom_parser/main.cc
+++ b/examples/dom_parser/main.cc
@@ -75,10 +75,8 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
     std::cout << indent << "     line = " << node->get_line() << std::endl;
 
     //Print attributes:
-    const auto attributes = nodeElement->get_attributes();
-    for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); 
++iter)
+    for (const auto& attribute : nodeElement->get_attributes())
     {
-      const auto attribute = *iter;
       const auto namespace_prefix = attribute->get_namespace_prefix();
 
       std::cout << indent << "  Attribute ";
diff --git a/examples/dom_xinclude/main.cc b/examples/dom_xinclude/main.cc
index 0031287..71e538c 100644
--- a/examples/dom_xinclude/main.cc
+++ b/examples/dom_xinclude/main.cc
@@ -71,10 +71,8 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
     std::cout << indent << "     Element line = " << node->get_line() << std::endl;
 
     //Print attributes:
-    const auto attributes = nodeElement->get_attributes();
-    for (xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); 
++iter)
+    for (const auto& attribute : nodeElement->get_attributes())
     {
-      const auto attribute = *iter;
       const auto namespace_prefix = attribute->get_namespace_prefix();
 
       std::cout << indent << "  Attribute ";
diff --git a/libxml++/nodes/element.cc b/libxml++/nodes/element.cc
index a2a4f55..63e47d1 100644
--- a/libxml++/nodes/element.cc
+++ b/libxml++/nodes/element.cc
@@ -23,18 +23,23 @@ Element::~Element()
 Element::AttributeList Element::get_attributes()
 {
   AttributeList attributes;
-  for(auto attr = cobj()->properties; attr; attr = attr->next)
+  for (auto attr = cobj()->properties; attr; attr = attr->next)
   {
     Node::create_wrapper(reinterpret_cast<xmlNode*>(attr));
     attributes.push_back(reinterpret_cast<Attribute*>(attr->_private));
   }
-
   return attributes;
 }
 
-const Element::AttributeList Element::get_attributes() const
+Element::const_AttributeList Element::get_attributes() const
 {
-  return const_cast<Element*>(this)->get_attributes();
+  const_AttributeList attributes;
+  for (auto attr = cobj()->properties; attr; attr = attr->next)
+  {
+    Node::create_wrapper(reinterpret_cast<xmlNode*>(attr));
+    attributes.push_back(reinterpret_cast<const Attribute*>(attr->_private));
+  }
+  return attributes;
 }
 
 Attribute* Element::get_attribute(const Glib::ustring& name,
diff --git a/libxml++/nodes/element.h b/libxml++/nodes/element.h
index 1cad9bd..fe23aee 100644
--- a/libxml++/nodes/element.h
+++ b/libxml++/nodes/element.h
@@ -27,6 +27,7 @@ public:
   ~Element() override;
 
   typedef std::list<Attribute*> AttributeList;
+  typedef std::list<const Attribute*> const_AttributeList;
 
   /** Add a namespace declaration to this node which will apply to this node and all children.
    *
@@ -53,7 +54,7 @@ public:
   /** Obtain the list of explicitly set attributes for this element.
    * @returns The list of explicitly set attributes.
    */
-  const AttributeList get_attributes() const;
+  const_AttributeList get_attributes() const;
 
   //TODO: There should be a const and non-const version.
   //See the patch at https://bugzilla.gnome.org/show_bug.cgi?id=632524


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