1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-21 15:08:01 +02:00

More stl idiom for TrackList and its iterators

This commit is contained in:
Paul Licameli 2018-01-14 16:55:49 -05:00
parent ba61e30cb2
commit 0265b8792d
8 changed files with 22 additions and 19 deletions

View File

@ -2912,7 +2912,7 @@ void AudacityProject::SortTracks(int flags)
// This one place outside of TrackList where we must use undisguised // This one place outside of TrackList where we must use undisguised
// std::list iterators! Avoid this elsewhere! // std::list iterators! Avoid this elsewhere!
std::vector<ListOfTracks::iterator> arr; std::vector<ListOfTracks::iterator> arr;
arr.reserve(mTracks->GetCount()); arr.reserve(mTracks->size());
bool lastTrackLinked = false; bool lastTrackLinked = false;
//sort by linked tracks. Assumes linked track follows owner in list. //sort by linked tracks. Assumes linked track follows owner in list.
@ -4332,7 +4332,7 @@ bool AudacityProject::DoEffect(const PluginID & ID, int flags)
wxGetApp().SetMissingAliasedFileWarningShouldShow(true); wxGetApp().SetMissingAliasedFileWarningShouldShow(true);
int nTracksOriginally = GetTrackCount(); auto nTracksOriginally = GetTrackCount();
TrackListIterator iter(GetTracks()); TrackListIterator iter(GetTracks());
Track *t = iter.First(); Track *t = iter.First();
WaveTrack *newTrack{}; WaveTrack *newTrack{};

View File

@ -1484,7 +1484,7 @@ void AudacityProject::SetProjectTitle( int number)
bool AudacityProject::GetIsEmpty() bool AudacityProject::GetIsEmpty()
{ {
return mTracks->IsEmpty(); return mTracks->empty();
} }
bool AudacityProject::SnapSelection() bool AudacityProject::SnapSelection()
@ -2896,7 +2896,7 @@ void AudacityProject::OpenFiles(AudacityProject *proj)
// there are no tracks, but there's an Undo history, etc, then // there are no tracks, but there's an Undo history, etc, then
// bad things can happen, including data files moving to the NEW // bad things can happen, including data files moving to the NEW
// project directory, etc. // project directory, etc.
if ( proj && ( proj->mDirty || !proj->mTracks->IsEmpty() ) ) if ( proj && ( proj->mDirty || !proj->mTracks->empty() ) )
proj = nullptr; proj = nullptr;
// This project is clean; it's never been touched. Therefore // This project is clean; it's never been touched. Therefore
@ -4145,7 +4145,7 @@ AudacityProject::AddImportedTracks(const wxString &fileName,
const auto numTracks = newTracks.size(); const auto numTracks = newTracks.size();
SelectNone(); SelectNone();
bool initiallyEmpty = mTracks->IsEmpty(); bool initiallyEmpty = mTracks->empty();
double newRate = 0; double newRate = 0;
wxString trackNameBase = fileName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.'); wxString trackNameBase = fileName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
bool isLinked = false; bool isLinked = false;

View File

@ -176,7 +176,7 @@ class AUDACITY_DLL_API AudacityProject final : public wxFrame,
TrackList *GetTracks() { return mTracks.get(); } TrackList *GetTracks() { return mTracks.get(); }
const TrackList *GetTracks() const { return mTracks.get(); } const TrackList *GetTracks() const { return mTracks.get(); }
const int GetTrackCount(){ return GetTracks()->GetCount(); } size_t GetTrackCount() const { return GetTracks()->size(); }
UndoManager *GetUndoManager() { return mUndoManager.get(); } UndoManager *GetUndoManager() { return mUndoManager.get(); }
sampleFormat GetDefaultFormat() { return mDefaultFormat; } sampleFormat GetDefaultFormat() { return mDefaultFormat; }

View File

@ -1142,18 +1142,17 @@ bool TrackList::Contains(const Track * t) const
return make_iterator_range( *this ).contains( t ); return make_iterator_range( *this ).contains( t );
} }
bool TrackList::IsEmpty() const bool TrackList::empty() const
{ {
return empty(); return begin() == end();
} }
int TrackList::GetCount() const size_t TrackList::size() const
{ {
int cnt = 0; int cnt = 0;
if (!empty()) { if (!empty())
cnt = back()->GetIndex() + 1; cnt = back()->GetIndex() + 1;
}
return cnt; return cnt;
} }
@ -1303,7 +1302,7 @@ namespace {
double doubleMin(double a, double b) { return std::min(a, b); } double doubleMin(double a, double b) { return std::min(a, b); }
double doubleMax(double a, double b) { return std::max(a, b); } double doubleMax(double a, double b) { return std::max(a, b); }
inline double Accumulate inline double Accumulate
(const ListOfTracks &list, (const TrackList &list,
double (Track::*memfn)() const, double (Track::*memfn)() const,
double (*combine)(double, double)) double (*combine)(double, double))
{ {
@ -1316,8 +1315,8 @@ namespace {
auto iter = list.begin(); auto iter = list.begin();
double acc = (**iter++.*memfn)(); double acc = (**iter++.*memfn)();
return std::accumulate(iter, list.end(), acc, return std::accumulate(iter, list.end(), acc,
[=](double acc, const ListOfTracks::value_type &pTrack) { [=](double acc, const Track *pTrack) {
return combine(acc, (*pTrack.*memfn)()); return combine(acc, (*pTrack.*memfn)());
}); });
} }
} }

View File

@ -365,6 +365,8 @@ class AUDACITY_DLL_API TrackListIterator /* not final */
Track *operator * () const; Track *operator * () const;
TrackListIterator &operator++ () { (void) Next(); return *this; } TrackListIterator &operator++ () { (void) Next(); return *this; }
TrackListIterator operator++ (int)
{ auto copy = *this; operator++(); return copy; }
bool operator == (const TrackListIterator &other) const; bool operator == (const TrackListIterator &other) const;
bool operator != (const TrackListIterator &other) const bool operator != (const TrackListIterator &other) const
@ -415,6 +417,8 @@ public:
const Track *operator * () const { return *mIter; } const Track *operator * () const { return *mIter; }
TrackListConstIterator &operator++ () { (void) Next(); return *this; } TrackListConstIterator &operator++ () { (void) Next(); return *this; }
TrackListConstIterator operator++ (int)
{ auto copy = *this; operator++(); return copy; }
bool operator == (const TrackListConstIterator &other) const bool operator == (const TrackListConstIterator &other) const
{ return mIter == other.mIter; } { return mIter == other.mIter; }
@ -666,8 +670,8 @@ class TrackList final : public wxEvtHandler, public ListOfTracks
return {}; return {};
} }
bool IsEmpty() const; bool empty() const;
int GetCount() const; size_t size() const;
double GetStartTime() const; double GetStartTime() const;
double GetEndTime() const; double GetEndTime() const;

View File

@ -1088,7 +1088,7 @@ bool TrackPanel::IsMouseCaptured()
void TrackPanel::UpdateViewIfNoTracks() void TrackPanel::UpdateViewIfNoTracks()
{ {
if (mTracks->IsEmpty()) if (mTracks->empty())
{ {
// BG: There are no more tracks on screen // BG: There are no more tracks on screen
//BG: Set zoom to normal //BG: Set zoom to normal

View File

@ -108,7 +108,7 @@ bool SelectCommand::Apply(CommandExecutionContext context)
Error(wxT("Trying to select a negatively numbered track!")); Error(wxT("Trying to select a negatively numbered track!"));
return false; return false;
} }
if (lastTrack >= tracks->GetCount()) if (lastTrack >= (long)tracks->size())
{ {
Error(wxT("Trying to select higher number track than exists!")); Error(wxT("Trying to select higher number track than exists!"));
return false; return false;

View File

@ -29,7 +29,7 @@ unsigned CommonTrackPanelCell::HandleWheelRotation
{ {
using namespace RefreshCode; using namespace RefreshCode;
if (pProject->GetTracks()->IsEmpty()) if (pProject->GetTracks()->empty())
// Scrolling and Zoom in and out commands are disabled when there are no tracks. // Scrolling and Zoom in and out commands are disabled when there are no tracks.
// This should be disabled too for consistency. Otherwise // This should be disabled too for consistency. Otherwise
// you do see changes in the time ruler. // you do see changes in the time ruler.