1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-12-17 22:21:17 +01:00

Define and use copy and move for TrackList, simplifying recording code

This commit is contained in:
Paul Licameli
2016-03-07 06:43:42 -05:00
parent 72705e8f31
commit aee12f5755
3 changed files with 70 additions and 27 deletions

View File

@@ -713,6 +713,49 @@ TrackList::TrackList(bool destructorDeletesTracks)
tail = NULL; tail = NULL;
} }
TrackList::TrackList(const TrackList &that)
{
DoAssign(that);
}
TrackList& TrackList::operator= (const TrackList &that)
{
if (this != &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() TrackList::~TrackList()
{ {
Clear(mDestructorDeletesTracks); Clear(mDestructorDeletesTracks);
@@ -1025,7 +1068,7 @@ bool TrackList::CanMoveDown(Track * t) const
// The complication is that the tracks are stored in a single // The complication is that the tracks are stored in a single
// linked list, and pairs of tracks are marked only by a flag // linked list, and pairs of tracks are marked only by a flag
// in one of the tracks. // in one of the tracks.
void TrackList::Swap(TrackListNode * s1, TrackListNode * s2) void TrackList::SwapNodes(TrackListNode * s1, TrackListNode * s2)
{ {
Track *link; Track *link;
Track *source[4]; Track *source[4];
@@ -1092,7 +1135,7 @@ bool TrackList::MoveUp(Track * t)
if (t) { if (t) {
Track *p = GetPrev(t, true); Track *p = GetPrev(t, true);
if (p) { if (p) {
Swap((TrackListNode *)p->GetNode(), (TrackListNode *)t->GetNode()); SwapNodes((TrackListNode *)p->GetNode(), (TrackListNode *)t->GetNode());
return true; return true;
} }
} }
@@ -1105,7 +1148,7 @@ bool TrackList::MoveDown(Track * t)
if (t) { if (t) {
Track *n = GetNext(t, true); Track *n = GetNext(t, true);
if (n) { if (n) {
Swap((TrackListNode *)t->GetNode(), (TrackListNode *)n->GetNode()); SwapNodes((TrackListNode *)t->GetNode(), (TrackListNode *)n->GetNode());
return true; return true;
} }
} }

View File

@@ -394,6 +394,18 @@ class AUDACITY_DLL_API TrackList final : public wxEvtHandler
// Create an empty TrackList // Create an empty TrackList
TrackList(bool destructorDeletesTracks = false); 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 // Destructor
virtual ~TrackList(); virtual ~TrackList();
@@ -473,11 +485,13 @@ class AUDACITY_DLL_API TrackList final : public wxEvtHandler
#endif #endif
private: private:
void DoAssign(const TrackList &that);
void RecalcPositions(const TrackListNode *node); void RecalcPositions(const TrackListNode *node);
void UpdatedEvent(const TrackListNode *node); void UpdatedEvent(const TrackListNode *node);
void ResizedEvent(const TrackListNode *node); void ResizedEvent(const TrackListNode *node);
void Swap(TrackListNode * s1, TrackListNode * s2); void SwapNodes(TrackListNode * s1, TrackListNode * s2);
TrackListNode *head; TrackListNode *head;
TrackListNode *tail; TrackListNode *tail;

View File

@@ -799,8 +799,8 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
SetRecord(true, mRecord->WasShiftDown()); SetRecord(true, mRecord->WasShiftDown());
if (p) { if (p) {
TrackList *t = p->GetTracks(); TrackList *trackList = p->GetTracks();
TrackListIterator it(t); TrackListIterator it(trackList);
if(it.First() == NULL) if(it.First() == NULL)
mRecord->SetShift(false); mRecord->SetShift(false);
double t0 = p->GetSel0(); double t0 = p->GetSel0();
@@ -818,7 +818,7 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
gPrefs->Read(wxT("/AudioIO/Duplex"), &duplex, true); gPrefs->Read(wxT("/AudioIO/Duplex"), &duplex, true);
if(duplex){ if(duplex){
playbackTracks = t->GetWaveTrackArray(false); playbackTracks = trackList->GetWaveTrackArray(false);
#ifdef EXPERIMENTAL_MIDI_OUT #ifdef EXPERIMENTAL_MIDI_OUT
midiTracks = t->GetNoteTrackArray(false); midiTracks = t->GetNoteTrackArray(false);
#endif #endif
@@ -832,7 +832,7 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
// If SHIFT key was down, the user wants append to tracks // If SHIFT key was down, the user wants append to tracks
int recordingChannels = 0; int recordingChannels = 0;
TrackList tracksCopy(true); TrackList tracksCopy{};
bool tracksCopied = false; bool tracksCopied = false;
bool shifted = mRecord->WasShiftDown(); bool shifted = mRecord->WasShiftDown();
if (shifted) { if (shifted) {
@@ -879,12 +879,7 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
if (t1 < t0) { if (t1 < t0) {
if (!tracksCopied) { if (!tracksCopied) {
tracksCopied = true; tracksCopied = true;
TrackListIterator iter(t); tracksCopy = *trackList;
Track *trk = iter.First();
while (trk) {
tracksCopy.Add(trk->Duplicate());
trk = iter.Next();
}
} }
WaveTrack *newTrack = p->GetTrackFactory()->NewWaveTrack(); WaveTrack *newTrack = p->GetTrackFactory()->NewWaveTrack();
@@ -985,7 +980,7 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
// we add the tracks where recording is done into now. We remove them // we add the tracks where recording is done into now. We remove them
// later if starting the stream fails // later if starting the stream fails
for (unsigned int i = 0; i < newRecordingTracks.size(); i++) for (unsigned int i = 0; i < newRecordingTracks.size(); i++)
t->Add(newRecordingTracks[i]); trackList->Add(newRecordingTracks[i]);
} }
//Automated Input Level Adjustment Initialization //Automated Input Level Adjustment Initialization
@@ -1010,22 +1005,13 @@ void ControlToolBar::OnRecord(wxCommandEvent &evt)
else { else {
if (shifted) { if (shifted) {
// Restore the tracks to remove any inserted silence // Restore the tracks to remove any inserted silence
if (tracksCopied) { if (tracksCopied)
t->Clear(true); *trackList = std::move(tracksCopy);
TrackListIterator iter(&tracksCopy);
Track *trk = iter.First();
while (trk)
{
Track *tmp = trk;
trk = iter.RemoveCurrent();
t->Add(tmp);
}
}
} }
else { else {
// msmeyer: Delete recently added tracks if opening stream fails // msmeyer: Delete recently added tracks if opening stream fails
for (unsigned int i = 0; i < newRecordingTracks.size(); i++) { for (unsigned int i = 0; i < newRecordingTracks.size(); i++) {
t->Remove(newRecordingTracks[i]); trackList->Remove(newRecordingTracks[i]);
delete newRecordingTracks[i]; delete newRecordingTracks[i];
} }
} }