Video-Frame (not sample)
[ardour.git] / libs / pbd / stateful_diff_command.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/stateful_diff_command.h"
21 #include "pbd/types_convert.h"
22 #include "pbd/property_list.h"
23 #include "pbd/demangle.h"
24 #include "pbd/i18n.h"
25
26 using namespace std;
27 using namespace PBD;
28
29 /** Create a new StatefulDiffCommand by examining the changes made to a Stateful
30  *  since the last time that clear_changes was called on it.
31  *  @param s Stateful object.
32  */
33
34 StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s)
35         : _object (s)
36         , _changes (0)
37 {
38         _changes = s->get_changes_as_properties (this);
39
40         /* if the stateful object that this command refers to goes away,
41            be sure to notify owners of this command.
42         */
43
44         s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
45 }
46
47 StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s, XMLNode const & n)
48         : _object (s)
49         , _changes (0)
50 {
51         const XMLNodeList& children (n.children());
52
53         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
54                 if ((*i)->name() == X_("Changes")) {
55                         _changes = s->property_factory (**i);
56                 }
57         }
58
59         assert (_changes != 0);
60
61         /* if the stateful object that this command refers to goes away,
62            be sure to notify owners of this command.
63         */
64
65         s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
66 }
67
68 StatefulDiffCommand::~StatefulDiffCommand ()
69 {
70         drop_references ();
71
72         delete _changes;
73 }
74
75 void
76 StatefulDiffCommand::operator() ()
77 {
78         boost::shared_ptr<Stateful> s (_object.lock());
79
80         if (s) {
81                 s->apply_changes (*_changes);
82         }
83 }
84
85 void
86 StatefulDiffCommand::undo ()
87 {
88         boost::shared_ptr<Stateful> s (_object.lock());
89
90         if (s) {
91                 PropertyList p = *_changes;
92                 p.invert ();
93                 s->apply_changes (p);
94         }
95 }
96
97 XMLNode&
98 StatefulDiffCommand::get_state ()
99 {
100         boost::shared_ptr<Stateful> s (_object.lock());
101
102         if (!s) {
103                 /* XXX should we throw? */
104                 return * new XMLNode("");
105         }
106
107         XMLNode* node = new XMLNode (X_("StatefulDiffCommand"));
108
109         node->set_property ("obj-id", s->id());
110         node->set_property ("type-name", demangled_name (*s.get()));
111
112         XMLNode* changes = new XMLNode (X_("Changes"));
113
114         _changes->get_changes_as_xml (changes);
115
116         node->add_child_nocopy (*changes);
117
118         return *node;
119 }
120
121 bool
122 StatefulDiffCommand::empty () const
123 {
124         return _changes->empty();
125 }