Skip to content

Commit 0832bcc

Browse files
committed
DirManager.cpp has fewer dependencies...
... To append files during recording or import, it doesn't depend on the subclasses of BlockFile, instead taking a factory function to which it gives the filename; and the choice of factory function is also lifted up through the level of class Sequence which is just above DirManager. This frees four files from dependency cycles, including DirManager.cpp but not yet Sequence.cpp
1 parent ca0fb19 commit 0832bcc

File tree

6 files changed

+85
-138
lines changed

6 files changed

+85
-138
lines changed

src/Dependencies.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ AliasedFile s.
4949
#include <wx/dataobj.h>
5050
#include <wx/stattext.h>
5151

52-
#include "BlockFile.h"
52+
#include "blockfile/SimpleBlockFile.h"
5353
#include "DirManager.h"
5454
#include "Prefs.h"
5555
#include "Project.h"
@@ -204,11 +204,14 @@ static void RemoveDependencies(AudacityProject *project,
204204
BlockFilePtr newBlockFile;
205205
{
206206
SampleBuffer buffer(len, format);
207-
// We tolerate exceptions from NewSimpleBlockFile and so we
208-
// can allow exceptions from ReadData too
207+
// We tolerate exceptions from NewBlockFile
208+
// and so we can allow exceptions from ReadData too
209209
f->ReadData(buffer.ptr(), format, 0, len);
210210
newBlockFile =
211-
dirManager->NewSimpleBlockFile(buffer.ptr(), len, format);
211+
dirManager->NewBlockFile( [&]( wxFileNameWrapper filePath ) {
212+
return make_blockfile<SimpleBlockFile>(
213+
std::move(filePath), buffer.ptr(), len, format);
214+
} );
212215
}
213216

214217
// Update our hash so we know what block files we've done

src/DirManager.cpp

+8-64
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@
8585
#include <sys/stat.h>
8686
#endif
8787

88+
#include "BlockFile.h"
8889
#include "FileNames.h"
89-
#include "blockfile/ODPCMAliasBlockFile.h"
90-
#include "blockfile/ODDecodeBlockFile.h"
9190
#include "InconsistencyException.h"
92-
#include "Project.h"
9391
#include "Prefs.h"
9492
#include "widgets/Warning.h"
9593
#include "widgets/ErrorDialog.h"
@@ -1182,71 +1180,17 @@ wxFileNameWrapper DirManager::MakeBlockFileName()
11821180
return ret;
11831181
}
11841182

1185-
BlockFilePtr DirManager::NewSimpleBlockFile(
1186-
samplePtr sampleData, size_t sampleLen,
1187-
sampleFormat format,
1188-
bool allowDeferredWrite)
1183+
BlockFilePtr DirManager::NewBlockFile( const BlockFileFactory &factory )
11891184
{
11901185
wxFileNameWrapper filePath{ MakeBlockFileName() };
11911186
const wxString fileName{ filePath.GetName() };
1192-
1193-
auto newBlockFile = make_blockfile<SimpleBlockFile>
1194-
(std::move(filePath), sampleData, sampleLen, format, allowDeferredWrite);
1195-
1187+
auto newBlockFile = factory( std::move(filePath) );
11961188
mBlockFileHash[fileName] = newBlockFile;
1197-
1198-
return newBlockFile;
1199-
}
1200-
1201-
BlockFilePtr DirManager::NewAliasBlockFile(
1202-
const FilePath &aliasedFile, sampleCount aliasStart,
1203-
size_t aliasLen, int aliasChannel)
1204-
{
1205-
wxFileNameWrapper filePath{ MakeBlockFileName() };
1206-
const wxString fileName = filePath.GetName();
1207-
1208-
auto newBlockFile = make_blockfile<PCMAliasBlockFile>
1209-
(std::move(filePath), wxFileNameWrapper{aliasedFile},
1210-
aliasStart, aliasLen, aliasChannel);
1211-
1212-
mBlockFileHash[fileName]=newBlockFile;
1213-
aliasList.push_back(aliasedFile);
1214-
1215-
return newBlockFile;
1216-
}
1217-
1218-
BlockFilePtr DirManager::NewODAliasBlockFile(
1219-
const FilePath &aliasedFile, sampleCount aliasStart,
1220-
size_t aliasLen, int aliasChannel)
1221-
{
1222-
wxFileNameWrapper filePath{ MakeBlockFileName() };
1223-
const wxString fileName{ filePath.GetName() };
1224-
1225-
auto newBlockFile = make_blockfile<ODPCMAliasBlockFile>
1226-
(std::move(filePath), wxFileNameWrapper{aliasedFile},
1227-
aliasStart, aliasLen, aliasChannel);
1228-
1229-
mBlockFileHash[fileName]=newBlockFile;
1230-
aliasList.push_back(aliasedFile);
1231-
1232-
return newBlockFile;
1233-
}
1234-
1235-
BlockFilePtr DirManager::NewODDecodeBlockFile(
1236-
const FilePath &aliasedFile, sampleCount aliasStart,
1237-
size_t aliasLen, int aliasChannel, int decodeType)
1238-
{
1239-
wxFileNameWrapper filePath{ MakeBlockFileName() };
1240-
const wxString fileName{ filePath.GetName() };
1241-
1242-
auto newBlockFile = make_blockfile<ODDecodeBlockFile>
1243-
(std::move(filePath), wxFileNameWrapper{ aliasedFile },
1244-
aliasStart, aliasLen, aliasChannel, decodeType);
1245-
1246-
mBlockFileHash[fileName]=newBlockFile;
1247-
aliasList.push_back(aliasedFile); //OD TODO: check to see if we need to remove this when done decoding.
1248-
//I don't immediately see a place where aliased files remove when a file is closed.
1249-
1189+
auto &aliasName = newBlockFile->GetExternalFileName();
1190+
if ( aliasName.IsOk() )
1191+
//OD TODO: check to see if we need to remove this when done decoding.
1192+
//I don't immediately see a place where aliased files remove when a file is closed.
1193+
aliasList.push_back( aliasName.GetFullPath() );
12501194
return newBlockFile;
12511195
}
12521196

src/DirManager.h

+2-17
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,8 @@ class PROFILE_DLL_API DirManager final : public XMLTagHandler {
124124

125125
wxLongLong GetFreeDiskSpace();
126126

127-
BlockFilePtr
128-
NewSimpleBlockFile(samplePtr sampleData,
129-
size_t sampleLen,
130-
sampleFormat format,
131-
bool allowDeferredWrite = false);
132-
133-
BlockFilePtr
134-
NewAliasBlockFile( const FilePath &aliasedFile, sampleCount aliasStart,
135-
size_t aliasLen, int aliasChannel);
136-
137-
BlockFilePtr
138-
NewODAliasBlockFile( const FilePath &aliasedFile, sampleCount aliasStart,
139-
size_t aliasLen, int aliasChannel);
140-
141-
BlockFilePtr
142-
NewODDecodeBlockFile( const FilePath &aliasedFile, sampleCount aliasStart,
143-
size_t aliasLen, int aliasChannel, int decodeType);
127+
using BlockFileFactory = std::function< BlockFilePtr( wxFileNameWrapper ) >;
128+
BlockFilePtr NewBlockFile( const BlockFileFactory &factory );
144129

145130
/// Returns true if the blockfile pointed to by b is contained by the DirManager
146131
bool ContainsBlockFile(const BlockFile *b) const;

src/Sequence.cpp

+38-45
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ namespace {
457457
{
458458
return numSamples > wxLL(9223372036854775807);
459459
}
460+
461+
BlockFilePtr NewSimpleBlockFile( DirManager &dm,
462+
samplePtr sampleData, size_t sampleLen,
463+
sampleFormat format,
464+
bool allowDeferredWrite = false)
465+
{
466+
return dm.NewBlockFile( [&]( wxFileNameWrapper filePath ) {
467+
return make_blockfile<SimpleBlockFile>(
468+
std::move(filePath), sampleData, sampleLen, format, allowDeferredWrite);
469+
} );
470+
}
460471
}
461472

462473
void Sequence::Paste(sampleCount s, const Sequence *src)
@@ -549,7 +560,7 @@ void Sequence::Paste(sampleCount s, const Sequence *src)
549560
splitPoint, length - splitPoint, true);
550561

551562
auto file =
552-
mDirManager->NewSimpleBlockFile(
563+
NewSimpleBlockFile( *mDirManager,
553564
// largerBlockLen is not more than mMaxSamples...
554565
buffer.ptr(), largerBlockLen.as_size_t(), mSampleFormat);
555566

@@ -709,41 +720,6 @@ void Sequence::InsertSilence(sampleCount s0, sampleCount len)
709720
Paste(s0, &sTrack);
710721
}
711722

712-
void Sequence::AppendAlias(const FilePath &fullPath,
713-
sampleCount start,
714-
size_t len, int channel, bool useOD)
715-
// STRONG-GUARANTEE
716-
{
717-
// Quick check to make sure that it doesn't overflow
718-
if (Overflows((mNumSamples.as_double()) + ((double)len)))
719-
THROW_INCONSISTENCY_EXCEPTION;
720-
721-
SeqBlock newBlock(
722-
useOD?
723-
mDirManager->NewODAliasBlockFile(fullPath, start, len, channel):
724-
mDirManager->NewAliasBlockFile(fullPath, start, len, channel),
725-
mNumSamples
726-
);
727-
mBlock.push_back(newBlock);
728-
mNumSamples += len;
729-
}
730-
731-
void Sequence::AppendCoded(const FilePath &fName, sampleCount start,
732-
size_t len, int channel, int decodeType)
733-
// STRONG-GUARANTEE
734-
{
735-
// Quick check to make sure that it doesn't overflow
736-
if (Overflows((mNumSamples.as_double()) + ((double)len)))
737-
THROW_INCONSISTENCY_EXCEPTION;
738-
739-
SeqBlock newBlock(
740-
mDirManager->NewODDecodeBlockFile(fName, start, len, channel, decodeType),
741-
mNumSamples
742-
);
743-
mBlock.push_back(newBlock);
744-
mNumSamples += len;
745-
}
746-
747723
void Sequence::AppendBlock
748724
(DirManager &mDirManager,
749725
BlockArray &mBlock, sampleCount &mNumSamples, const SeqBlock &b)
@@ -1267,13 +1243,13 @@ void Sequence::SetSamples(samplePtr buffer, sampleFormat format,
12671243
else
12681244
ClearSamples(scratch.ptr(), mSampleFormat, bstart, blen);
12691245

1270-
block.f = mDirManager->NewSimpleBlockFile(
1246+
block.f = NewSimpleBlockFile( *mDirManager,
12711247
scratch.ptr(), fileLength, mSampleFormat);
12721248
}
12731249
else {
12741250
// Avoid reading the disk when the replacement is total
12751251
if (useBuffer)
1276-
block.f = mDirManager->NewSimpleBlockFile(
1252+
block.f = NewSimpleBlockFile( *mDirManager,
12771253
useBuffer, fileLength, mSampleFormat);
12781254
else
12791255
block.f = make_blockfile<SilentBlockFile>(fileLength);
@@ -1599,7 +1575,7 @@ void Sequence::Append(samplePtr buffer, sampleFormat format,
15991575
const auto newLastBlockLen = length + addLen;
16001576

16011577
SeqBlock newLastBlock(
1602-
mDirManager->NewSimpleBlockFile(
1578+
NewSimpleBlockFile( *mDirManager,
16031579
buffer2.ptr(), newLastBlockLen, mSampleFormat,
16041580
blockFileLog != NULL
16051581
),
@@ -1625,12 +1601,12 @@ void Sequence::Append(samplePtr buffer, sampleFormat format,
16251601
const auto addedLen = std::min(idealSamples, len);
16261602
BlockFilePtr pFile;
16271603
if (format == mSampleFormat) {
1628-
pFile = mDirManager->NewSimpleBlockFile(
1604+
pFile = NewSimpleBlockFile( *mDirManager,
16291605
buffer, addedLen, mSampleFormat, blockFileLog != NULL);
16301606
}
16311607
else {
16321608
CopySamples(buffer, format, buffer2.ptr(), mSampleFormat, addedLen);
1633-
pFile = mDirManager->NewSimpleBlockFile(
1609+
pFile = NewSimpleBlockFile( *mDirManager,
16341610
buffer2.ptr(), addedLen, mSampleFormat, blockFileLog != NULL);
16351611
}
16361612

@@ -1673,7 +1649,7 @@ void Sequence::Blockify
16731649
int newLen = ((i + 1) * len / num) - offset;
16741650
samplePtr bufStart = buffer + (offset * SAMPLE_SIZE(mSampleFormat));
16751651

1676-
b.f = mDirManager.NewSimpleBlockFile(bufStart, newLen, mSampleFormat);
1652+
b.f = NewSimpleBlockFile( mDirManager, bufStart, newLen, mSampleFormat );
16771653

16781654
list.push_back(b);
16791655
}
@@ -1735,7 +1711,7 @@ void Sequence::Delete(sampleCount start, sampleCount len)
17351711
( pos + len ).as_size_t(), newLen - pos, true);
17361712

17371713
auto newFile =
1738-
mDirManager->NewSimpleBlockFile(scratch.ptr(), newLen, mSampleFormat);
1714+
NewSimpleBlockFile( *mDirManager, scratch.ptr(), newLen, mSampleFormat );
17391715

17401716
// Don't make a duplicate array. We can still give STRONG-GUARANTEE
17411717
// if we modify only one block in place.
@@ -1779,7 +1755,7 @@ void Sequence::Delete(sampleCount start, sampleCount len)
17791755
ensureSampleBufferSize(scratch, mSampleFormat, scratchSize, preBufferLen);
17801756
Read(scratch.ptr(), mSampleFormat, preBlock, 0, preBufferLen, true);
17811757
auto pFile =
1782-
mDirManager->NewSimpleBlockFile(scratch.ptr(), preBufferLen, mSampleFormat);
1758+
NewSimpleBlockFile( *mDirManager, scratch.ptr(), preBufferLen, mSampleFormat );
17831759

17841760
newBlock.push_back(SeqBlock(pFile, preBlock.start));
17851761
} else {
@@ -1825,7 +1801,7 @@ void Sequence::Delete(sampleCount start, sampleCount len)
18251801
auto pos = (start + len - postBlock.start).as_size_t();
18261802
Read(scratch.ptr(), mSampleFormat, postBlock, pos, postBufferLen, true);
18271803
auto file =
1828-
mDirManager->NewSimpleBlockFile(scratch.ptr(), postBufferLen, mSampleFormat);
1804+
NewSimpleBlockFile( *mDirManager, scratch.ptr(), postBufferLen, mSampleFormat );
18291805

18301806
newBlock.push_back(SeqBlock(file, start));
18311807
} else {
@@ -2016,6 +1992,23 @@ size_t Sequence::GetMaxDiskBlockSize()
20161992
return sMaxDiskBlockSize;
20171993
}
20181994

1995+
void Sequence::AppendBlockFile( const BlockFileFactory &factory, size_t len )
1996+
// STRONG-GUARANTEE
1997+
{
1998+
// Quick check to make sure that it doesn't overflow
1999+
if (Overflows((mNumSamples.as_double()) + ((double)len)))
2000+
THROW_INCONSISTENCY_EXCEPTION;
2001+
2002+
SeqBlock newBlock(
2003+
mDirManager->NewBlockFile( [&]( wxFileNameWrapper filePath ){
2004+
return factory( std::move( filePath ), len );
2005+
} ),
2006+
mNumSamples
2007+
);
2008+
mBlock.push_back(newBlock);
2009+
mNumSamples += len;
2010+
}
2011+
20192012
void Sequence::AppendBlockFile(const BlockFilePtr &blockFile)
20202013
{
20212014
// We assume blockFile has the correct ref count already

src/Sequence.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BlockFile;
2323
using BlockFilePtr = std::shared_ptr<BlockFile>;
2424

2525
class DirManager;
26+
class wxFileNameWrapper;
2627

2728
// This is an internal data structure! For advanced use only.
2829
class SeqBlock {
@@ -107,12 +108,12 @@ class PROFILE_DLL_API Sequence final : public XMLTagHandler{
107108
void Append(samplePtr buffer, sampleFormat format, size_t len,
108109
XMLWriter* blockFileLog=NULL);
109110
void Delete(sampleCount start, sampleCount len);
110-
void AppendAlias(const FilePath &fullPath,
111-
sampleCount start,
112-
size_t len, int channel, bool useOD);
113111

114-
void AppendCoded(const FilePath &fName, sampleCount start,
115-
size_t len, int channel, int decodeType);
112+
using BlockFileFactory =
113+
std::function< BlockFilePtr( wxFileNameWrapper, size_t /* len */ ) >;
114+
// An overload of AppendBlockFile that passes the factory to DirManager
115+
// which supplies it with a file name
116+
void AppendBlockFile( const BlockFileFactory &factory, size_t len );
116117

117118
///gets an int with OD flags so that we can determine which ODTasks should be run on this track after save/open, etc.
118119
unsigned int GetODFlags();
@@ -121,7 +122,7 @@ class PROFILE_DLL_API Sequence final : public XMLTagHandler{
121122
// sequence. This function is used by the recording log crash recovery
122123
// code, but may be useful for other purposes. The blockfile must already
123124
// be registered within the dir manager hash. This is the case
124-
// when the blockfile is created using DirManager::NewSimpleBlockFile or
125+
// when the blockfile is created using SimpleBlockFile or
125126
// loaded from an XML file via DirManager::HandleXMLTag
126127
void AppendBlockFile(const BlockFilePtr &blockFile);
127128

0 commit comments

Comments
 (0)