2010-02-09 14:44:24 +00:00
|
|
|
/*
|
2019-08-03 05:10:55 +02: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 14:44:24 +00:00
|
|
|
|
|
|
|
|
#include "pbd/stateful_diff_command.h"
|
2010-03-02 19:12:01 +00:00
|
|
|
#include "pbd/demangle.h"
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2020-02-28 06:08:26 +01:00
|
|
|
#include "pbd/property_list.h"
|
|
|
|
|
#include "pbd/types_convert.h"
|
2010-02-09 14:44:24 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
|
|
/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
|
2010-08-25 17:32:08 +00:00
|
|
|
* since the last time that clear_changes was called on it.
|
2010-02-09 14:44:24 +00:00
|
|
|
* @param s Stateful object.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-16 16:33:28 -07:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (std::shared_ptr<StatefulDestructible> s)
|
2020-02-28 06:08:26 +01:00
|
|
|
: _object (s)
|
2025-11-11 12:52:38 -07:00
|
|
|
, _changes (nullptr)
|
2010-02-09 14:44:24 +00:00
|
|
|
{
|
2010-08-25 17:31:57 +00:00
|
|
|
_changes = s->get_changes_as_properties (this);
|
2010-06-23 20:14:07 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
/* if the stateful object that this command refers to goes away,
|
2010-06-23 20:14:07 +00:00
|
|
|
be sure to notify owners of this command.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-11 12:54:37 -07:00
|
|
|
if (!_changes || _changes->empty()) {
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, std::bind (Destructible::drop_and_kill, this));
|
|
|
|
|
}
|
2010-02-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-16 16:33:28 -07:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (std::shared_ptr<StatefulDestructible> s, XMLNode const& n)
|
2010-02-09 22:28:46 +00:00
|
|
|
: _object (s)
|
2025-11-11 12:52:38 -07:00
|
|
|
, _changes (nullptr)
|
2010-02-09 22:28:46 +00:00
|
|
|
{
|
2020-02-28 06:08:26 +01:00
|
|
|
const XMLNodeList& children (n.children ());
|
2010-03-02 00:00:00 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
for (XMLNodeList::const_iterator i = children.begin (); i != children.end (); ++i) {
|
|
|
|
|
if ((*i)->name () == X_ ("Changes")) {
|
|
|
|
|
_changes = s->property_factory (**i);
|
|
|
|
|
}
|
2010-08-25 17:31:57 +00:00
|
|
|
}
|
2010-03-02 00:00:00 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
assert (_changes != 0);
|
2010-06-23 20:14:07 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
/* if the stateful object that this command refers to goes away,
|
2010-06-23 20:14:07 +00:00
|
|
|
be sure to notify owners of this command.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-11 12:54:37 -07:00
|
|
|
if (_changes->empty()) {
|
|
|
|
|
s->DropReferences.connect_same_thread (*this, std::bind (Destructible::drop_and_kill, this));
|
|
|
|
|
}
|
2010-02-09 22:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-09 14:44:24 +00:00
|
|
|
StatefulDiffCommand::~StatefulDiffCommand ()
|
|
|
|
|
{
|
2020-02-28 06:08:26 +01:00
|
|
|
delete _changes;
|
2010-02-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
StatefulDiffCommand::operator() ()
|
|
|
|
|
{
|
2023-02-16 16:33:28 -07:00
|
|
|
std::shared_ptr<Stateful> s (_object.lock ());
|
2010-02-11 23:10:29 +00:00
|
|
|
|
|
|
|
|
if (s) {
|
2020-02-28 06:08:26 +01:00
|
|
|
s->apply_changes (*_changes);
|
2010-02-11 23:10:29 +00:00
|
|
|
}
|
2010-02-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
StatefulDiffCommand::undo ()
|
|
|
|
|
{
|
2023-02-16 16:33:28 -07:00
|
|
|
std::shared_ptr<Stateful> s (_object.lock ());
|
2010-02-11 23:10:29 +00:00
|
|
|
|
|
|
|
|
if (s) {
|
2010-08-25 17:31:57 +00:00
|
|
|
PropertyList p = *_changes;
|
|
|
|
|
p.invert ();
|
2020-02-28 06:08:26 +01:00
|
|
|
s->apply_changes (p);
|
2010-02-11 23:10:29 +00:00
|
|
|
}
|
2010-02-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XMLNode&
|
2022-04-06 21:56:32 -06:00
|
|
|
StatefulDiffCommand::get_state () const
|
2010-02-09 14:44:24 +00:00
|
|
|
{
|
2023-02-16 16:33:28 -07:00
|
|
|
std::shared_ptr<Stateful> s (_object.lock ());
|
2010-02-11 23:10:29 +00:00
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
|
/* XXX should we throw? */
|
2020-02-28 06:08:26 +01:00
|
|
|
return *new XMLNode ("");
|
2010-02-11 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
XMLNode* node = new XMLNode (X_ ("StatefulDiffCommand"));
|
2010-02-09 22:28:46 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
node->set_property ("obj-id", s->id ());
|
|
|
|
|
node->set_property ("type-name", demangled_name (*s.get ()));
|
2010-03-02 00:00:00 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
XMLNode* changes = new XMLNode (X_ ("Changes"));
|
2010-03-02 00:00:00 +00:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
_changes->get_changes_as_xml (changes);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2020-02-28 06:08:26 +01:00
|
|
|
node->add_child_nocopy (*changes);
|
2010-02-09 22:28:46 +00:00
|
|
|
|
|
|
|
|
return *node;
|
2010-02-09 14:44:24 +00:00
|
|
|
}
|
2010-08-26 01:44:11 +00:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
StatefulDiffCommand::empty () const
|
|
|
|
|
{
|
2020-02-28 06:08:26 +01:00
|
|
|
return _changes->empty ();
|
2010-08-26 01:44:11 +00:00
|
|
|
}
|