From ae27e33f293e709e7f15e4ede9cb0b7a155a27f3 Mon Sep 17 00:00:00 2001 From: Tim Mayberry Date: Fri, 2 Sep 2016 23:08:32 +1000 Subject: [PATCH] Add a template based get/set_property API to PBD::XMLNode --- libs/pbd/pbd/xml++.h | 66 ++++++++++++++++++++++++++++++++++---------- 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 #include +#include + +#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 XMLNodeList; typedef std::vector > 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 + bool set_property (const char* name, const T& value) + { + std::string str; + if (!PBD::to_string (value, str)) { + return false; + } + return set_property(name, str); + } + + bool get_property (const char* name, std::string& value) const; + + template + bool get_property (const char* name, T& value) const + { + XMLProperty const* const prop = property (name); + if (!prop) { + return false; + } + + return PBD::string_to (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) { -- 2.30.2