1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

Demos: Convey proper ownership semantics

This commit is contained in:
reuk 2020-03-12 11:34:06 +00:00
parent 4abffb1f11
commit 5af01b9b16
20 changed files with 77 additions and 76 deletions

View file

@ -74,11 +74,11 @@ inline File getExamplesDirectory() noexcept
#endif
}
inline InputStream* createAssetInputStream (const char* resourcePath)
inline std::unique_ptr<InputStream> createAssetInputStream (const char* resourcePath)
{
#if JUCE_ANDROID
ZipFile apkZip (File::getSpecialLocation (File::invokedExecutableFile));
return apkZip.createStreamForEntry (apkZip.getIndexOfFileName ("assets/" + String (resourcePath)));
return std::unique_ptr<InputStream> (apkZip.createStreamForEntry (apkZip.getIndexOfFileName ("assets/" + String (resourcePath))));
#else
#if JUCE_IOS
auto assetsDir = File::getSpecialLocation (File::currentExecutableFile)

View file

@ -182,7 +182,7 @@ struct SynthAudioSource : public AudioSource
{
WavAudioFormat wavFormat;
std::unique_ptr<AudioFormatReader> audioReader (wavFormat.createReaderFor (createAssetInputStream ("cello.wav"), true));
std::unique_ptr<AudioFormatReader> audioReader (wavFormat.createReaderFor (createAssetInputStream ("cello.wav").release(), true));
BigInteger allNotes;
allNotes.setRange (0, 128, true);

View file

@ -489,7 +489,7 @@ public:
{
lastSVGLoadTime = Time::getCurrentTime();
ZipFile icons (createAssetInputStream ("icons.zip"), true);
ZipFile icons (createAssetInputStream ("icons.zip").release(), true);
// Load a random SVG file from our embedded icons.zip file.
const std::unique_ptr<InputStream> svgFileStream (icons.createStreamForEntry (Random::getSystemRandom().nextInt (icons.getNumEntries())));

View file

@ -667,7 +667,7 @@ private:
if (iconsFromZipFile.size() == 0)
{
// If we've not already done so, load all the images from the zip file..
ZipFile icons (createAssetInputStream ("icons.zip"), true);
ZipFile icons (createAssetInputStream ("icons.zip").release(), true);
for (int i = 0; i < icons.getNumEntries(); ++i)
{

View file

@ -976,7 +976,7 @@ AudioFormatReader* AiffAudioFormat::createReaderFor (InputStream* sourceStream,
MemoryMappedAudioFormatReader* AiffAudioFormat::createMemoryMappedReader (const File& file)
{
return createMemoryMappedReader (file.createInputStream());
return createMemoryMappedReader (file.createInputStream().release());
}
MemoryMappedAudioFormatReader* AiffAudioFormat::createMemoryMappedReader (FileInputStream* fin)

View file

@ -494,9 +494,9 @@ StringArray OggVorbisAudioFormat::getQualityOptions()
int OggVorbisAudioFormat::estimateOggFileQuality (const File& source)
{
if (auto* in = source.createInputStream())
if (auto in = source.createInputStream())
{
if (auto r = std::unique_ptr<AudioFormatReader> (createReaderFor (in, true)))
if (auto r = std::unique_ptr<AudioFormatReader> (createReaderFor (in.release(), true)))
{
auto lengthSecs = r->lengthInSamples / r->sampleRate;
auto approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs);

View file

@ -1703,7 +1703,7 @@ AudioFormatReader* WavAudioFormat::createReaderFor (InputStream* sourceStream, b
MemoryMappedAudioFormatReader* WavAudioFormat::createMemoryMappedReader (const File& file)
{
return createMemoryMappedReader (file.createInputStream());
return createMemoryMappedReader (file.createInputStream().release());
}
MemoryMappedAudioFormatReader* WavAudioFormat::createMemoryMappedReader (FileInputStream* fin)
@ -1748,7 +1748,7 @@ namespace WavFileHelpers
TemporaryFile tempFile (file);
WavAudioFormat wav;
std::unique_ptr<AudioFormatReader> reader (wav.createReaderFor (file.createInputStream(), true));
std::unique_ptr<AudioFormatReader> reader (wav.createReaderFor (file.createInputStream().release(), true));
if (reader != nullptr)
{
@ -1781,7 +1781,7 @@ bool WavAudioFormat::replaceMetadataInFile (const File& wavFile, const StringPai
{
using namespace WavFileHelpers;
std::unique_ptr<WavAudioFormatReader> reader (static_cast<WavAudioFormatReader*> (createReaderFor (wavFile.createInputStream(), true)));
std::unique_ptr<WavAudioFormatReader> reader (static_cast<WavAudioFormatReader*> (createReaderFor (wavFile.createInputStream().release(), true)));
if (reader != nullptr)
{

View file

@ -126,14 +126,14 @@ AudioFormatReader* AudioFormatManager::createReaderFor (const File& file)
for (auto* af : knownFormats)
if (af->canHandleFile (file))
if (auto* in = file.createInputStream())
if (auto* r = af->createReaderFor (in, true))
if (auto in = file.createInputStream())
if (auto* r = af->createReaderFor (in.release(), true))
return r;
return nullptr;
}
AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileStream)
AudioFormatReader* AudioFormatManager::createReaderFor (std::unique_ptr<InputStream> audioFileStream)
{
// you need to actually register some formats before the manager can
// use them to open a file!
@ -141,22 +141,21 @@ AudioFormatReader* AudioFormatManager::createReaderFor (InputStream* audioFileSt
if (audioFileStream != nullptr)
{
std::unique_ptr<InputStream> in (audioFileStream);
auto originalStreamPos = in->getPosition();
auto originalStreamPos = audioFileStream->getPosition();
for (auto* af : knownFormats)
{
if (auto* r = af->createReaderFor (in.get(), false))
if (auto* r = af->createReaderFor (audioFileStream.get(), false))
{
in.release();
audioFileStream.release();
return r;
}
in->setPosition (originalStreamPos);
audioFileStream->setPosition (originalStreamPos);
// the stream that is passed-in must be capable of being repositioned so
// that all the formats can have a go at opening it.
jassert (in->getPosition() == originalStreamPos);
jassert (audioFileStream->getPosition() == originalStreamPos);
}
}

View file

@ -138,7 +138,7 @@ public:
If none of the registered formats can open the stream, it'll return nullptr.
If it returns a reader, it's the caller's responsibility to delete the reader.
*/
AudioFormatReader* createReaderFor (InputStream* audioFileStream);
AudioFormatReader* createReaderFor (std::unique_ptr<InputStream> audioFileStream);
private:
//==============================================================================

View file

@ -220,7 +220,7 @@ private:
{
if (reader == nullptr && source != nullptr)
if (auto* audioFileStream = source->createInputStream())
reader.reset (owner.formatManagerToUse.createReaderFor (audioFileStream));
reader.reset (owner.formatManagerToUse.createReaderFor (std::unique_ptr<InputStream> (audioFileStream)));
}
bool readNextBlock()

View file

@ -174,8 +174,8 @@ void SoundPlayer::play (const void* resourceData, size_t resourceSize)
{
if (resourceData != nullptr && resourceSize > 0)
{
MemoryInputStream* mem = new MemoryInputStream (resourceData, resourceSize, false);
play (formatManager.createReaderFor (mem), true);
auto mem = std::make_unique<MemoryInputStream> (resourceData, resourceSize, false);
play (formatManager.createReaderFor (std::move (mem)), true);
}
}

View file

@ -726,22 +726,24 @@ bool File::startAsProcess (const String& parameters) const
}
//==============================================================================
FileInputStream* File::createInputStream() const
std::unique_ptr<FileInputStream> File::createInputStream() const
{
std::unique_ptr<FileInputStream> fin (new FileInputStream (*this));
auto fin = std::make_unique<FileInputStream> (*this);
if (fin->openedOk())
return fin.release();
return fin;
return nullptr;
}
FileOutputStream* File::createOutputStream (size_t bufferSize) const
std::unique_ptr<FileOutputStream> File::createOutputStream (size_t bufferSize) const
{
std::unique_ptr<FileOutputStream> out (new FileOutputStream (*this, bufferSize));
auto fout = std::make_unique<FileOutputStream> (*this, bufferSize);
return out->failedToOpen() ? nullptr
: out.release();
if (fout->openedOk())
return fout;
return nullptr;
}
//==============================================================================
@ -753,8 +755,8 @@ bool File::appendData (const void* const dataToAppend,
if (numberOfBytes == 0)
return true;
FileOutputStream out (*this, 8192);
return out.openedOk() && out.write (dataToAppend, numberOfBytes);
FileOutputStream fout (*this, 8192);
return fout.openedOk() && fout.write (dataToAppend, numberOfBytes);
}
bool File::replaceWithData (const void* const dataToWrite,
@ -770,12 +772,12 @@ bool File::replaceWithData (const void* const dataToWrite,
bool File::appendText (const String& text, bool asUnicode, bool writeHeaderBytes, const char* lineFeed) const
{
FileOutputStream out (*this);
FileOutputStream fout (*this);
if (out.failedToOpen())
if (fout.failedToOpen())
return false;
return out.writeText (text, asUnicode, writeHeaderBytes, lineFeed);
return fout.writeText (text, asUnicode, writeHeaderBytes, lineFeed);
}
bool File::replaceWithText (const String& textToWrite, bool asUnicode, bool writeHeaderBytes, const char* lineFeed) const

View file

@ -622,7 +622,7 @@ public:
start of the file), or nullptr if the file can't be opened for some reason
@see createOutputStream, loadFileAsData
*/
FileInputStream* createInputStream() const;
std::unique_ptr<FileInputStream> createInputStream() const;
/** Creates a stream to write to this file.
@ -655,7 +655,7 @@ public:
end of the file), or nullptr if the file can't be opened for some reason
@see createInputStream, appendData, appendText
*/
FileOutputStream* createOutputStream (size_t bufferSize = 0x8000) const;
std::unique_ptr<FileOutputStream> createOutputStream (size_t bufferSize = 0x8000) const;
//==============================================================================
/** Loads a file's contents into memory as a block of binary data.

View file

@ -655,21 +655,21 @@ private:
#endif
//==============================================================================
InputStream* URL::createInputStream (bool usePostCommand,
OpenStreamProgressCallback* progressCallback,
void* progressCallbackContext,
String headers,
int timeOutMs,
StringPairArray* responseHeaders,
int* statusCode,
int numRedirectsToFollow,
String httpRequestCmd) const
std::unique_ptr<InputStream> URL::createInputStream (bool usePostCommand,
OpenStreamProgressCallback* progressCallback,
void* progressCallbackContext,
String headers,
int timeOutMs,
StringPairArray* responseHeaders,
int* statusCode,
int numRedirectsToFollow,
String httpRequestCmd) const
{
if (isLocalFile())
{
#if JUCE_IOS
// We may need to refresh the embedded bookmark.
return new iOSFileStreamWrapper<FileInputStream> (const_cast<URL&>(*this));
return std::make_unique<iOSFileStreamWrapper<FileInputStream>> (const_cast<URL&>(*this));
#else
return getLocalFile().createInputStream();
#endif
@ -717,27 +717,29 @@ InputStream* URL::createInputStream (bool usePostCommand,
if (! success || wi->isError())
return nullptr;
return wi.release();
// Older GCCs complain about binding unique_ptr<WebInputStream>&& to unique_ptr<InputStream>
// if we just `return wi` here.
return std::unique_ptr<InputStream> (std::move (wi));
}
#if JUCE_ANDROID
OutputStream* juce_CreateContentURIOutputStream (const URL&);
#endif
OutputStream* URL::createOutputStream() const
std::unique_ptr<OutputStream> URL::createOutputStream() const
{
if (isLocalFile())
{
#if JUCE_IOS
// We may need to refresh the embedded bookmark.
return new iOSFileStreamWrapper<FileOutputStream> (const_cast<URL&> (*this));
return std::make_unique<iOSFileStreamWrapper<FileOutputStream>> (const_cast<URL&> (*this));
#else
return new FileOutputStream (getLocalFile());
return std::make_unique<FileOutputStream> (getLocalFile());
#endif
}
#if JUCE_ANDROID
return juce_CreateContentURIOutputStream (*this);
return std::unique_ptr<OutputStream> (juce_CreateContentURIOutputStream (*this));
#else
return nullptr;
#endif

View file

@ -340,22 +340,22 @@ public:
@returns an input stream that the caller must delete, or a null pointer if there was an
error trying to open it.
*/
InputStream* createInputStream (bool doPostLikeRequest,
OpenStreamProgressCallback* progressCallback = nullptr,
void* progressCallbackContext = nullptr,
String extraHeaders = {},
int connectionTimeOutMs = 0,
StringPairArray* responseHeaders = nullptr,
int* statusCode = nullptr,
int numRedirectsToFollow = 5,
String httpRequestCmd = {}) const;
std::unique_ptr<InputStream> createInputStream (bool doPostLikeRequest,
OpenStreamProgressCallback* progressCallback = nullptr,
void* progressCallbackContext = nullptr,
String extraHeaders = {},
int connectionTimeOutMs = 0,
StringPairArray* responseHeaders = nullptr,
int* statusCode = nullptr,
int numRedirectsToFollow = 5,
String httpRequestCmd = {}) const;
/** Attempts to open an output stream to a URL for writing
This method can only be used for certain scheme types such as local files
and content:// URIs on Android.
*/
OutputStream* createOutputStream() const;
std::unique_ptr<OutputStream> createOutputStream() const;
//==============================================================================
/** Represents a download task.

View file

@ -34,12 +34,12 @@ FileInputSource::~FileInputSource()
InputStream* FileInputSource::createInputStream()
{
return file.createInputStream();
return file.createInputStream().release();
}
InputStream* FileInputSource::createInputStreamFor (const String& relatedItemPath)
{
return file.getSiblingFile (relatedItemPath).createInputStream();
return file.getSiblingFile (relatedItemPath).createInputStream().release();
}
int64 FileInputSource::hashCode() const

View file

@ -39,7 +39,7 @@ URLInputSource::~URLInputSource()
InputStream* URLInputSource::createInputStream()
{
return u.createInputStream (false);
return u.createInputStream (false).release();
}
InputStream* URLInputSource::createInputStreamFor (const String& relatedItemPath)
@ -48,7 +48,7 @@ InputStream* URLInputSource::createInputStreamFor (const String& relatedItemPath
auto parent = sub.containsChar (L'/') ? sub.upToLastOccurrenceOf ("/", false, false)
: String ();
return u.withNewSubPath (parent).getChildURL (relatedItemPath).createInputStream (false);
return u.withNewSubPath (parent).getChildURL (relatedItemPath).createInputStream (false).release();
}
int64 URLInputSource::hashCode() const

View file

@ -541,7 +541,7 @@ private:
{
if (stream == nullptr)
{
stream.reset (file.createInputStream());
stream = file.createInputStream();
if (stream == nullptr)
return false;

View file

@ -95,7 +95,7 @@ struct ConvolutionEngine
numInputSegments = (blockSize > 128 ? numSegments : 3 * numSegments);
FFTobject.reset (new FFT (roundToInt (std::log2 (FFTSize))));
FFTobject = std::make_unique<FFT> (roundToInt (std::log2 (FFTSize)));
bufferInput.setSize (1, static_cast<int> (FFTSize));
bufferOutput.setSize (1, static_cast<int> (FFTSize * 2));
@ -803,13 +803,11 @@ private:
{
if (currentInfo.sourceType == SourceType::sourceBinaryData)
{
if (! (copyAudioStreamInAudioBuffer (new MemoryInputStream (currentInfo.sourceData, (size_t) currentInfo.sourceDataSize, false))))
return;
copyAudioStreamInAudioBuffer (std::make_unique<MemoryInputStream> (currentInfo.sourceData, (size_t) currentInfo.sourceDataSize, false));
}
else if (currentInfo.sourceType == SourceType::sourceAudioFile)
{
if (! (copyAudioStreamInAudioBuffer (new FileInputStream (currentInfo.fileImpulseResponse))))
return;
copyAudioStreamInAudioBuffer (std::make_unique<FileInputStream> (currentInfo.fileImpulseResponse));
}
else if (currentInfo.sourceType == SourceType::sourceAudioBuffer)
{
@ -847,11 +845,11 @@ private:
/** Converts the data from an audio file into a stereo audio buffer of floats, and
performs resampling if necessary.
*/
bool copyAudioStreamInAudioBuffer (InputStream* stream)
bool copyAudioStreamInAudioBuffer (std::unique_ptr<InputStream> stream)
{
AudioFormatManager manager;
manager.registerBasicFormats();
std::unique_ptr<AudioFormatReader> formatReader (manager.createReaderFor (stream));
std::unique_ptr<AudioFormatReader> formatReader (manager.createReaderFor (std::move (stream)));
if (formatReader != nullptr)
{

View file

@ -1197,7 +1197,7 @@ private:
auto linkedFile = originalFile.getParentDirectory().getChildFile (link);
if (linkedFile.existsAsFile())
inputStream.reset (linkedFile.createInputStream());
inputStream = linkedFile.createInputStream();
}
if (inputStream != nullptr)