From cbaa335946316fdfe9885a7311bdc9cb2561e5bf Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 14 Jul 2023 21:32:40 -0600 Subject: [PATCH] temporal: some cleanup of the GridIterator API --- libs/ardour/session_transport.cc | 2 +- libs/temporal/tempo.cc | 24 +++++++++++++++----- libs/temporal/temporal/tempo.h | 38 +++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/libs/ardour/session_transport.cc b/libs/ardour/session_transport.cc index 2959329e2a..01097ce689 100644 --- a/libs/ardour/session_transport.cc +++ b/libs/ardour/session_transport.cc @@ -299,7 +299,7 @@ Session::locate (samplepos_t target_sample, bool for_loop_end, bool force, bool } _last_roll_location = _last_roll_or_reversal_location = _transport_sample; - _click_iterator.valid = false; + _click_iterator.invalidate (); Located (); /* EMIT SIGNAL */ } diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index 46fd27b7f0..7ab123ee8c 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -774,6 +774,17 @@ MusicTimePoint::set_name (std::string const & str) /* XXX need a signal or something to announce change */ } +bool +GridIterator::valid_for (TempoMap const & m, superclock_t start) const +{ + if (!valid || start != end || map != &m) { + return false; + } + + return true; +} + + /* TEMPOMAP */ TempoMap::TempoMap (Tempo const & initial_tempo, Meter const & initial_meter) @@ -2540,15 +2551,16 @@ TempoMap::get_grid (TempoMapPoints& ret, superclock_t rstart, superclock_t end, void TempoMap::get_grid_with_iterator (GridIterator& iter, TempoMapPoints& ret, superclock_t rstart, superclock_t end, uint32_t bar_mod, uint32_t beat_div) const { - DEBUG_TRACE (DEBUG::Grid, string_compose (">>> GRID-I START %1 .. %2 (barmod = %3) iter valid ? %4 iter for %5\n", rstart, end, bar_mod, iter.valid, iter.end)); + DEBUG_TRACE (DEBUG::Grid, string_compose (">>> GRID-I START %1 .. %2 (barmod = %3) iter valid ? %4 iter for %5\n", rstart, end, bar_mod, iter.valid_for (*this, rstart), iter.end)); - if (!iter.valid || rstart != iter.end) { + if (!iter.valid_for (*this, rstart)) { + std::cerr << "iterator @ " << &iter << " invalid, valid = " << iter.valid_for (*this, rstart) << " end " << iter.end << " rs " << rstart << std::endl; Points::const_iterator p = get_grid (ret, rstart, end, bar_mod, beat_div); if (!ret.empty()) { TempoMapPoint& tmp (ret.back()); - iter = GridIterator (&tmp.tempo(), &tmp.meter(), tmp.sclock(), tmp.beats(), tmp.bbt(), p, end); + iter = GridIterator (*this, &tmp.tempo(), &tmp.meter(), tmp.sclock(), tmp.beats(), tmp.bbt(), p, end); } else { - iter.end = end; + iter.catch_up_to (end); } return; } @@ -2567,9 +2579,9 @@ TempoMap::get_grid_with_iterator (GridIterator& iter, TempoMapPoints& ret, super if (!ret.empty()) { TempoMapPoint& tmp (ret.back()); - iter = GridIterator (&metric.tempo(), &metric.meter(), tmp.sclock(), tmp.beats(), tmp.bbt(), p, end); + iter = GridIterator (*this, &metric.tempo(), &metric.meter(), tmp.sclock(), tmp.beats(), tmp.bbt(), p, end); } else { - iter.end = end; + iter.catch_up_to (end); } DEBUG_TRACE (DEBUG::Grid, "<<< GRID-I DONE\n"); diff --git a/libs/temporal/temporal/tempo.h b/libs/temporal/temporal/tempo.h index 24a466f20e..1a0bacc3f6 100644 --- a/libs/temporal/temporal/tempo.h +++ b/libs/temporal/temporal/tempo.h @@ -647,30 +647,39 @@ typedef boost::intrusive::list> MusicTimes; typedef boost::intrusive::list> Points; -struct LIBTEMPORAL_API GridIterator +class LIBTEMPORAL_API GridIterator { - GridIterator () : valid (false), sclock (0), tempo (nullptr), meter (nullptr), end (0) {} - GridIterator (TempoPoint const * tp, MeterPoint const * mp, superclock_t sc, Beats const & b, BBT_Time const & bb, Points::const_iterator p, superclock_t e) - : valid (false) - , sclock (sc) + public: + GridIterator () : sclock (0), tempo (nullptr), meter (nullptr), end (0), valid (false), map (nullptr) {} + GridIterator (TempoMap const & m, TempoPoint const * tp, MeterPoint const * mp, superclock_t sc, Beats const & b, BBT_Time const & bb, Points::const_iterator p, superclock_t e) + : sclock (sc) , beats (b) , bbt (bb) , tempo (tp) , meter (mp) , points_iterator (p) , end (e) + , valid (false) + , map (&m) { valid = (tempo && meter); } - bool valid; - superclock_t sclock; - Beats beats; - BBT_Time bbt; - TempoPoint const * tempo; - MeterPoint const * meter; + bool valid_for (TempoMap const & map, superclock_t start) const; + void catch_up_to (superclock_t e) { end = e; } + void invalidate () { valid = false; } + + superclock_t sclock; + Beats beats; + BBT_Time bbt; + TempoPoint const * tempo; + MeterPoint const * meter; Points::const_iterator points_iterator; - superclock_t end; + superclock_t end; + + private: + bool valid; + TempoMap const * map; }; class /*LIBTEMPORAL_API*/ TempoMap : public PBD::StatefulDestructible @@ -940,11 +949,14 @@ class /*LIBTEMPORAL_API*/ TempoMap : public PBD::StatefulDestructible Meters const & meters() const { return _meters; } MusicTimes const & bartimes() const { return _bartimes; } + + LIBTEMPORAL_API Points::const_iterator get_grid (TempoMapPoints & points, superclock_t start, superclock_t end, uint32_t bar_mod = 0, uint32_t beat_div = 1) const; + LIBTEMPORAL_API void grid (TempoMapPoints& points, superclock_t start, superclock_t end, uint32_t bar_mod = 0, uint32_t beat_div = 1) const { get_grid (points, start, end, bar_mod, beat_div); } - LIBTEMPORAL_API Points::const_iterator get_grid (TempoMapPoints & points, superclock_t start, superclock_t end, uint32_t bar_mod = 0, uint32_t beat_div = 1) const; + LIBTEMPORAL_API void get_grid_with_iterator (GridIterator& iter, TempoMapPoints& ret, superclock_t rstart, superclock_t end, uint32_t bar_mod = 0, uint32_t beat_div = 1) const; struct EmptyTempoMapException : public std::exception {