diff options
author | David Robillard <d@drobilla.net> | 2006-07-28 01:08:57 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2006-07-28 01:08:57 +0000 |
commit | 8277d134b9733aee344782891c99f07114384d9e (patch) | |
tree | 4472cc8608cf59272b127e1c5c722e0530aaac58 /libs/pbd | |
parent | 60454cc8dc1ca5e1819b853b55916d52497d495c (diff) |
Merged with trunk R708
git-svn-id: svn://localhost/ardour2/branches/midi@712 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/pbd/basename.h | 2 | ||||
-rw-r--r-- | libs/pbd/pbd/rcu.h | 120 | ||||
-rw-r--r-- | libs/pbd/pbd/xml++.h | 16 | ||||
-rw-r--r-- | libs/pbd/pool.cc | 2 | ||||
-rw-r--r-- | libs/pbd/stateful.cc | 1 | ||||
-rw-r--r-- | libs/pbd/whitespace.cc | 30 | ||||
-rw-r--r-- | libs/pbd/xml++.cc | 416 |
7 files changed, 357 insertions, 230 deletions
diff --git a/libs/pbd/pbd/basename.h b/libs/pbd/pbd/basename.h index 35aebe166c..a7e36acff0 100644 --- a/libs/pbd/pbd/basename.h +++ b/libs/pbd/pbd/basename.h @@ -8,6 +8,6 @@ namespace PBD extern std::string basename_nosuffix (const std::string&); -}; +} // namespace PBD #endif // __stupid_basename_h__ diff --git a/libs/pbd/pbd/rcu.h b/libs/pbd/pbd/rcu.h new file mode 100644 index 0000000000..6d9586cb3c --- /dev/null +++ b/libs/pbd/pbd/rcu.h @@ -0,0 +1,120 @@ +#ifndef __pbd_rcu_h__ +#define __pbd_rcu_h__ + +#include "boost/shared_ptr.hpp" +#include "glibmm/thread.h" + +#include <list> + +template<class T> +class RCUManager +{ +public: + + RCUManager (T* new_rcu_value) + : m_rcu_value(new_rcu_value) + { + + } + + virtual ~RCUManager() { } + + boost::shared_ptr<T> reader () const { return m_rcu_value; } + + // should be private + virtual boost::shared_ptr<T> write_copy () = 0; + + // should be private + virtual void update (boost::shared_ptr<T> new_value) = 0; + +protected: + + boost::shared_ptr<T> m_rcu_value; + + +}; + + +template<class T> +class SerializedRCUManager : public RCUManager<T> +{ +public: + + SerializedRCUManager(T* new_rcu_value) + : RCUManager<T>(new_rcu_value) + { + + } + + virtual boost::shared_ptr<T> write_copy () + { + m_lock.lock(); + + // I hope this is doing what I think it is doing :) + boost::shared_ptr<T> new_copy(new T(*RCUManager<T>::m_rcu_value)); + + // XXX todo remove old copies with only 1 reference from the list. + + return new_copy; + } + + virtual void update (boost::shared_ptr<T> new_value) + { + // So a current reader doesn't hold the only reference to + // the existing value when we assign it a new value which + // should ensure that deletion of old values doesn't + // occur in a reader thread. + boost::shared_ptr<T> old_copy = RCUManager<T>::m_rcu_value; + + // we hold the lock at this point effectively blocking + // other writers. + RCUManager<T>::m_rcu_value = new_value; + + + // XXX add the old value to the list of old copies. + + m_lock.unlock(); + } + +private: + Glib::Mutex m_lock; + + std::list<boost::shared_ptr<T> > m_old_values; +}; + +template<class T> +class RCUWriter +{ +public: + + RCUWriter(RCUManager<T>& manager) + : m_manager(manager) + { + m_copy = m_manager.write_copy(); + } + + ~RCUWriter() + { + // we can check here that the refcount of m_copy is 1 + + if(m_copy.use_count() == 1) { + m_manager.update(m_copy); + } else { + + // critical error. + } + + } + + // or operator boost::shared_ptr<T> (); + boost::shared_ptr<T> get_copy() { return m_copy; } + +private: + + RCUManager<T>& m_manager; + + // preferably this holds a pointer to T + boost::shared_ptr<T> m_copy; +}; + +#endif /* __pbd_rcu_h__ */ diff --git a/libs/pbd/pbd/xml++.h b/libs/pbd/pbd/xml++.h index afb896e1d5..5dcb4f084a 100644 --- a/libs/pbd/pbd/xml++.h +++ b/libs/pbd/pbd/xml++.h @@ -37,7 +37,6 @@ private: string _filename; XMLNode *_root; int _compression; - bool _initialized; public: XMLTree(); @@ -45,9 +44,8 @@ public: XMLTree(const XMLTree *); ~XMLTree(); - bool initialized() const { return _initialized; }; XMLNode *root() const { return _root; }; - XMLNode *set_root(XMLNode *n) { _initialized = true; return _root = n; }; + XMLNode *set_root(XMLNode *n) { return _root = n; }; const string & filename() const { return _filename; }; const string & set_filename(const string &fn) { return _filename = fn; }; @@ -69,7 +67,6 @@ public: class XMLNode { private: - bool _initialized; string _name; bool _is_content; string _content; @@ -83,18 +80,17 @@ public: XMLNode(const XMLNode&); ~XMLNode(); - bool initialized() const { return _initialized; }; const string name() const { return _name; }; bool is_content() const { return _is_content; }; const string & content() const { return _content; }; - const string & set_content(const string &); + const string & set_content (const string &); XMLNode *add_content(const string & = string()); - const XMLNodeList & children(const string & = string()) const; - XMLNode *add_child(const char *); - XMLNode *add_child_copy(const XMLNode&); - void add_child_nocopy (XMLNode&); + const XMLNodeList & children (const string & = string()) const; + XMLNode *add_child (const char *); + XMLNode *add_child_copy (const XMLNode&); + void add_child_nocopy (XMLNode&); const XMLPropertyList & properties() const { return _proplist; }; XMLProperty *property(const char * ); diff --git a/libs/pbd/pool.cc b/libs/pbd/pool.cc index 089766482d..be8032b7b6 100644 --- a/libs/pbd/pool.cc +++ b/libs/pbd/pool.cc @@ -70,7 +70,7 @@ Pool::alloc () } else { return ptr; } -}; +} void Pool::release (void *ptr) diff --git a/libs/pbd/stateful.cc b/libs/pbd/stateful.cc index 16aa528f59..786410e817 100644 --- a/libs/pbd/stateful.cc +++ b/libs/pbd/stateful.cc @@ -134,4 +134,3 @@ Stateful::instant_xml (const string& str, const string& dir) return 0; } - diff --git a/libs/pbd/whitespace.cc b/libs/pbd/whitespace.cc index 7f74940457..e35a8a8c0e 100644 --- a/libs/pbd/whitespace.cc +++ b/libs/pbd/whitespace.cc @@ -7,9 +7,11 @@ strip_whitespace_edges (string& str) { string::size_type i; string::size_type len; - string::size_type s; + string::size_type s; len = str.length(); + + /* strip front */ for (i = 0; i < len; ++i) { if (isgraph (str[i])) { @@ -17,14 +19,26 @@ strip_whitespace_edges (string& str) } } - s = i; + /* strip back */ + + if (len > 1) { + + s = i; + i = len - 1; + + do { + if (isgraph (str[i]) || i == 0) { + break; + } - for (i = len - 1; i >= 0; --i) { - if (isgraph (str[i])) { - break; - } - } + --i; + + } while (true); + + str = str.substr (s, (i - s) + 1); - str = str.substr (s, (i - s) + 1); + } else { + str = str.substr (s); + } } diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc index e496d8b2fd..03fa116279 100644 --- a/libs/pbd/xml++.cc +++ b/libs/pbd/xml++.cc @@ -12,117 +12,112 @@ static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int); XMLTree::XMLTree() : _filename(), - _root(), - _compression(0), - _initialized(false) + _root(0), + _compression(0) { } XMLTree::XMLTree(const string &fn) : _filename(fn), _root(0), - _compression(0), - _initialized(false) + _compression(0) { read(); } XMLTree::XMLTree(const XMLTree * from) { - _filename = from->filename(); - _root = new XMLNode(*from->root()); - _compression = from->compression(); - _initialized = true; + _filename = from->filename(); + _root = new XMLNode(*from->root()); + _compression = from->compression(); } XMLTree::~XMLTree() { - if (_initialized && _root) + if (_root) { delete _root; + } } int XMLTree::set_compression(int c) { - if (c > 9) - c = 9; - - if (c < 0) - c = 0; - - _compression = c; - - return _compression; + if (c > 9) { + c = 9; + } else if (c < 0) { + c = 0; + } + + _compression = c; + + return _compression; } bool XMLTree::read(void) { - xmlDocPtr doc; - - if (_root) { + xmlDocPtr doc; + + if (_root) { delete _root; _root = 0; - } - - xmlKeepBlanksDefault(0); - - doc = xmlParseFile(_filename.c_str()); - if (!doc) { - _initialized = false; - return false; - } - - _root = readnode(xmlDocGetRootElement(doc)); - xmlFreeDoc(doc); - _initialized = true; - - return true; + } + + xmlKeepBlanksDefault(0); + + doc = xmlParseFile(_filename.c_str()); + if (!doc) { + return false; + } + + _root = readnode(xmlDocGetRootElement(doc)); + xmlFreeDoc(doc); + + return true; } bool XMLTree::read_buffer(const string & buffer) { - xmlDocPtr doc; - - _filename = ""; - - if (_root) { + xmlDocPtr doc; + + _filename = ""; + + if (_root) { delete _root; _root = 0; - } - - doc = xmlParseMemory((char *) buffer.c_str(), buffer.length()); - if (!doc) { - _initialized = false; + } + + doc = xmlParseMemory((char *) buffer.c_str(), buffer.length()); + if (!doc) { return false; - } - - _root = readnode(xmlDocGetRootElement(doc)); - xmlFreeDoc(doc); - _initialized = true; - - return true; + } + + _root = readnode(xmlDocGetRootElement(doc)); + xmlFreeDoc(doc); + + return true; } bool XMLTree::write(void) const { - xmlDocPtr doc; - XMLNodeList children; - int result; - - xmlKeepBlanksDefault(0); - doc = xmlNewDoc((xmlChar *) "1.0"); - xmlSetDocCompressMode(doc, _compression); - writenode(doc, _root, doc->children, 1); - result = xmlSaveFormatFile(_filename.c_str(), doc, 1); - xmlFreeDoc(doc); - - if (result == -1) + xmlDocPtr doc; + XMLNodeList children; + int result; + + xmlKeepBlanksDefault(0); + doc = xmlNewDoc((xmlChar *) "1.0"); + xmlSetDocCompressMode(doc, _compression); + writenode(doc, _root, doc->children, 1); + result = xmlSaveFormatFile(_filename.c_str(), doc, 1); + xmlFreeDoc(doc); + + if (result == -1) { return false; - - return true; + } + + return true; } void @@ -142,105 +137,104 @@ XMLTree::debug(FILE* out) const const string & XMLTree::write_buffer(void) const { - static string retval; - char *ptr; - int len; - xmlDocPtr doc; - XMLNodeList children; - - xmlKeepBlanksDefault(0); - doc = xmlNewDoc((xmlChar *) "1.0"); - xmlSetDocCompressMode(doc, _compression); - writenode(doc, _root, doc->children, 1); - xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len); - xmlFreeDoc(doc); - - retval = ptr; - - free(ptr); - - return retval; + static string retval; + char *ptr; + int len; + xmlDocPtr doc; + XMLNodeList children; + + xmlKeepBlanksDefault(0); + doc = xmlNewDoc((xmlChar *) "1.0"); + xmlSetDocCompressMode(doc, _compression); + writenode(doc, _root, doc->children, 1); + xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len); + xmlFreeDoc(doc); + + retval = ptr; + + free(ptr); + + return retval; } XMLNode::XMLNode(const string & n) : _name(n), _is_content(false), _content(string()) { - - if (_name.empty()) - _initialized = false; - else - _initialized = true; } XMLNode::XMLNode(const string & n, const string & c) - :_name(string()), _is_content(true), _content(c) + :_name(n), _is_content(true), _content(c) { - _initialized = true; } XMLNode::XMLNode(const XMLNode& from) - : _initialized(false) { - XMLPropertyList props; - XMLPropertyIterator curprop; - XMLNodeList nodes; - XMLNodeIterator curnode; - - _name = from.name(); - set_content(from.content()); - - props = from.properties(); - for (curprop = props.begin(); curprop != props.end(); curprop++) - add_property((*curprop)->name().c_str(), (*curprop)->value()); - - nodes = from.children(); - for (curnode = nodes.begin(); curnode != nodes.end(); curnode++) + XMLPropertyList props; + XMLPropertyIterator curprop; + XMLNodeList nodes; + XMLNodeIterator curnode; + + _name = from.name(); + set_content(from.content()); + + props = from.properties(); + for (curprop = props.begin(); curprop != props.end(); ++curprop) { + add_property((*curprop)->name().c_str(), (*curprop)->value()); + } + + nodes = from.children(); + for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) { add_child_copy(**curnode); + } } XMLNode::~XMLNode() { - XMLNodeIterator curchild; - XMLPropertyIterator curprop; - - for (curchild = _children.begin(); curchild != _children.end(); - curchild++) + XMLNodeIterator curchild; + XMLPropertyIterator curprop; + + for (curchild = _children.begin(); curchild != _children.end(); ++curchild) { delete *curchild; - - for (curprop = _proplist.begin(); curprop != _proplist.end(); - curprop++) + } + + for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) { delete *curprop; + } } const string & XMLNode::set_content(const string & c) { - if (c.empty()) + if (c.empty()) { _is_content = false; - else + } else { _is_content = true; - - _content = c; - - return _content; + } + + _content = c; + + return _content; } const XMLNodeList & XMLNode::children(const string & n) const { - static XMLNodeList retval; - XMLNodeConstIterator cur; - - if (n.length() == 0) + static XMLNodeList retval; + XMLNodeConstIterator cur; + + if (n.length() == 0) { return _children; - - retval.erase(retval.begin(), retval.end()); - - for (cur = _children.begin(); cur != _children.end(); cur++) - if ((*cur)->name() == n) - retval.insert(retval.end(), *cur); - - return retval; + } + + retval.erase(retval.begin(), retval.end()); + + for (cur = _children.begin(); cur != _children.end(); ++cur) { + if ((*cur)->name() == n) { + retval.insert(retval.end(), *cur); + } + } + + return retval; } XMLNode * @@ -273,8 +267,10 @@ XMLProperty * XMLNode::property(const char * n) { string ns(n); - if (_propmap.find(ns) == _propmap.end()) + if (_propmap.find(ns) == _propmap.end()) { return 0; + } + return _propmap[ns]; } @@ -288,8 +284,9 @@ XMLNode::add_property(const char * n, const string & v) XMLProperty *tmp = new XMLProperty(ns, v); - if (!tmp) + if (!tmp) { return 0; + } _propmap[tmp->name()] = tmp; _proplist.insert(_proplist.end(), tmp); @@ -307,43 +304,43 @@ XMLNode::add_property(const char * n, const char * v) void XMLNode::remove_property(const string & n) { - if (_propmap.find(n) != _propmap.end()) { + if (_propmap.find(n) != _propmap.end()) { _proplist.remove(_propmap[n]); _propmap.erase(n); - } + } } void XMLNode::remove_nodes(const string & n) { - XMLNodeIterator i = _children.begin(); - XMLNodeIterator tmp; - - while (i != _children.end()) { - tmp = i; - ++tmp; - if ((*i)->name() == n) { - _children.erase (i); - } - i = tmp; - } + XMLNodeIterator i = _children.begin(); + XMLNodeIterator tmp; + + while (i != _children.end()) { + tmp = i; + ++tmp; + if ((*i)->name() == n) { + _children.erase (i); + } + i = tmp; + } } void XMLNode::remove_nodes_and_delete(const string & n) { - XMLNodeIterator i = _children.begin(); - XMLNodeIterator tmp; - - while (i != _children.end()) { - tmp = i; - ++tmp; - if ((*i)->name() == n) { - delete *i; - _children.erase (i); - } - i = tmp; - } + XMLNodeIterator i = _children.begin(); + XMLNodeIterator tmp; + + while (i != _children.end()) { + tmp = i; + ++tmp; + if ((*i)->name() == n) { + delete *i; + _children.erase (i); + } + i = tmp; + } } XMLProperty::XMLProperty(const string &n, const string &v) @@ -359,64 +356,65 @@ XMLProperty::~XMLProperty() static XMLNode * readnode(xmlNodePtr node) { - string name, content; - xmlNodePtr child; - XMLNode *tmp; - xmlAttrPtr attr; - - if (node->name) + string name, content; + xmlNodePtr child; + XMLNode *tmp; + xmlAttrPtr attr; + + if (node->name) { name = (char *) node->name; - - tmp = new XMLNode(name); - - for (attr = node->properties; attr; attr = attr->next) { + } + + tmp = new XMLNode(name); + + for (attr = node->properties; attr; attr = attr->next) { content = ""; - if (attr->children) - content = (char *) attr->children->content; + if (attr->children) { + content = (char *) attr->children->content; + } tmp->add_property((char *) attr->name, content); - } - - if (node->content) + } + + if (node->content) { tmp->set_content((char *) node->content); - else + } else { tmp->set_content(string()); - - for (child = node->children; child; child = child->next) - tmp->add_child_nocopy (*readnode(child)); - - return tmp; + } + + for (child = node->children; child; child = child->next) { + tmp->add_child_nocopy (*readnode(child)); + } + + return tmp; } static void -writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = - 0) +writenode(xmlDocPtr doc, XMLNode * n, xmlNodePtr p, int root = 0) { - XMLPropertyList props; - XMLPropertyIterator curprop; - XMLNodeList children; - XMLNodeIterator curchild; - xmlNodePtr node; - - if (root) - node = doc->children = - xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0); - - else + XMLPropertyList props; + XMLPropertyIterator curprop; + XMLNodeList children; + XMLNodeIterator curchild; + xmlNodePtr node; + + if (root) { + node = doc->children = xmlNewDocNode(doc, 0, (xmlChar *) n->name().c_str(), 0); + } else { node = xmlNewChild(p, 0, (xmlChar *) n->name().c_str(), 0); - - if (n->is_content()) { + } + + if (n->is_content()) { node->type = XML_TEXT_NODE; - xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), - n->content().length()); - } - - props = n->properties(); - for (curprop = props.begin(); curprop != props.end(); curprop++) - xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(), - (xmlChar *) (*curprop)->value().c_str()); - - children = n->children(); - for (curchild = children.begin(); curchild != children.end(); - curchild++) + xmlNodeSetContentLen(node, (const xmlChar *) n->content().c_str(), n->content().length()); + } + + props = n->properties(); + for (curprop = props.begin(); curprop != props.end(); ++curprop) { + xmlSetProp(node, (xmlChar *) (*curprop)->name().c_str(), (xmlChar *) (*curprop)->value().c_str()); + } + + children = n->children(); + for (curchild = children.begin(); curchild != children.end(); ++curchild) { writenode(doc, *curchild, node); + } } |