2 Copyright (C) 2006 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef __lib_pbd_memento_command_h__
22 #define __lib_pbd_memento_command_h__
26 #include "pbd/libpbd_visibility.h"
27 #include "pbd/command.h"
28 #include "pbd/stacktrace.h"
29 #include "pbd/xml++.h"
30 #include "pbd/demangle.h"
32 #include <sigc++/slot.h>
35 /** A class that can return a Stateful object which is the subject of a MementoCommand.
37 * The existence of this class means that the undo record can refer to objects which
38 * don't exist in the session file. Currently this is used for
40 * 1. MIDI automation; when MIDI automation is edited, undo records are
41 * written for the AutomationList being changed. However this AutomationList
42 * is a temporary structure, built by a MidiModel, which doesn't get written
43 * to the session file. Hence we need to be able to go from a MidiSource and
44 * Parameter to an AutomationList. This Binder mechanism allows this through
45 * MidiAutomationListBinder; the undo record stores the source and parameter,
46 * and these are bound to an AutomationList by the Binder.
48 * 2. Crossfades; unlike regions, these are completely removed from a session
49 * when they are deleted. This means that the undo record can contain
50 * references to non-existant crossfades. To get around this, CrossfadeBinder
51 * can do `just-in-time' binding from the crossfade ID.
53 template <class obj_T>
54 class LIBPBD_TEMPLATE_API MementoCommandBinder : public PBD::Destructible
57 /** @return Stateful object to operate on */
58 virtual obj_T* get () const = 0;
60 /** @return Name of our type */
61 virtual std::string type_name () const {
62 return PBD::demangled_name (*get ());
65 /** Add our own state to an XMLNode */
66 virtual void add_state (XMLNode *) = 0;
69 /** A simple MementoCommandBinder which binds directly to an object */
70 template <class obj_T>
71 class LIBPBD_TEMPLATE_API SimpleMementoCommandBinder : public MementoCommandBinder<obj_T>
74 SimpleMementoCommandBinder (obj_T& o)
77 _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this));
84 void add_state (XMLNode* node) {
85 node->set_property ("obj-id", _object.id().to_s());
89 /* The object we are binding died, so drop references to ourselves */
90 this->drop_references ();
95 PBD::ScopedConnection _object_death_connection;
98 /** This command class is initialized with before and after mementos
99 * (from Stateful::get_state()), so undo becomes restoring the before
100 * memento, and redo is restoring the after memento.
102 template <class obj_T>
103 class LIBPBD_TEMPLATE_API MementoCommand : public Command
106 MementoCommand (obj_T& a_object, XMLNode* a_before, XMLNode* a_after)
107 : _binder (new SimpleMementoCommandBinder<obj_T> (a_object)), before (a_before), after (a_after)
109 /* The binder's object died, so we must die */
110 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
113 MementoCommand (MementoCommandBinder<obj_T>* b, XMLNode* a_before, XMLNode* a_after)
114 : _binder (b), before (a_before), after (a_after)
116 /* The binder's object died, so we must die */
117 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
127 void binder_dying () {
133 _binder->get()->set_state(*after, Stateful::current_state_version);
139 _binder->get()->set_state(*before, Stateful::current_state_version);
143 virtual XMLNode &get_state() {
145 if (before && after) {
146 name = "MementoCommand";
148 name = "MementoUndoCommand";
150 name = "MementoRedoCommand";
153 XMLNode* node = new XMLNode(name);
154 _binder->add_state (node);
156 node->set_property ("type-name", _binder->type_name ());
159 node->add_child_copy(*before);
163 node->add_child_copy(*after);
170 MementoCommandBinder<obj_T>* _binder;
173 PBD::ScopedConnection _binder_death_connection;
176 #endif // __lib_pbd_memento_h__