Add a template based get/set_property API to PBD::XMLNode
authorTim Mayberry <mojofunk@gmail.com>
Fri, 2 Sep 2016 13:08:32 +0000 (23:08 +1000)
committerTim Mayberry <mojofunk@gmail.com>
Tue, 18 Apr 2017 23:36:48 +0000 (09:36 +1000)
libs/pbd/pbd/xml++.h
libs/pbd/xml++.cc

index 9064bc185439b4544fd50e706dcedd52e2b82a38..2a18d9deb7fc8f13bc4f29e2486675902904cbf2 100644 (file)
 #include <libxml/tree.h>
 #include <boost/shared_ptr.hpp>
 
+#include <glibmm/ustring.h>
+
+#include "pbd/string_convert.h"
 #include "pbd/libpbd_visibility.h"
 
 class XMLTree;
 class XMLNode;
-class XMLProperty;
+
+class LIBPBD_API XMLProperty {
+public:
+       XMLProperty(const std::string& n, const std::string& v = std::string());
+       ~XMLProperty();
+
+       const std::string& name() const { return _name; }
+       const std::string& value() const { return _value; }
+       const std::string& set_value(const std::string& v) { return _value = v; }
+
+private:
+       std::string _name;
+       std::string _value;
+};
 
 typedef std::vector<XMLNode *>                   XMLNodeList;
 typedef std::vector<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
@@ -129,6 +145,40 @@ public:
        XMLProperty* add_property(const char* name, const char* value = "");
        XMLProperty* add_property(const char* name, const long value);
 
+       bool set_property (const char* name, const std::string& value);
+
+       bool set_property (const char* name, const char* cstr) {
+               return set_property (name, std::string(cstr));
+       }
+
+       bool set_property (const char* name, const Glib::ustring& ustr)
+       {
+               return set_property (name, ustr.raw ());
+       }
+
+       template<class T>
+       bool set_property (const char* name, const T& value)
+       {
+               std::string str;
+               if (!PBD::to_string<T> (value, str)) {
+                       return false;
+               }
+               return set_property(name, str);
+       }
+
+       bool get_property (const char* name, std::string& value) const;
+
+       template <class T>
+       bool get_property (const char* name, T& value) const
+       {
+               XMLProperty const* const prop = property (name);
+               if (!prop) {
+                       return false;
+               }
+
+               return PBD::string_to<T> (prop->value (), value);
+       }
+
        void remove_property(const std::string&);
        void remove_property_recursively(const std::string&);
 
@@ -152,20 +202,6 @@ private:
        void clear_lists ();
 };
 
-class LIBPBD_API XMLProperty {
-public:
-       XMLProperty(const std::string& n, const std::string& v = std::string());
-       ~XMLProperty();
-
-       const std::string& name() const { return _name; }
-       const std::string& value() const { return _value; }
-       const std::string& set_value(const std::string& v) { return _value = v; }
-
-private:
-       std::string _name;
-       std::string _value;
-};
-
 class LIBPBD_API XMLException: public std::exception {
 public:
        explicit XMLException(const std::string msg) : _message(msg) {}
index dbe6d51e0d302efbbc27737e79e3db5b9b5133a0..95547b15ee2eb4cddde042a660cc2b495d6ff464 100644 (file)
@@ -590,6 +590,23 @@ XMLNode::add_property(const char* name, const long value)
        return add_property(name, str);
 }
 
+bool
+XMLNode::set_property(const char* name, const string& str) {
+       return add_property (name, str);
+}
+
+bool
+XMLNode::get_property(const char* name, std::string& value) const
+{
+       XMLProperty const* const prop = property (name);
+       if (!prop)
+               return false;
+
+       value = prop->value ();
+
+       return true;
+}
+
 void
 XMLNode::remove_property(const string& name)
 {