Ardour  8.12
memento_command.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
4  * Copyright (C) 2007-2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2016-2017 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __lib_pbd_memento_command_h__
24 #define __lib_pbd_memento_command_h__
25 
26 #include <iostream>
27 
28 #include "pbd/libpbd_visibility.h"
29 #include "pbd/command.h"
30 #include "pbd/xml++.h"
31 #include "pbd/demangle.h"
32 
33 #include <sigc++/slot.h>
34 #include <typeinfo>
35 
54 template <class obj_T>
56 {
57 public:
58  virtual void set_state (XMLNode const &, int version) const = 0;
59  virtual XMLNode& get_state () const = 0;
60 
62  virtual std::string type_name () const = 0;
63 
65  virtual void add_state (XMLNode *) = 0;
66 };
67 
69 template <class obj_T>
71 {
72 public:
74  : _object (o)
75  {
76  _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this));
77  }
78 
79  void set_state (XMLNode const & node , int version) const { _object.set_state (node, version); }
80  XMLNode& get_state () const { return _object.get_state(); }
81  std::string type_name() const {
82  return PBD::demangled_name (_object);
83  }
84 
85  void add_state (XMLNode* node) {
86  node->set_property ("obj-id", _object.id().to_s());
87  }
88 
89  void object_died () {
90  /* The object we are binding died, so drop references to ourselves */
91  this->drop_references ();
92  }
93 
94 private:
95  obj_T& _object;
97 };
98 
103 template <class obj_T>
105 {
106 public:
107  MementoCommand (obj_T& a_object, XMLNode* a_before, XMLNode* a_after)
108  : _binder (new SimpleMementoCommandBinder<obj_T> (a_object)), before (a_before), after (a_after)
109  {
110  /* The binder's object died, so we must die */
111  _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
112  }
113 
115  : _binder (b), before (a_before), after (a_after)
116  {
117  /* The binder's object died, so we must die */
118  _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
119  }
120 
122  delete before;
123  delete after;
124  delete _binder;
125  }
126 
127  void binder_dying () {
128  /* delegate to UndoTransaction::command_death */
129  drop_references ();
130  }
131 
132  void operator() () {
133  if (after) {
134  _binder->set_state(*after, Stateful::current_state_version);
135  }
136  }
137 
138  void undo() {
139  if (before) {
140  _binder->set_state(*before, Stateful::current_state_version);
141  }
142  }
143 
144  virtual XMLNode &get_state() const {
145  std::string name;
146  if (before && after) {
147  name = "MementoCommand";
148  } else if (before) {
149  name = "MementoUndoCommand";
150  } else {
151  name = "MementoRedoCommand";
152  }
153 
154  XMLNode* node = new XMLNode(name);
155  _binder->add_state (node);
156 
157  node->set_property ("type-name", _binder->type_name ());
158 
159  if (before) {
160  node->add_child_copy(*before);
161  }
162 
163  if (after) {
164  node->add_child_copy(*after);
165  }
166 
167  return *node;
168  }
169 
170 protected:
175 };
176 
177 #endif // __lib_pbd_memento_h__
virtual void add_state(XMLNode *)=0
virtual void set_state(XMLNode const &, int version) const =0
virtual std::string type_name() const =0
virtual XMLNode & get_state() const =0
MementoCommand(MementoCommandBinder< obj_T > *b, XMLNode *a_before, XMLNode *a_after)
virtual XMLNode & get_state() const
PBD::ScopedConnection _binder_death_connection
MementoCommandBinder< obj_T > * _binder
MementoCommand(obj_T &a_object, XMLNode *a_before, XMLNode *a_after)
virtual void drop_references()
Definition: destructible.h:34
std::string type_name() const
void add_state(XMLNode *node)
void set_state(XMLNode const &node, int version) const
XMLNode & get_state() const
PBD::ScopedConnection _object_death_connection
Definition: xml++.h:114
bool set_property(const char *name, const std::string &value)
XMLNode * add_child_copy(const XMLNode &)
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_TEMPLATE_API
std::string demangled_name(T const &obj)
Definition: demangle.h:46