diff --git a/libs/ardour/ardour/location.h b/libs/ardour/ardour/location.h index acfbc2cc79..8889ca39af 100644 --- a/libs/ardour/ardour/location.h +++ b/libs/ardour/ardour/location.h @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -246,6 +247,7 @@ class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDes { public: typedef std::list LocationList; + typedef std::pair LocationPair; Locations (Session &); ~Locations (); @@ -299,6 +301,7 @@ public: timepos_t first_mark_after (timepos_t const &, bool include_special_ranges = false); Location* next_section (Location*, timepos_t&, timepos_t&) const; + Location* section_at (timepos_t const&, timepos_t&, timepos_t&) const; void marks_either_side (timepos_t const &, timepos_t &, timepos_t &) const; @@ -343,6 +346,8 @@ public: } private: + void sorted_section_locations (std::vector&) const; + LocationList locations; Location* current_location; mutable Glib::Threads::RWLock _lock; diff --git a/libs/ardour/location.cc b/libs/ardour/location.cc index b2faac7e9d..3b06c9b090 100644 --- a/libs/ardour/location.cc +++ b/libs/ardour/location.cc @@ -1370,18 +1370,16 @@ Locations::set_state (const XMLNode& node, int version) } -typedef std::pair LocationPair; - struct LocationStartEarlierComparison { - bool operator() (LocationPair a, LocationPair b) { + bool operator() (Locations::LocationPair a, Locations::LocationPair b) { return a.first < b.first; } }; struct LocationStartLaterComparison { - bool operator() (LocationPair a, LocationPair b) { + bool operator() (Locations::LocationPair a, Locations::LocationPair b) { return a.first > b.first; } }; @@ -1568,10 +1566,9 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_ before = *i; } -Location* -Locations::next_section (Location* l, timepos_t& start, timepos_t& end) const +void +Locations::sorted_section_locations (vector& locs) const { - vector locs; { Glib::Threads::RWLock::ReaderLock lm (_lock); @@ -1586,6 +1583,13 @@ Locations::next_section (Location* l, timepos_t& start, timepos_t& end) const LocationStartEarlierComparison cmp; sort (locs.begin(), locs.end(), cmp); +} + +Location* +Locations::next_section (Location* l, timepos_t& start, timepos_t& end) const +{ + vector locs; + sorted_section_locations (locs); if (locs.size () < 2) { return NULL; @@ -1619,6 +1623,30 @@ Locations::next_section (Location* l, timepos_t& start, timepos_t& end) const return NULL; } +Location* +Locations::section_at (timepos_t const& when, timepos_t& start, timepos_t& end) const +{ + vector locs; + sorted_section_locations (locs); + + if (locs.size () < 2) { + return NULL; + } + + Location* rv = NULL; + for (auto const& i: locs) { + if (when >= i.first) { + start = i.first; + rv = i.second; + } else { + end = i.first; + return rv; + } + } + + return NULL; +} + Location* Locations::session_range_location () const {