13
0
livetrax/libs/pbd/stateful_diff_command.cc

125 lines
3.2 KiB
C++
Raw Normal View History

/*
* 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.
*/
#include "pbd/stateful_diff_command.h"
#include "pbd/demangle.h"
#include "pbd/i18n.h"
2020-02-28 00:08:26 -05:00
#include "pbd/property_list.h"
#include "pbd/types_convert.h"
using namespace std;
using namespace PBD;
/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
* since the last time that clear_changes was called on it.
* @param s Stateful object.
*/
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s)
2020-02-28 00:08:26 -05:00
: _object (s)
, _changes (0)
{
_changes = s->get_changes_as_properties (this);
2020-02-28 00:08:26 -05:00
/* if the stateful object that this command refers to goes away,
be sure to notify owners of this command.
*/
2020-02-28 00:08:26 -05:00
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
}
2020-02-28 00:08:26 -05:00
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s, XMLNode const& n)
: _object (s)
2020-02-28 00:08:26 -05:00
, _changes (0)
{
2020-02-28 00:08:26 -05:00
const XMLNodeList& children (n.children ());
2020-02-28 00:08:26 -05:00
for (XMLNodeList::const_iterator i = children.begin (); i != children.end (); ++i) {
if ((*i)->name () == X_ ("Changes")) {
_changes = s->property_factory (**i);
}
}
2020-02-28 00:08:26 -05:00
assert (_changes != 0);
2020-02-28 00:08:26 -05:00
/* if the stateful object that this command refers to goes away,
be sure to notify owners of this command.
*/
2020-02-28 00:08:26 -05:00
s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
}
StatefulDiffCommand::~StatefulDiffCommand ()
{
2020-02-28 00:08:26 -05:00
delete _changes;
}
void
StatefulDiffCommand::operator() ()
{
2020-02-28 00:08:26 -05:00
boost::shared_ptr<Stateful> s (_object.lock ());
if (s) {
2020-02-28 00:08:26 -05:00
s->apply_changes (*_changes);
}
}
void
StatefulDiffCommand::undo ()
{
2020-02-28 00:08:26 -05:00
boost::shared_ptr<Stateful> s (_object.lock ());
if (s) {
PropertyList p = *_changes;
p.invert ();
2020-02-28 00:08:26 -05:00
s->apply_changes (p);
}
}
XMLNode&
StatefulDiffCommand::get_state () const
{
2020-02-28 00:08:26 -05:00
boost::shared_ptr<Stateful> s (_object.lock ());
if (!s) {
/* XXX should we throw? */
2020-02-28 00:08:26 -05:00
return *new XMLNode ("");
}
2020-02-28 00:08:26 -05:00
XMLNode* node = new XMLNode (X_ ("StatefulDiffCommand"));
2020-02-28 00:08:26 -05:00
node->set_property ("obj-id", s->id ());
node->set_property ("type-name", demangled_name (*s.get ()));
2020-02-28 00:08:26 -05:00
XMLNode* changes = new XMLNode (X_ ("Changes"));
2020-02-28 00:08:26 -05:00
_changes->get_changes_as_xml (changes);
2020-02-28 00:08:26 -05:00
node->add_child_nocopy (*changes);
return *node;
}
bool
StatefulDiffCommand::empty () const
{
2020-02-28 00:08:26 -05:00
return _changes->empty ();
}