Merge branch 'master' into windows
[ardour.git] / libs / pbd / pbd / sequence_property.h
index 7fdc9643975a25689e3a13dcd05db94035d6511c..b9d59724ddc56175fb6d9dab62ec1ea1ae352cb5 100644 (file)
@@ -31,6 +31,8 @@
 #include "pbd/id.h"
 #include "pbd/property_basics.h"
 #include "pbd/property_list.h"
+#include "pbd/stateful_diff_command.h"
+#include "pbd/error.h"
 
 namespace PBD {
 
@@ -80,8 +82,6 @@ class SequenceProperty : public PropertyBase
        SequenceProperty (PropertyID id, const boost::function<void(const ChangeRecord&)>& update)
                 : PropertyBase (id), _update_callback (update) {}
 
-       virtual typename Container::value_type lookup_id (const PBD::ID&) = 0;
-
         void invert () {
                _changes.removed.swap (_changes.added);
         }
@@ -97,18 +97,25 @@ class SequenceProperty : public PropertyBase
                        for (typename ChangeContainer::iterator i = _changes.added.begin(); i != _changes.added.end(); ++i) {
                                 XMLNode* add_node = new XMLNode ("Add");
                                 child->add_child_nocopy (*add_node);
-                                add_node->add_property ("id", (*i)->id().to_s());
+                               get_content_as_xml (*i, *add_node);
                        }
                }
                if (!_changes.removed.empty()) {
                        for (typename ChangeContainer::iterator i = _changes.removed.begin(); i != _changes.removed.end(); ++i) {
                                 XMLNode* remove_node = new XMLNode ("Remove");
                                 child->add_child_nocopy (*remove_node);
-                                remove_node->add_property ("id", (*i)->id().to_s());
+                               get_content_as_xml (*i, *remove_node);
                        }
                }
        }
 
+       /** Get a representation of one of our items as XML.  The representation must be sufficient to
+        *  restore the item's state later; an ID is ok if someone else is storing the item state,
+        *  otherwise it needs to be the full state.  The supplied node is an \<Add\> or \<Remove\>
+        *  which this method can either add properties or children to.
+        */
+       virtual void get_content_as_xml (typename ChangeContainer::value_type, XMLNode &) const = 0;
+
        bool set_value (XMLNode const &) {
                /* XXX: not used, but probably should be */
                assert (false);
@@ -147,52 +154,75 @@ class SequenceProperty : public PropertyBase
        }
 
        void get_changes_as_properties (PBD::PropertyList& changes, Command* cmd) const {
-               if (changed ()) {
-                       SequenceProperty<Container>* a = copy_for_history ();
-                       changes.add (a);
-
-                       if (cmd) {
-                               /* whenever one of the items emits DropReferences, make sure
-                                  that the Destructible we've been told to notify hears about
-                                  it. the Destructible is likely to be the Command being built
-                                  with this diff().
-                               */
+               if (!changed ()) {
+                       return;
+               }
+               
+               /* Create a property with just the changes and not the actual values */
+               SequenceProperty<Container>* a = create ();
+               a->_changes = _changes;
+               changes.add (a);
+               
+               if (cmd) {
+                       /* whenever one of the items emits DropReferences, make sure
+                          that the Destructible we've been told to notify hears about
+                          it. the Destructible is likely to be the Command being built
+                          with this diff().
+                       */
                         
-                               for (typename ChangeContainer::iterator i = a->changes().added.begin(); i != a->changes().added.end(); ++i) {
-                                       (*i)->DropReferences.connect_same_thread (*cmd, boost::bind (&Destructible::drop_references, cmd));
-                               }
-                        }
+                       for (typename ChangeContainer::iterator i = a->changes().added.begin(); i != a->changes().added.end(); ++i) {
+                               (*i)->DropReferences.connect_same_thread (*cmd, boost::bind (&Destructible::drop_references, cmd));
+                       }
                }
         }
 
        SequenceProperty<Container>* clone_from_xml (XMLNode const & node) const {
 
                XMLNodeList const children = node.children ();
+
+               /* find the node for this property name */
+               
+               std::string const c = capitalize (property_name ());
+               XMLNodeList::const_iterator i = children.begin();
+               while (i != children.end() && (*i)->name() != c) {
+                       ++i;
+               }
+
+               if (i == children.end()) {
+                       return 0;
+               }
+
+               /* create a property with the changes */
                
-               for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-
-                       if ((*i)->name() == capitalize (property_name())) {
-                               
-                               SequenceProperty<Container>* p = create ();
-                               
-                               if (p->set_changes (**i)) {
-                                       return p;
-                               } else {
-                                       delete p;
-                               }
+               SequenceProperty<Container>* p = create ();
+
+               XMLNodeList const & grandchildren = (*i)->children ();
+               for (XMLNodeList::const_iterator j = grandchildren.begin(); j != grandchildren.end(); ++j) {
+
+                       typename Container::value_type v = get_content_from_xml (**j);
+
+                       if (!v) {
+                               warning << "undo transaction references an unknown object" << endmsg;
+                       } else if ((*j)->name() == "Add") {
+                               p->_changes.added.insert (v);
+                       } else if ((*j)->name() == "Remove") {
+                               p->_changes.removed.insert (v);
                        }
-                }
+               }
 
-               return 0;
+               return p;
         }
 
+       /** Given an \<Add\> or \<Remove\> node as passed into get_content_to_xml, obtain an item */
+       virtual typename Container::value_type get_content_from_xml (XMLNode const & node) const = 0;
+
        void clear_owned_changes () {
                for (typename Container::iterator i = begin(); i != end(); ++i) {
                        (*i)->clear_changes ();
                }
        }
 
-       void rdiff (std::vector<StatefulDiffCommand*>& cmds) const {
+       void rdiff (std::vector<Command*>& cmds) const {
                for (typename Container::const_iterator i = begin(); i != end(); ++i) {
                        if ((*i)->changed ()) {
                                StatefulDiffCommand* sdc = new StatefulDiffCommand (*i);
@@ -201,7 +231,7 @@ class SequenceProperty : public PropertyBase
                }
        }
 
-        Container rlist() { return _val; }
+        Container rlist() const { return _val; }
 
        /* Wrap salient methods of Sequence
         */
@@ -235,6 +265,11 @@ class SequenceProperty : public PropertyBase
                return _val.erase (f, l);
        }
 
+       void remove (const typename Container::value_type& v) {
+               _changes.remove (v);
+               _val.remove (v);
+       }
+
        void push_back (const typename Container::value_type& v) {
                _changes.add (v);
                _val.push_back (v);
@@ -310,54 +345,22 @@ class SequenceProperty : public PropertyBase
         
         const ChangeRecord& changes () const { return _changes; }
 
-  protected:
-       Container _val;
-       ChangeRecord _changes;
-       boost::function<void(const ChangeRecord&)> _update_callback;
+protected:
 
-        /** Load serialized change history.
-         * @return true if loading succeeded, false otherwise
-         */
-
-       bool set_changes (XMLNode const & history_node) {
-
-                const XMLNodeList& children (history_node.children());
-
-                for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-                        const XMLProperty* prop = (*i)->property ("id");
-                        if (prop) {
-                                PBD::ID id (prop->value());
-                                typename Container::value_type v = lookup_id (id);
-                                if (!v) {
-                                        std::cerr << "No such item, ID = " << id.to_s() << " (from " << prop->value() << ")\n";
-                                        return false;
-                                }
-                                if ((*i)->name() == "Add") {
-                                        _changes.added.insert (v);
-                                } else if ((*i)->name() == "Remove") {
-                                        _changes.removed.insert (v);
-                                }
-                        }
-                }
-
-                return true;
-        }
+       /* copy construction only by subclasses */
+       SequenceProperty (SequenceProperty<Container> const & p)
+               : PropertyBase (p)
+               , _val (p._val)
+               , _changes (p._changes)
+               , _update_callback (p._update_callback)
+       {}
+       
+       Container _val; ///< our actual container of things
+       ChangeRecord _changes; ///< changes to the container (adds/removes) that have happened since clear_changes() was last called
+       boost::function<void(const ChangeRecord&)> _update_callback;
 
-private:
+private:       
        virtual SequenceProperty<Container>* create () const = 0;
-
-        /* create a copy of this ListSequenceProperty that only
-           has what is needed for use in a history list command. This
-           means that it won't contain the actual item list but
-           will have the added/removed list.
-        */
-       
-       SequenceProperty<Container>* copy_for_history () const {
-               SequenceProperty<Container>* copy = create ();
-               /* this is all we need */
-               copy->_changes = _changes;
-               return copy;
-       }
 };
 
 }