[libxml++] Attribute and subclasses: Move some methods



commit 30e996f1cddf150d51ec348a974674bd7eded9fd
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Mon Sep 28 17:00:44 2015 +0200

    Attribute and subclasses: Move some methods
    
    * libxml++/attribute.[h|cc]: Remove get_name(). Make get_value() virtual.
    Move set_value() and cobj() to AttributeNode.
    * libxml++/attributedeclaration.h: get_value() is overridden.
    * libxml++/attributenode.[h|cc]: Add get_value(), set_value(), cobj().
    Bug #754673.

 libxml++/attribute.cc           |   56 ---------------------------------------
 libxml++/attribute.h            |   36 +------------------------
 libxml++/attributedeclaration.h |    2 +-
 libxml++/attributenode.cc       |   36 +++++++++++++++++++++++++
 libxml++/attributenode.h        |   23 ++++++++++++++++
 5 files changed, 61 insertions(+), 92 deletions(-)
---
diff --git a/libxml++/attribute.cc b/libxml++/attribute.cc
index 949afd5..56f659a 100644
--- a/libxml++/attribute.cc
+++ b/libxml++/attribute.cc
@@ -21,61 +21,5 @@ Attribute::~Attribute()
 {
 }
 
-Glib::ustring Attribute::get_name() const
-{
-  // This will get the name also for an AttributeDeclaration. The name is in
-  // the same position in xmlNode, xmlAttr and xmlAttribute.
-  return cobj()->name ? (char*)cobj()->name : Glib::ustring();
-}
-
-//TODO when we can break ABI: Make get_value() virtual.
-Glib::ustring Attribute::get_value() const
-{
-  const AttributeDeclaration* const attributeDecl =
-    dynamic_cast<const AttributeDeclaration*>(this);
-  if (attributeDecl) // AttributeDeclaration
-    return attributeDecl->get_value();
-
-  // AttributeNode
-  xmlChar* value = nullptr;
-  if (cobj()->ns && cobj()->ns->href)
-    value = xmlGetNsProp(cobj()->parent, cobj()->name, cobj()->ns->href);
-  else
-    value = xmlGetNoNsProp(cobj()->parent, cobj()->name);
-
-  const Glib::ustring retn = value ? (const char*)value : "";
-  if (value)
-    xmlFree(value);
-  return retn;
-}
-
-//TODO when we can break ABI: Move set_value() to AttributeNode.
-void Attribute::set_value(const Glib::ustring& value)
-{
-  if (dynamic_cast<const AttributeDeclaration*>(this))
-    return; // Won't change the value of an AttributeDeclaration
-
-  if (cobj()->ns)
-    xmlSetNsProp(cobj()->parent, cobj()->ns, cobj()->name, (const xmlChar*)value.c_str());
-  else
-    xmlSetProp(cobj()->parent, cobj()->name, (const xmlChar*)value.c_str());
-}
-
-xmlAttr* Attribute::cobj()
-{
-  // yes, this does what it looks like: it takes an xmlNode pointer
-  // and *reinterprets* it as an xmlAttr pointer
-  // -stefan
-  return reinterpret_cast<xmlAttr*>(Node::cobj());
-}
-
-const xmlAttr* Attribute::cobj() const
-{
-  // yes, this does what it looks like: it takes an xmlNode pointer
-  // and *reinterprets* it as an xmlAttr pointer
-  // -stefan
-  return reinterpret_cast<const xmlAttr*>(Node::cobj());
-}
-
 } //namespace xmlpp
 
diff --git a/libxml++/attribute.h b/libxml++/attribute.h
index 3794582..57193b9 100644
--- a/libxml++/attribute.h
+++ b/libxml++/attribute.h
@@ -30,44 +30,10 @@ public:
   explicit Attribute(_xmlNode* node);
   ~Attribute() override;
   
-  //TODO: Can we remove this and just use Node::get_name()?
-  // Yes, when we can break ABI. /Kjell Ahlstedt 2012-02-09
-
-  /** Get the name of this attribute.
-   * See also Node::get_namespace_prefix() and Node::get_namespace_uri()
-   * @returns The attribute's name.
-   */
-  Glib::ustring get_name() const;
-
   /** Get the value of this attribute.
-   * Can be used for both an AttributeDeclaration and an AttributeNode.
    * @returns The attribute's value.
    */
-  Glib::ustring get_value() const;
-
-  /** Set the value of this attribute.
-   *
-   * If this is an AttributeDeclaration, the value will not be changed.
-   * This method is here for backward compatibility. It may be moved to
-   * AttributeNode in the future.
-   */
-  void set_value(const Glib::ustring& value);
-
-  /** Access the underlying libxml implementation.
-   *
-   * If this is an AttributeDeclaration, use AttributeDeclaration::cobj() instead.
-   * This method is here for backward compatibility. It may be moved to
-   * AttributeNode in the future.
-   */
-  _xmlAttr* cobj();
-
-  /** Access the underlying libxml implementation.
-   *
-   * If this is an AttributeDeclaration, use AttributeDeclaration::cobj() instead.
-   * This method is here for backward compatibility. It may be moved to
-   * AttributeNode in the future.
-   */
-  const _xmlAttr* cobj() const;
+  virtual Glib::ustring get_value() const = 0;
 };
 
 } // namespace xmlpp
diff --git a/libxml++/attributedeclaration.h b/libxml++/attributedeclaration.h
index 3178cf3..446436f 100644
--- a/libxml++/attributedeclaration.h
+++ b/libxml++/attributedeclaration.h
@@ -34,7 +34,7 @@ public:
   /** Get the default value of this attribute.
    * @returns The attribute's default value.
    */
-  Glib::ustring get_value() const;
+  Glib::ustring get_value() const override;
 
   ///Access the underlying libxml implementation.
   _xmlAttribute* cobj();
diff --git a/libxml++/attributenode.cc b/libxml++/attributenode.cc
index a874415..267af7a 100644
--- a/libxml++/attributenode.cc
+++ b/libxml++/attributenode.cc
@@ -20,4 +20,40 @@ AttributeNode::~AttributeNode()
 {
 }
 
+Glib::ustring AttributeNode::get_value() const
+{
+  xmlChar* value = nullptr;
+  if (cobj()->ns && cobj()->ns->href)
+    value = xmlGetNsProp(cobj()->parent, cobj()->name, cobj()->ns->href);
+  else
+    value = xmlGetNoNsProp(cobj()->parent, cobj()->name);
+
+  const Glib::ustring retn = value ? (const char*)value : "";
+  if (value)
+    xmlFree(value);
+  return retn;
+}
+
+void AttributeNode::set_value(const Glib::ustring& value)
+{
+  if (cobj()->ns)
+    xmlSetNsProp(cobj()->parent, cobj()->ns, cobj()->name, (const xmlChar*)value.c_str());
+  else
+    xmlSetProp(cobj()->parent, cobj()->name, (const xmlChar*)value.c_str());
+}
+
+xmlAttr* AttributeNode::cobj()
+{
+  // An XML_ATTRIBUTE_NODE is represented by an xmlAttr struct. Reinterpret
+  // the xmlNode pointer stored in the base class as an xmlAttr pointer.
+  return reinterpret_cast<xmlAttr*>(Node::cobj());
+}
+
+const xmlAttr* AttributeNode::cobj() const
+{
+  // An XML_ATTRIBUTE_NODE is represented by an xmlAttr struct. Reinterpret
+  // the xmlNode pointer stored in the base class as an xmlAttr pointer.
+  return reinterpret_cast<const xmlAttr*>(Node::cobj());
+}
+
 } //namespace xmlpp
diff --git a/libxml++/attributenode.h b/libxml++/attributenode.h
index a60874f..435ba49 100644
--- a/libxml++/attributenode.h
+++ b/libxml++/attributenode.h
@@ -25,6 +25,29 @@ class AttributeNode : public Attribute
 public:
   explicit AttributeNode(_xmlNode* node);
   ~AttributeNode() override;
+
+  /** Get the value of this attribute.
+   * @returns The attribute's value.
+   */
+  Glib::ustring get_value() const override;
+
+  /** Set the value of this attribute.
+   *
+   * @newin{3,0} Replaces Attribute::set_value()
+   */
+  void set_value(const Glib::ustring& value);
+
+  /** Access the underlying libxml implementation.
+   *
+   * @newin{3,0} Replaces Attribute::cobj()
+   */
+  _xmlAttr* cobj();
+
+  /** Access the underlying libxml implementation.
+   *
+   * @newin{3,0} Replaces Attribute::cobj() const
+   */
+  const _xmlAttr* cobj() const;
 };
 
 } // namespace xmlpp


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