1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-17 17:17:40 +02:00

Factory methods will return non-NULL or throw

This commit is contained in:
Paul Licameli 2017-03-31 15:00:16 -04:00
parent f1b354b141
commit 6b84dc1c1d
9 changed files with 70 additions and 81 deletions

View File

@ -429,8 +429,11 @@ void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
if (mEditDetail) if (mEditDetail)
Printf(wxT("Cut: %d - %d \n"), x0 * chunkSize, (x0 + xlen) * chunkSize); Printf(wxT("Cut: %d - %d \n"), x0 * chunkSize, (x0 + xlen) * chunkSize);
auto tmp = t->Cut(double (x0 * chunkSize), double ((x0 + xlen) * chunkSize)); Track::Holder tmp;
if (!tmp) { try {
tmp = t->Cut(double (x0 * chunkSize), double ((x0 + xlen) * chunkSize));
}
catch (const AudacityException&) {
Printf(wxT("Trial %d\n"), z); Printf(wxT("Trial %d\n"), z);
Printf(wxT("Cut (%d, %d) failed.\n"), (x0 * chunkSize), Printf(wxT("Cut (%d, %d) failed.\n"), (x0 * chunkSize),
(x0 + xlen) * chunkSize); (x0 + xlen) * chunkSize);

View File

@ -2339,10 +2339,10 @@ bool LabelTrack::Save(wxTextFile * out, bool overwrite)
Track::Holder LabelTrack::Cut(double t0, double t1) Track::Holder LabelTrack::Cut(double t0, double t1)
{ {
auto tmp = Copy(t0, t1); auto tmp = Copy(t0, t1);
if (!tmp)
return{};
if (!Clear(t0, t1)) if (!Clear(t0, t1))
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
return tmp; return tmp;
} }
@ -2353,8 +2353,7 @@ Track::Holder LabelTrack::SplitCut(double t0, double t1)
// SplitCut() == Copy() + SplitDelete() // SplitCut() == Copy() + SplitDelete()
Track::Holder tmp = Copy(t0, t1); Track::Holder tmp = Copy(t0, t1);
if (!tmp)
return {};
if (!SplitDelete(t0, t1)) if (!SplitDelete(t0, t1))
return {}; return {};

View File

@ -4177,7 +4177,6 @@ void AudacityProject::OnCut()
dest = n->Copy(mViewInfo.selectedRegion.t0(), dest = n->Copy(mViewInfo.selectedRegion.t0(),
mViewInfo.selectedRegion.t1()); mViewInfo.selectedRegion.t1());
if (dest)
FinishCopy(n, std::move(dest), newClipboard); FinishCopy(n, std::move(dest), newClipboard);
} }
n = iter.Next(); n = iter.Next();
@ -4309,7 +4308,6 @@ void AudacityProject::OnCopy()
if (n->GetSelected()) { if (n->GetSelected()) {
auto dest = n->Copy(mViewInfo.selectedRegion.t0(), auto dest = n->Copy(mViewInfo.selectedRegion.t0(),
mViewInfo.selectedRegion.t1()); mViewInfo.selectedRegion.t1());
if (dest)
FinishCopy(n, std::move(dest), newClipboard); FinishCopy(n, std::move(dest), newClipboard);
} }
n = iter.Next(); n = iter.Next();
@ -4914,12 +4912,10 @@ void AudacityProject::OnDuplicate()
// Make copies not for clipboard but for direct addition to the project // Make copies not for clipboard but for direct addition to the project
auto dest = n->Copy(mViewInfo.selectedRegion.t0(), auto dest = n->Copy(mViewInfo.selectedRegion.t0(),
mViewInfo.selectedRegion.t1(), false); mViewInfo.selectedRegion.t1(), false);
if (dest) {
dest->Init(*n); dest->Init(*n);
dest->SetOffset(wxMax(mViewInfo.selectedRegion.t0(), n->GetOffset())); dest->SetOffset(wxMax(mViewInfo.selectedRegion.t0(), n->GetOffset()));
mTracks->Add(std::move(dest)); mTracks->Add(std::move(dest));
} }
}
if (n == l) { if (n == l) {
break; break;
@ -5129,9 +5125,7 @@ void AudacityProject::OnSplit()
double sel0 = mViewInfo.selectedRegion.t0(); double sel0 = mViewInfo.selectedRegion.t0();
double sel1 = mViewInfo.selectedRegion.t1(); double sel1 = mViewInfo.selectedRegion.t1();
dest = NULL; dest = n->Copy(sel0, sel1);
n->Copy(sel0, sel1, &dest);
if (dest) {
dest->Init(*n); dest->Init(*n);
dest->SetOffset(wxMax(sel0, n->GetOffset())); dest->SetOffset(wxMax(sel0, n->GetOffset()));
@ -5145,7 +5139,6 @@ void AudacityProject::OnSplit()
newTracks.Add(dest); newTracks.Add(dest);
} }
}
n = iter.Next(); n = iter.Next();
} }
@ -5190,11 +5183,9 @@ void AudacityProject::OnSplitNew()
mViewInfo.selectedRegion.t1()); mViewInfo.selectedRegion.t1());
} }
#endif #endif
if (dest) {
dest->SetOffset(wxMax(newt0, offset)); dest->SetOffset(wxMax(newt0, offset));
FinishCopy(n, std::move(dest), *mTracks); FinishCopy(n, std::move(dest), *mTracks);
} }
}
if (n == l) { if (n == l) {
break; break;

View File

@ -435,7 +435,8 @@ int NoteTrack::GetVisibleChannels()
Track::Holder NoteTrack::Cut(double t0, double t1) Track::Holder NoteTrack::Cut(double t0, double t1)
{ {
if (t1 <= t0) if (t1 <= t0)
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
double len = t1-t0; double len = t1-t0;
auto newTrack = std::make_unique<NoteTrack>(mDirManager); auto newTrack = std::make_unique<NoteTrack>(mDirManager);
@ -457,7 +458,8 @@ Track::Holder NoteTrack::Cut(double t0, double t1)
Track::Holder NoteTrack::Copy(double t0, double t1, bool) const Track::Holder NoteTrack::Copy(double t0, double t1, bool) const
{ {
if (t1 <= t0) if (t1 <= t0)
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
double len = t1-t0; double len = t1-t0;
auto newTrack = std::make_unique<NoteTrack>(mDirManager); auto newTrack = std::make_unique<NoteTrack>(mDirManager);

View File

@ -438,7 +438,8 @@ std::unique_ptr<Sequence> Sequence::Copy(sampleCount s0, sampleCount s1) const
} }
if (! ConsistencyCheck(wxT("Sequence::Copy()"))) if (! ConsistencyCheck(wxT("Sequence::Copy()")))
return {}; //THROW_INCONSISTENCY_EXCEPTION
;
return dest; return dest;
} }

View File

@ -309,7 +309,6 @@ bool Track::SyncLockAdjust(double oldT1, double newT1)
return true; return true;
auto tmp = Cut(oldT1, GetEndTime()); auto tmp = Cut(oldT1, GetEndTime());
if (!tmp) return false;
bool ret = Paste(newT1, tmp.get()); bool ret = Paste(newT1, tmp.get());
wxASSERT(ret); // TODO: handle this. wxASSERT(ret); // TODO: handle this.

View File

@ -532,15 +532,14 @@ bool WaveTrack::IsEmpty(double t0, double t1) const
Track::Holder WaveTrack::Cut(double t0, double t1) Track::Holder WaveTrack::Cut(double t0, double t1)
{ {
if (t1 < t0) if (t1 < t0)
return{}; // THROW_INCONSISTENCY_EXCEPTION
;
auto tmp = Copy(t0, t1); auto tmp = Copy(t0, t1);
if (!tmp)
return{};
if (!Clear(t0, t1)) if (!Clear(t0, t1))
return{}; // THROW_INCONSISTENCY_EXCEPTION
;
return tmp; return tmp;
} }
@ -548,14 +547,15 @@ Track::Holder WaveTrack::Cut(double t0, double t1)
Track::Holder WaveTrack::SplitCut(double t0, double t1) Track::Holder WaveTrack::SplitCut(double t0, double t1)
{ {
if (t1 < t0) if (t1 < t0)
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
// SplitCut is the same as 'Copy', then 'SplitDelete' // SplitCut is the same as 'Copy', then 'SplitDelete'
auto tmp = Copy(t0, t1); auto tmp = Copy(t0, t1);
if (!tmp)
return{};
if (!SplitDelete(t0, t1)) if (!SplitDelete(t0, t1))
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
return tmp; return tmp;
} }
@ -564,14 +564,15 @@ Track::Holder WaveTrack::SplitCut(double t0, double t1)
Track::Holder WaveTrack::CutAndAddCutLine(double t0, double t1) Track::Holder WaveTrack::CutAndAddCutLine(double t0, double t1)
{ {
if (t1 < t0) if (t1 < t0)
return {}; //THROW_INCONSISTENCY_EXCEPTION
;
// Cut is the same as 'Copy', then 'Delete' // Cut is the same as 'Copy', then 'Delete'
auto tmp = Copy(t0, t1); auto tmp = Copy(t0, t1);
if (!tmp)
return {};
if (!ClearAndAddCutLine(t0, t1)) if (!ClearAndAddCutLine(t0, t1))
return {}; //THROW_INCONSISTENCY_EXCEPTION
;
return tmp; return tmp;
} }
@ -636,7 +637,8 @@ bool WaveTrack::Trim (double t0, double t1)
Track::Holder WaveTrack::Copy(double t0, double t1, bool forClipboard) const Track::Holder WaveTrack::Copy(double t0, double t1, bool forClipboard) const
{ {
if (t1 <= t0) if (t1 <= t0)
return{}; //THROW_INCONSISTENCY_EXCEPTION
;
WaveTrack *newTrack; WaveTrack *newTrack;
Track::Holder result Track::Holder result
@ -1166,8 +1168,6 @@ bool WaveTrack::SyncLockAdjust(double oldT1, double newT1)
gPrefs->Read(wxT("/GUI/EditClipCanMove"), &clipsCanMove); gPrefs->Read(wxT("/GUI/EditClipCanMove"), &clipsCanMove);
if (clipsCanMove) { if (clipsCanMove) {
auto tmp = Cut (oldT1, GetEndTime() + 1.0/GetRate()); auto tmp = Cut (oldT1, GetEndTime() + 1.0/GetRate());
if (!tmp)
return false;
ret = Paste(newT1, tmp.get()); ret = Paste(newT1, tmp.get());
wxASSERT(ret); wxASSERT(ret);

View File

@ -1173,8 +1173,6 @@ bool EffectEqualization::ProcessOne(int count, WaveTrack * t,
//remove the old audio and get the NEW //remove the old audio and get the NEW
t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second); t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second);
auto toClipOutput = output->Copy(clipStartEndTimes[i].first-startT+offsetT0,clipStartEndTimes[i].second-startT+offsetT0); auto toClipOutput = output->Copy(clipStartEndTimes[i].first-startT+offsetT0,clipStartEndTimes[i].second-startT+offsetT0);
if(toClipOutput)
{
//put the processed audio in //put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get()); bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this. wxASSERT(bResult); // TO DO: Actually handle this.
@ -1188,7 +1186,6 @@ bool EffectEqualization::ProcessOne(int count, WaveTrack * t,
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second); t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
} }
} }
}
return bLoopSuccess; return bLoopSuccess;
} }

View File

@ -558,8 +558,6 @@ bool EffectEqualization48x::ProcessTail(WaveTrack * t, WaveTrack * output, sampl
t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second); t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second);
// output->Copy(clipStartEndTimes[i].first-startT+offsetT0,clipStartEndTimes[i].second-startT+offsetT0, &toClipOutput); // output->Copy(clipStartEndTimes[i].first-startT+offsetT0,clipStartEndTimes[i].second-startT+offsetT0, &toClipOutput);
auto toClipOutput = output->Copy(clipStartEndTimes[i].first-startT, clipStartEndTimes[i].second-startT); auto toClipOutput = output->Copy(clipStartEndTimes[i].first-startT, clipStartEndTimes[i].second-startT);
if(toClipOutput)
{
//put the processed audio in //put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get()); bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this. wxASSERT(bResult); // TO DO: Actually handle this.
@ -571,7 +569,6 @@ bool EffectEqualization48x::ProcessTail(WaveTrack * t, WaveTrack * output, sampl
clipRealStartEndTimes[i].second >= startT+lenT) ) clipRealStartEndTimes[i].second >= startT+lenT) )
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second); t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
} }
}
return true; return true;
} }