2010-02-09 09:44:24 -05:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2010-02-09 09:44:24 -05:00
|
|
|
|
|
|
|
#include "pbd/stateful_diff_command.h"
|
2016-08-28 08:56:44 -04:00
|
|
|
#include "pbd/types_convert.h"
|
2010-03-01 19:00:00 -05:00
|
|
|
#include "pbd/property_list.h"
|
2010-03-02 14:12:01 -05:00
|
|
|
#include "pbd/demangle.h"
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2010-02-09 09:44:24 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
|
2010-08-25 13:32:08 -04:00
|
|
|
* since the last time that clear_changes was called on it.
|
2010-02-09 09:44:24 -05:00
|
|
|
* @param s Stateful object.
|
|
|
|
*/
|
|
|
|
|
2010-06-23 16:14:07 -04:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s)
|
|
|
|
: _object (s)
|
2010-08-25 13:31:57 -04:00
|
|
|
, _changes (0)
|
2010-02-09 09:44:24 -05:00
|
|
|
{
|
2010-08-25 13:31:57 -04:00
|
|
|
_changes = s->get_changes_as_properties (this);
|
2010-06-23 16:14:07 -04:00
|
|
|
|
|
|
|
/* if the stateful object that this command refers to goes away,
|
|
|
|
be sure to notify owners of this command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
2010-06-23 16:14:07 -04:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s, XMLNode const & n)
|
2010-02-09 17:28:46 -05:00
|
|
|
: _object (s)
|
2010-08-25 13:31:57 -04:00
|
|
|
, _changes (0)
|
2010-02-09 17:28:46 -05:00
|
|
|
{
|
2010-03-01 19:00:00 -05:00
|
|
|
const XMLNodeList& children (n.children());
|
|
|
|
|
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
2010-08-25 13:31:57 -04:00
|
|
|
if ((*i)->name() == X_("Changes")) {
|
|
|
|
_changes = s->property_factory (**i);
|
2010-03-01 19:00:00 -05:00
|
|
|
}
|
2010-08-25 13:31:57 -04:00
|
|
|
}
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-08-25 13:31:57 -04:00
|
|
|
assert (_changes != 0);
|
2010-06-23 16:14:07 -04:00
|
|
|
|
|
|
|
/* if the stateful object that this command refers to goes away,
|
|
|
|
be sure to notify owners of this command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
|
2010-02-09 17:28:46 -05:00
|
|
|
}
|
|
|
|
|
2010-02-09 09:44:24 -05:00
|
|
|
StatefulDiffCommand::~StatefulDiffCommand ()
|
|
|
|
{
|
2010-06-23 16:14:07 -04:00
|
|
|
drop_references ();
|
|
|
|
|
2010-08-25 13:31:57 -04:00
|
|
|
delete _changes;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::operator() ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-08-25 13:31:57 -04:00
|
|
|
s->apply_changes (*_changes);
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::undo ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-08-25 13:31:57 -04:00
|
|
|
PropertyList p = *_changes;
|
|
|
|
p.invert ();
|
|
|
|
s->apply_changes (p);
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
StatefulDiffCommand::get_state ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
/* XXX should we throw? */
|
|
|
|
return * new XMLNode("");
|
|
|
|
}
|
|
|
|
|
2010-02-09 17:28:46 -05:00
|
|
|
XMLNode* node = new XMLNode (X_("StatefulDiffCommand"));
|
|
|
|
|
2016-08-28 08:56:44 -04:00
|
|
|
node->set_property ("obj-id", s->id());
|
|
|
|
node->set_property ("type-name", demangled_name (*s.get()));
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-08-25 13:31:57 -04:00
|
|
|
XMLNode* changes = new XMLNode (X_("Changes"));
|
2010-03-01 19:00:00 -05:00
|
|
|
|
2010-08-25 13:31:57 -04:00
|
|
|
_changes->get_changes_as_xml (changes);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-08-25 13:31:57 -04:00
|
|
|
node->add_child_nocopy (*changes);
|
2010-02-09 17:28:46 -05:00
|
|
|
|
|
|
|
return *node;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
2010-08-25 21:44:11 -04:00
|
|
|
|
|
|
|
bool
|
|
|
|
StatefulDiffCommand::empty () const
|
|
|
|
{
|
|
|
|
return _changes->empty();
|
|
|
|
}
|