From d87b10037bde914b6ae4dd221b31f4bdfc3586ca Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 5 Aug 2023 11:31:36 -0600 Subject: [PATCH] temporal: new object to try to make domain swaps undo-able --- libs/ardour/ardour/midi_region.h | 2 +- libs/temporal/domainswap.cc | 54 ++++++++++++++++++++++++++ libs/temporal/temporal/domainswap.h | 60 +++++++++++++++++++++++++++++ libs/temporal/temporal/tempo.h | 1 + libs/temporal/temporal/types.h | 6 --- libs/temporal/wscript | 1 + 6 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 libs/temporal/domainswap.cc create mode 100644 libs/temporal/temporal/domainswap.h diff --git a/libs/ardour/ardour/midi_region.h b/libs/ardour/ardour/midi_region.h index dc9cc66e46..c6d9621b34 100644 --- a/libs/ardour/ardour/midi_region.h +++ b/libs/ardour/ardour/midi_region.h @@ -26,8 +26,8 @@ #include #include "temporal/beats.h" +#include "temporal/domainswap.h" #include "temporal/range.h" -#include "temporal/types.h" #include "pbd/string_convert.h" diff --git a/libs/temporal/domainswap.cc b/libs/temporal/domainswap.cc new file mode 100644 index 0000000000..5558062480 --- /dev/null +++ b/libs/temporal/domainswap.cc @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 Paul Davis + * + * 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 "temporal/domainswap.h" + +using namespace Temporal; + +void +TimeDomainCommand::add (TimeDomainSwapper& tds) +{ + tds.DropReferences.connect_same_thread (tds_connections, boost::bind (&TimeDomainCommand::going_away, this, &tds)); + swappers.insert (&tds); +} + +void +TimeDomainCommand::going_away (TimeDomainSwapper* tds) +{ + Swappers::iterator i = swappers.find (tds); + + if (i != swappers.end()) { + swappers.erase (i); + } +} + +void +TimeDomainCommand::operator() () +{ + for (auto & swapper : swappers) { + swapper->swap_domain (from, to); + } +} + +void +TimeDomainCommand::undo () +{ + for (auto & swapper : swappers) { + swapper->swap_domain (to, from); + } +} diff --git a/libs/temporal/temporal/domainswap.h b/libs/temporal/temporal/domainswap.h new file mode 100644 index 0000000000..e359b63d47 --- /dev/null +++ b/libs/temporal/temporal/domainswap.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 Paul Davis + * + * 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. + */ + +#ifndef __temporal_domain_swap_h__ +#define __temporal_domain_swap_h__ + +#include + +#include "pbd/command.h" +#include "pbd/destructible.h" +#include "pbd/signals.h" + +#include "temporal/types.h" + +namespace Temporal { + +struct LIBTEMPORAL_API TimeDomainSwapper : public virtual PBD::Destructible { + virtual ~TimeDomainSwapper() {} + virtual void swap_domain (Temporal::TimeDomain from, Temporal::TimeDomain to) = 0; +}; + + +struct LIBTEMPORAL_API TimeDomainCommand : public Command { + public: + TimeDomainCommand (TimeDomain f, TimeDomain t) : from (f), to (t) {} + void add (TimeDomainSwapper&); + + void operator() (); + void undo (); + + private: + TimeDomain from; + TimeDomain to; + + typedef std::set Swappers; + Swappers swappers; + PBD::ScopedConnectionList tds_connections; + + void going_away (TimeDomainSwapper*); + +}; + +} + +#endif /* __tmeporal_domain_swap_h__ */ diff --git a/libs/temporal/temporal/tempo.h b/libs/temporal/temporal/tempo.h index e3a4bdc7a0..e772f51a55 100644 --- a/libs/temporal/temporal/tempo.h +++ b/libs/temporal/temporal/tempo.h @@ -42,6 +42,7 @@ #include "temporal/beats.h" #include "temporal/bbt_argument.h" #include "temporal/bbt_time.h" +#include "temporal/domainswap.h" #include "temporal/superclock.h" #include "temporal/timeline.h" #include "temporal/types.h" diff --git a/libs/temporal/temporal/types.h b/libs/temporal/temporal/types.h index d46a944906..ed7a88013a 100644 --- a/libs/temporal/temporal/types.h +++ b/libs/temporal/temporal/types.h @@ -124,14 +124,8 @@ enum RoundMode { extern void setup_enum_writer (); -struct LIBTEMPORAL_API TimeDomainSwapper { - virtual ~TimeDomainSwapper() {} - virtual void swap_domain (Temporal::TimeDomain from, Temporal::TimeDomain to) = 0; -}; - } std::ostream& operator<< (std::ostream& o, Temporal::ratio_t const & r); - #endif /* __libpbd_position_types_h__ */ diff --git a/libs/temporal/wscript b/libs/temporal/wscript index 8f3518ae4e..bbdbc620c1 100644 --- a/libs/temporal/wscript +++ b/libs/temporal/wscript @@ -28,6 +28,7 @@ temporal_sources = [ 'debug.cc', 'bbt_time.cc', 'beats.cc', + 'domainswap.cc', 'enums.cc', 'range.cc', 'superclock.cc',