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)
Printf(wxT("Cut: %d - %d \n"), x0 * chunkSize, (x0 + xlen) * chunkSize);
auto tmp = t->Cut(double (x0 * chunkSize), double ((x0 + xlen) * chunkSize));
if (!tmp) {
Track::Holder tmp;
try {
tmp = t->Cut(double (x0 * chunkSize), double ((x0 + xlen) * chunkSize));
}
catch (const AudacityException&) {
Printf(wxT("Trial %d\n"), z);
Printf(wxT("Cut (%d, %d) failed.\n"), (x0 * 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)
{
auto tmp = Copy(t0, t1);
if (!tmp)
return{};
if (!Clear(t0, t1))
return{};
//THROW_INCONSISTENCY_EXCEPTION
;
return tmp;
}
@ -2353,8 +2353,7 @@ Track::Holder LabelTrack::SplitCut(double t0, double t1)
// SplitCut() == Copy() + SplitDelete()
Track::Holder tmp = Copy(t0, t1);
if (!tmp)
return {};
if (!SplitDelete(t0, t1))
return {};

View File

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

View File

@ -435,7 +435,8 @@ int NoteTrack::GetVisibleChannels()
Track::Holder NoteTrack::Cut(double t0, double t1)
{
if (t1 <= t0)
return{};
//THROW_INCONSISTENCY_EXCEPTION
;
double len = t1-t0;
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
{
if (t1 <= t0)
return{};
//THROW_INCONSISTENCY_EXCEPTION
;
double len = t1-t0;
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()")))
return {};
//THROW_INCONSISTENCY_EXCEPTION
;
return dest;
}

View File

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

View File

@ -1173,20 +1173,17 @@ bool EffectEqualization::ProcessOne(int count, WaveTrack * t,
//remove the old audio and get the NEW
t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second);
auto toClipOutput = output->Copy(clipStartEndTimes[i].first-startT+offsetT0,clipStartEndTimes[i].second-startT+offsetT0);
if(toClipOutput)
{
//put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this.
wxUnusedVar(bResult);
//if the clip was only partially selected, the Paste will have created a split line. Join is needed to take care of this
//This is not true when the selection is fully contained within one clip (second half of conditional)
if( (clipRealStartEndTimes[i].first != clipStartEndTimes[i].first ||
clipRealStartEndTimes[i].second != clipStartEndTimes[i].second) &&
!(clipRealStartEndTimes[i].first <= startT &&
clipRealStartEndTimes[i].second >= startT+lenT) )
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
}
//put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this.
wxUnusedVar(bResult);
//if the clip was only partially selected, the Paste will have created a split line. Join is needed to take care of this
//This is not true when the selection is fully contained within one clip (second half of conditional)
if( (clipRealStartEndTimes[i].first != clipStartEndTimes[i].first ||
clipRealStartEndTimes[i].second != clipStartEndTimes[i].second) &&
!(clipRealStartEndTimes[i].first <= startT &&
clipRealStartEndTimes[i].second >= startT+lenT) )
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
}
}

View File

@ -558,19 +558,16 @@ bool EffectEqualization48x::ProcessTail(WaveTrack * t, WaveTrack * output, sampl
t->Clear(clipStartEndTimes[i].first,clipStartEndTimes[i].second);
// 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);
if(toClipOutput)
{
//put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this.
//if the clip was only partially selected, the Paste will have created a split line. Join is needed to take care of this
//This is not true when the selection is fully contained within one clip (second half of conditional)
if( (clipRealStartEndTimes[i].first != clipStartEndTimes[i].first ||
clipRealStartEndTimes[i].second != clipStartEndTimes[i].second) &&
!(clipRealStartEndTimes[i].first <= startT &&
clipRealStartEndTimes[i].second >= startT+lenT) )
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
}
//put the processed audio in
bool bResult = t->Paste(clipStartEndTimes[i].first, toClipOutput.get());
wxASSERT(bResult); // TO DO: Actually handle this.
//if the clip was only partially selected, the Paste will have created a split line. Join is needed to take care of this
//This is not true when the selection is fully contained within one clip (second half of conditional)
if( (clipRealStartEndTimes[i].first != clipStartEndTimes[i].first ||
clipRealStartEndTimes[i].second != clipStartEndTimes[i].second) &&
!(clipRealStartEndTimes[i].first <= startT &&
clipRealStartEndTimes[i].second >= startT+lenT) )
t->Join(clipRealStartEndTimes[i].first,clipRealStartEndTimes[i].second);
}
return true;
}