13
0

Add method to find a section location by position

This commit is contained in:
Ben Loftis 2023-09-03 14:17:08 -05:00 committed by Robin Gareus
parent 5c888f2b90
commit 101744b812
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
2 changed files with 40 additions and 7 deletions

View File

@ -29,6 +29,7 @@
#include <list>
#include <iostream>
#include <map>
#include <vector>
#include <sys/types.h>
@ -246,6 +247,7 @@ class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDes
{
public:
typedef std::list<Location *> LocationList;
typedef std::pair<Temporal::timepos_t, Location*> 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<LocationPair>&) const;
LocationList locations;
Location* current_location;
mutable Glib::Threads::RWLock _lock;

View File

@ -1370,18 +1370,16 @@ Locations::set_state (const XMLNode& node, int version)
}
typedef std::pair<timepos_t,Location*> 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<LocationPair>& locs) const
{
vector<LocationPair> 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<LocationPair> 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<LocationPair> 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
{