ardour
memento_command.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 Paul Davis
3  Author: Hans Fugal
4 
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.
9 
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.
14 
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.
18 
19 */
20 
21 #ifndef __lib_pbd_memento_command_h__
22 #define __lib_pbd_memento_command_h__
23 
24 #include <iostream>
25 
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"
31 
32 #include <sigc++/slot.h>
33 #include <typeinfo>
34 
53 template <class obj_T>
55 {
56 public:
58  virtual obj_T* get () const = 0;
59 
61  virtual std::string type_name () const {
62  return PBD::demangled_name (*get ());
63  }
64 
66  virtual void add_state (XMLNode *) = 0;
67 };
68 
70 template <class obj_T>
72 {
73 public:
75  : _object (o)
76  {
77  _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this));
78  }
79 
80  obj_T* get () const {
81  return &_object;
82  }
83 
84  void add_state (XMLNode* node) {
85  node->add_property ("obj_id", _object.id().to_s());
86  }
87 
88  void object_died () {
89  /* The object we are binding died, so drop references to ourselves */
90  this->drop_references ();
91  }
92 
93 private:
94  obj_T& _object;
96 };
97 
102 template <class obj_T>
104 {
105 public:
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)
108  {
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));
111  }
112 
114  : _binder (b), before (a_before), after (a_after)
115  {
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));
118  }
119 
121  drop_references ();
122  delete before;
123  delete after;
124  delete _binder;
125  }
126 
127  void binder_dying () {
128  delete this;
129  }
130 
131  void operator() () {
132  if (after) {
133  _binder->get()->set_state(*after, Stateful::current_state_version);
134  }
135  }
136 
137  void undo() {
138  if (before) {
139  _binder->get()->set_state(*before, Stateful::current_state_version);
140  }
141  }
142 
143  virtual XMLNode &get_state() {
144  std::string name;
145  if (before && after) {
146  name = "MementoCommand";
147  } else if (before) {
148  name = "MementoUndoCommand";
149  } else {
150  name = "MementoRedoCommand";
151  }
152 
153  XMLNode* node = new XMLNode(name);
154  _binder->add_state (node);
155 
156  node->add_property ("type_name", _binder->type_name ());
157 
158  if (before) {
159  node->add_child_copy(*before);
160  }
161 
162  if (after) {
163  node->add_child_copy(*after);
164  }
165 
166  return *node;
167  }
168 
169 protected:
174 };
175 
176 #endif // __lib_pbd_memento_h__
virtual void operator()()=0
MementoCommand(obj_T &a_object, XMLNode *a_before, XMLNode *a_after)
MementoCommand(MementoCommandBinder< obj_T > *b, XMLNode *a_before, XMLNode *a_after)
XMLNode * add_child_copy(const XMLNode &)
Definition: xml++.cc:363
MementoCommandBinder< obj_T > * _binder
std::string demangled_name(T const &obj)
Definition: demangle.h:36
void drop_references()
Definition: destructible.h:36
#define LIBPBD_TEMPLATE_API
XMLProperty * add_property(const char *name, const std::string &value)
virtual std::string type_name() const
Definition: xml++.h:95
PBD::ScopedConnection _object_death_connection
void add_state(XMLNode *node)
virtual XMLNode & get_state()
const std::string & name() const
Definition: command.h:39
PBD::ScopedConnection _binder_death_connection