audacia/src/SampleBlock.cpp
Paul Licameli a3fcd611b5
Dependency cleanup (#627)
* DBConnection doesn't use ProjectFileIO or need friendship...

... Instead, it is given its own weak_ptr to the project

* Demote the bypass flag into DBConnection...

... So SqliteSampleBlock needs ProjectFileIO only to get the DBConnection

* Accessor functions for the connection objects for SqliteSampleBlock

* Another level of indirection to get to the DBConnection object...

... The document holds the unique_ptr to DBConnection in an attached object;
later we want the SqliteSampleBlockFactory to locate the same pointer without
using ProjectFileIO

* SqliteSampleBlock and its factory don't use class ProjectFileIO...

... Instead they share a pointer to the pointer to the current DBConnection.

This means they no longer know how to invoke the lazy opening of that
connection.

So just require that this be done before any operations on blocks happen.  If
it hasn't, throw and let the application recover.

* ProjectFileIO no longer needs weak_ptr to Project for safety...

... so eliminate much ugliness from 127696879d

* Move DBConnection to new files...

... And SqliteSampleBlock does not depend on ProjectFileIO.

* SampleBlock.h doesn't need ClientData.h

* Function ProjectFileIO::Conn() isn't needed
2020-07-23 01:04:46 -05:00

116 lines
2.6 KiB
C++

/**********************************************************************
Audacity: A Digital Audio Editor
SampleBlock.cpp
**********************************************************************/
#include "Audacity.h"
#include "InconsistencyException.h"
#include "SampleBlock.h"
#include "SampleFormat.h"
#include <wx/defs.h>
static SampleBlockFactoryFactory& installedFactory()
{
static SampleBlockFactoryFactory theFactory;
return theFactory;
}
SampleBlockFactoryFactory SampleBlockFactory::RegisterFactoryFactory(
SampleBlockFactoryFactory newFactory )
{
auto &theFactory = installedFactory();
auto result = std::move( theFactory );
theFactory = std::move( newFactory );
return result;
}
SampleBlockFactoryPtr SampleBlockFactory::New( AudacityProject &project )
{
auto &factory = installedFactory();
if ( ! factory )
THROW_INCONSISTENCY_EXCEPTION;
return factory( project );
}
SampleBlockFactory::~SampleBlockFactory() = default;
SampleBlockPtr SampleBlockFactory::Get(SampleBlockID sbid)
{
auto result = DoGet(sbid);
if (!result)
THROW_INCONSISTENCY_EXCEPTION;
return result;
}
SampleBlockPtr SampleBlockFactory::Create(samplePtr src,
size_t numsamples,
sampleFormat srcformat)
{
auto result = DoCreate(src, numsamples, srcformat);
if (!result)
THROW_INCONSISTENCY_EXCEPTION;
return result;
}
SampleBlockPtr SampleBlockFactory::CreateSilent(
size_t numsamples,
sampleFormat srcformat)
{
auto result = DoCreateSilent(numsamples, srcformat);
if (!result)
THROW_INCONSISTENCY_EXCEPTION;
return result;
}
SampleBlockPtr SampleBlockFactory::CreateFromXML(
sampleFormat srcformat,
const wxChar **attrs)
{
auto result = DoCreateFromXML(srcformat, attrs);
if (!result)
THROW_INCONSISTENCY_EXCEPTION;
return result;
}
SampleBlock::~SampleBlock() = default;
size_t SampleBlock::GetSamples(samplePtr dest,
sampleFormat destformat,
size_t sampleoffset,
size_t numsamples, bool mayThrow)
{
try{ return DoGetSamples(dest, destformat, sampleoffset, numsamples); }
catch( ... ) {
if( mayThrow )
throw;
ClearSamples( dest, destformat, 0, numsamples );
return 0;
}
}
MinMaxRMS SampleBlock::GetMinMaxRMS(
size_t start, size_t len, bool mayThrow)
{
try{ return DoGetMinMaxRMS(start, len); }
catch( ... ) {
if( mayThrow )
throw;
return {};
}
}
MinMaxRMS SampleBlock::GetMinMaxRMS(bool mayThrow) const
{
try{ return DoGetMinMaxRMS(); }
catch( ... ) {
if( mayThrow )
throw;
return {};
}
}