diff options
author | Tim Mayberry <mojofunk@gmail.com> | 2016-09-02 23:08:32 +1000 |
---|---|---|
committer | Tim Mayberry <mojofunk@gmail.com> | 2017-04-19 09:36:48 +1000 |
commit | ae27e33f293e709e7f15e4ede9cb0b7a155a27f3 (patch) | |
tree | 19c6110f62018a51c9fc1b0348c42dd97afc25cc /libs/pbd | |
parent | 8f516a2c4feb060339bcc6deaee429ab7112cb10 (diff) |
Add a template based get/set_property API to PBD::XMLNode
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/pbd/xml++.h | 66 | ||||
-rw-r--r-- | libs/pbd/xml++.cc | 17 |
2 files changed, 68 insertions, 15 deletions
diff --git a/libs/pbd/pbd/xml++.h b/libs/pbd/pbd/xml++.h index 9064bc1854..2a18d9deb7 100644 --- a/libs/pbd/pbd/xml++.h +++ b/libs/pbd/pbd/xml++.h @@ -36,11 +36,27 @@ #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) {} diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc index dbe6d51e0d..95547b15ee 100644 --- a/libs/pbd/xml++.cc +++ b/libs/pbd/xml++.cc @@ -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) { |