[libxml++] Document::get_root_node(): Propagate const qualifier to root Element



commit 9c4a8ab554ac76323aae77d07f46a520c5165328
Author: Knut Aksel Røysland <knutroy ifi uio no>
Date:   Thu Sep 17 13:19:06 2015 +0200

    Document::get_root_node(): Propagate const qualifier to root Element
    
    * libxml++/document.[h|cc]: Let the const version of get_root_node() return
    a const Element*. Add a non-const version.
    * examples/sax_parser_build_dom/main.cc:
    * examples/sax_parser_build_dom/svgdocument.[h|cc]: Add some const.
    Bug #632522.

 examples/sax_parser_build_dom/main.cc        |    5 +----
 examples/sax_parser_build_dom/svgdocument.cc |    6 ++----
 examples/sax_parser_build_dom/svgdocument.h  |    4 +---
 libxml++/document.cc                         |    7 ++++++-
 libxml++/document.h                          |   10 +++++++---
 5 files changed, 17 insertions(+), 15 deletions(-)
---
diff --git a/examples/sax_parser_build_dom/main.cc b/examples/sax_parser_build_dom/main.cc
index 92e2c9d..132b51d 100644
--- a/examples/sax_parser_build_dom/main.cc
+++ b/examples/sax_parser_build_dom/main.cc
@@ -1,5 +1,3 @@
-// -*- C++ -*-
-
 /* main.cc
  *
  * By Dan Dennedy <dan dennedy org> 
@@ -65,7 +63,7 @@ main(int argc, char* argv[])
     auto nl = element->find("//path[ style != '']");
     if(!nl.empty())
     {
-      auto path = dynamic_cast<SVG::Path*>(nl[0]);
+      auto path = dynamic_cast<const SVG::Path*>(nl[0]);
       std::cout << "style of first path node with a style = \"" << path->get_style() << "\"" << std::endl;
     }
   }
@@ -77,4 +75,3 @@ main(int argc, char* argv[])
   
   return EXIT_SUCCESS;
 }
-
diff --git a/examples/sax_parser_build_dom/svgdocument.cc b/examples/sax_parser_build_dom/svgdocument.cc
index db4aceb..b919053 100644
--- a/examples/sax_parser_build_dom/svgdocument.cc
+++ b/examples/sax_parser_build_dom/svgdocument.cc
@@ -1,5 +1,3 @@
-// -*- C++ -*-
-
 /* svgdocument.cc
  *
  * By Dan Dennedy <dan dennedy org>
@@ -25,9 +23,9 @@
 
 namespace SVG {
 
-SVG::Element* Document::get_root() const
+const SVG::Element* Document::get_root() const
 {
-   return dynamic_cast<SVG::Element*>(get_root_node()); // RTTI
+   return dynamic_cast<const SVG::Element*>(get_root_node()); // RTTI
 }
 
 } //namespace SVG
diff --git a/examples/sax_parser_build_dom/svgdocument.h b/examples/sax_parser_build_dom/svgdocument.h
index 324d899..6261b6a 100644
--- a/examples/sax_parser_build_dom/svgdocument.h
+++ b/examples/sax_parser_build_dom/svgdocument.h
@@ -1,5 +1,3 @@
-// -*- C++ -*-
-
 /* svgdocument.h
  *
  * By Dan Dennedy <dan dennedy org> 
@@ -32,7 +30,7 @@ namespace SVG {
 class Document : public xmlpp::Document
 {
 public:
-  SVG::Element* get_root() const;
+  const SVG::Element* get_root() const;
   // TODO: add custom document methods
 };
 
diff --git a/libxml++/document.cc b/libxml++/document.cc
index e4f5c1c..c3ced5c 100644
--- a/libxml++/document.cc
+++ b/libxml++/document.cc
@@ -223,7 +223,7 @@ void Document::set_internal_subset(const Glib::ustring& name,
     dtd->_private = new Dtd(dtd);
 }
 
-Element* Document::get_root_node() const
+Element* Document::get_root_node()
 {
   auto root = xmlDocGetRootElement(impl_);
   if(root == nullptr)
@@ -235,6 +235,11 @@ Element* Document::get_root_node() const
   }
 }
 
+const Element* Document::get_root_node() const
+{
+  return const_cast<Document*>(this)->get_root_node();
+}
+
 Element* Document::create_root_node(const Glib::ustring& name,
                                     const Glib::ustring& ns_uri,
                                     const Glib::ustring& ns_prefix)
diff --git a/libxml++/document.h b/libxml++/document.h
index eaede6c..a70a566 100644
--- a/libxml++/document.h
+++ b/libxml++/document.h
@@ -95,13 +95,17 @@ public:
                            const Glib::ustring& external_id,
                            const Glib::ustring& system_id);
 
-  //TODO: There should be a const and non-const version.
-  //See the patch here: https://bugzilla.gnome.org/show_bug.cgi?id=632522
   /** Return the root node.
    * This function does @b not create a default root node if it doesn't exist.
    * @return A pointer to the root node if it exists, <tt>0</tt> otherwise.
    */
-  Element* get_root_node() const;
+  Element* get_root_node();
+
+  /** Return the root node.
+   * This function does @b not create a default root node if it doesn't exist.
+   * @return A pointer to the root node if it exists, <tt>0</tt> otherwise.
+   */
+  const Element* get_root_node() const;
 
   /** Create the root element node.
    * If the document already contains a root element node, it is replaced, and


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