X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fpbd%2Fpbd%2Fmemento_command.h;h=d22561ab217985fe8c12d48755518f504a9a139b;hb=8d0ec2403f3444e3a53d16b21f2f8557abd71b55;hp=b1347c169d827054719e653ace891019bf7d8e88;hpb=3b89d9eaa03406a5e03648f47734211f09b89d62;p=ardour.git diff --git a/libs/pbd/pbd/memento_command.h b/libs/pbd/pbd/memento_command.h index b1347c169d..d22561ab21 100644 --- a/libs/pbd/pbd/memento_command.h +++ b/libs/pbd/pbd/memento_command.h @@ -23,44 +23,120 @@ #include +#include "pbd/libpbd_visibility.h" #include "pbd/command.h" #include "pbd/stacktrace.h" #include "pbd/xml++.h" -#include "pbd/shiva.h" +#include "pbd/demangle.h" #include #include +/** A class that can return a Stateful object which is the subject of a MementoCommand. + * + * The existence of this class means that the undo record can refer to objects which + * don't exist in the session file. Currently this is used for + * + * 1. MIDI automation; when MIDI automation is edited, undo records are + * written for the AutomationList being changed. However this AutomationList + * is a temporary structure, built by a MidiModel, which doesn't get written + * to the session file. Hence we need to be able to go from a MidiSource and + * Parameter to an AutomationList. This Binder mechanism allows this through + * MidiAutomationListBinder; the undo record stores the source and parameter, + * and these are bound to an AutomationList by the Binder. + * + * 2. Crossfades; unlike regions, these are completely removed from a session + * when they are deleted. This means that the undo record can contain + * references to non-existant crossfades. To get around this, CrossfadeBinder + * can do `just-in-time' binding from the crossfade ID. + */ +template +class LIBPBD_TEMPLATE_API MementoCommandBinder : public PBD::Destructible +{ +public: + /** @return Stateful object to operate on */ + virtual obj_T* get () const = 0; + + /** @return Name of our type */ + virtual std::string type_name () const { + return PBD::demangled_name (*get ()); + } + + /** Add our own state to an XMLNode */ + virtual void add_state (XMLNode *) = 0; +}; + +/** A simple MementoCommandBinder which binds directly to an object */ +template +class LIBPBD_TEMPLATE_API SimpleMementoCommandBinder : public MementoCommandBinder +{ +public: + SimpleMementoCommandBinder (obj_T& o) + : _object (o) + { + _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this)); + } + + obj_T* get () const { + return &_object; + } + + void add_state (XMLNode* node) { + node->add_property ("obj_id", _object.id().to_s()); + } + + void object_died () { + /* The object we are binding died, so drop references to ourselves */ + this->drop_references (); + } + +private: + obj_T& _object; + PBD::ScopedConnection _object_death_connection; +}; + /** This command class is initialized with before and after mementos * (from Stateful::get_state()), so undo becomes restoring the before * memento, and redo is restoring the after memento. */ template -class MementoCommand : public Command +class LIBPBD_TEMPLATE_API MementoCommand : public Command { public: - MementoCommand(obj_T& a_object, XMLNode* a_before, XMLNode* a_after) - : obj(a_object), before(a_before), after(a_after) + MementoCommand (obj_T& a_object, XMLNode* a_before, XMLNode* a_after) + : _binder (new SimpleMementoCommandBinder (a_object)), before (a_before), after (a_after) { - /* catch destruction of the object */ - new PBD::PairedShiva< obj_T,MementoCommand > (obj, *this); + /* The binder's object died, so we must die */ + _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this)); } + MementoCommand (MementoCommandBinder* b, XMLNode* a_before, XMLNode* a_after) + : _binder (b), before (a_before), after (a_after) + { + /* The binder's object died, so we must die */ + _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this)); + } + ~MementoCommand () { - GoingAway(); /* EMIT SIGNAL */ + drop_references (); delete before; delete after; + delete _binder; + } + + void binder_dying () { + delete this; } void operator() () { if (after) { - obj.set_state(*after); + _binder->get()->set_state(*after, Stateful::current_state_version); } } void undo() { if (before) { - obj.set_state(*before); + _binder->get()->set_state(*before, Stateful::current_state_version); } } @@ -75,9 +151,9 @@ public: } XMLNode* node = new XMLNode(name); - - node->add_property("obj_id", obj.id().to_s()); - node->add_property("type_name", typeid(obj).name()); + _binder->add_state (node); + + node->add_property ("type_name", _binder->type_name ()); if (before) { node->add_child_copy(*before); @@ -91,9 +167,10 @@ public: } protected: - obj_T& obj; + MementoCommandBinder* _binder; XMLNode* before; XMLNode* after; + PBD::ScopedConnection _binder_death_connection; }; #endif // __lib_pbd_memento_h__