From cd8a19f055095a348caec1cdcbb517b38eb54fd0 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Fri, 26 Feb 2016 22:28:06 -0500 Subject: [PATCH] Define copy, move, and Swap for TrackList --- src/Track.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- src/Track.h | 16 +++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/Track.cpp b/src/Track.cpp index f536e0981..a559fc82a 100644 --- a/src/Track.cpp +++ b/src/Track.cpp @@ -713,6 +713,47 @@ TrackList::TrackList(bool destructorDeletesTracks) tail = NULL; } +TrackList::TrackList(const TrackList &that) +{ + DoAssign(that); +} + +TrackList& TrackList::operator= (const TrackList &that) +{ + this->Clear(mDestructorDeletesTracks); + DoAssign(that); + return *this; +} + +TrackList::TrackList(TrackList &&that) +{ + Swap(that); +} + +TrackList &TrackList::operator= (TrackList &&that) +{ + if (this != &that) { + this->Clear(mDestructorDeletesTracks); + Swap(that); + } + return *this; +} + +void TrackList::DoAssign(const TrackList &that) +{ + mDestructorDeletesTracks = true; + TrackListConstIterator it(&that); + for (const Track *track = it.First(); track; track = it.Next()) + Add(track->Duplicate()); +} + +void TrackList::Swap(TrackList &that) +{ + std::swap(mDestructorDeletesTracks, that.mDestructorDeletesTracks); + std::swap(head, that.head); + std::swap(tail, that.tail); +} + TrackList::~TrackList() { Clear(mDestructorDeletesTracks); @@ -1025,7 +1066,7 @@ bool TrackList::CanMoveDown(Track * t) const // The complication is that the tracks are stored in a single // linked list, and pairs of tracks are marked only by a flag // in one of the tracks. -void TrackList::Swap(TrackListNode * s1, TrackListNode * s2) +void TrackList::SwapNodes(TrackListNode * s1, TrackListNode * s2) { Track *link; Track *source[4]; @@ -1092,7 +1133,7 @@ bool TrackList::MoveUp(Track * t) if (t) { Track *p = GetPrev(t, true); if (p) { - Swap((TrackListNode *)p->GetNode(), (TrackListNode *)t->GetNode()); + SwapNodes((TrackListNode *)p->GetNode(), (TrackListNode *)t->GetNode()); return true; } } @@ -1105,7 +1146,7 @@ bool TrackList::MoveDown(Track * t) if (t) { Track *n = GetNext(t, true); if (n) { - Swap((TrackListNode *)t->GetNode(), (TrackListNode *)n->GetNode()); + SwapNodes((TrackListNode *)t->GetNode(), (TrackListNode *)n->GetNode()); return true; } } diff --git a/src/Track.h b/src/Track.h index f19515d29..c36e7b20f 100644 --- a/src/Track.h +++ b/src/Track.h @@ -383,6 +383,18 @@ class AUDACITY_DLL_API TrackList final : public wxEvtHandler // Create an empty TrackList TrackList(bool destructorDeletesTracks = false); + // Allow copy -- a deep copy that duplicates all tracks + TrackList(const TrackList &that); + TrackList &operator= (const TrackList &that); + + // Allow move + TrackList(TrackList &&that); + TrackList& operator= (TrackList&&); + + // Move is defined in terms of Swap + void Swap(TrackList &that); + + // Destructor virtual ~TrackList(); @@ -462,11 +474,13 @@ class AUDACITY_DLL_API TrackList final : public wxEvtHandler #endif private: + void DoAssign(const TrackList &that); + void RecalcPositions(const TrackListNode *node); void UpdatedEvent(const TrackListNode *node); void ResizedEvent(const TrackListNode *node); - void Swap(TrackListNode * s1, TrackListNode * s2); + void SwapNodes(TrackListNode * s1, TrackListNode * s2); TrackListNode *head; TrackListNode *tail;