diff --git a/examples/Assets/DemoUtilities.h b/examples/Assets/DemoUtilities.h index 0893cfc0d7..829a883a87 100644 --- a/examples/Assets/DemoUtilities.h +++ b/examples/Assets/DemoUtilities.h @@ -74,11 +74,11 @@ inline File getExamplesDirectory() noexcept #endif } -inline InputStream* createAssetInputStream (const char* resourcePath) +inline std::unique_ptr 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 (apkZip.createStreamForEntry (apkZip.getIndexOfFileName ("assets/" + String (resourcePath)))); #else #if JUCE_IOS auto assetsDir = File::getSpecialLocation (File::currentExecutableFile) diff --git a/examples/Audio/AudioSynthesiserDemo.h b/examples/Audio/AudioSynthesiserDemo.h index 644f061f83..8acdcf705e 100644 --- a/examples/Audio/AudioSynthesiserDemo.h +++ b/examples/Audio/AudioSynthesiserDemo.h @@ -182,7 +182,7 @@ struct SynthAudioSource : public AudioSource { WavAudioFormat wavFormat; - std::unique_ptr audioReader (wavFormat.createReaderFor (createAssetInputStream ("cello.wav"), true)); + std::unique_ptr audioReader (wavFormat.createReaderFor (createAssetInputStream ("cello.wav").release(), true)); BigInteger allNotes; allNotes.setRange (0, 128, true); diff --git a/examples/GUI/GraphicsDemo.h b/examples/GUI/GraphicsDemo.h index d5fecd464f..a870a4f261 100644 --- a/examples/GUI/GraphicsDemo.h +++ b/examples/GUI/GraphicsDemo.h @@ -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 svgFileStream (icons.createStreamForEntry (Random::getSystemRandom().nextInt (icons.getNumEntries()))); diff --git a/examples/GUI/WidgetsDemo.h b/examples/GUI/WidgetsDemo.h index 9ee29bccb9..8a99e440cd 100644 --- a/examples/GUI/WidgetsDemo.h +++ b/examples/GUI/WidgetsDemo.h @@ -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) { diff --git a/modules/juce_audio_formats/codecs/juce_AiffAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_AiffAudioFormat.cpp index a1264aa22c..7d750c9b7f 100644 --- a/modules/juce_audio_formats/codecs/juce_AiffAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_AiffAudioFormat.cpp @@ -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) diff --git a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp index ebf1c012c2..f824012e74 100644 --- a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp @@ -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 (createReaderFor (in, true))) + if (auto r = std::unique_ptr (createReaderFor (in.release(), true))) { auto lengthSecs = r->lengthInSamples / r->sampleRate; auto approxBitsPerSecond = (int) (source.getSize() * 8 / lengthSecs); diff --git a/modules/juce_audio_formats/codecs/juce_WavAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_WavAudioFormat.cpp index d49315ccbb..dd03c70d9d 100644 --- a/modules/juce_audio_formats/codecs/juce_WavAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_WavAudioFormat.cpp @@ -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 reader (wav.createReaderFor (file.createInputStream(), true)); + std::unique_ptr 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 reader (static_cast (createReaderFor (wavFile.createInputStream(), true))); + std::unique_ptr reader (static_cast (createReaderFor (wavFile.createInputStream().release(), true))); if (reader != nullptr) { diff --git a/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp b/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp index 768271995a..17e3e09be7 100644 --- a/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp +++ b/modules/juce_audio_formats/format/juce_AudioFormatManager.cpp @@ -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 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 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); } } diff --git a/modules/juce_audio_formats/format/juce_AudioFormatManager.h b/modules/juce_audio_formats/format/juce_AudioFormatManager.h index 36140e839f..bbc5a4bfdd 100644 --- a/modules/juce_audio_formats/format/juce_AudioFormatManager.h +++ b/modules/juce_audio_formats/format/juce_AudioFormatManager.h @@ -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 audioFileStream); private: //============================================================================== diff --git a/modules/juce_audio_utils/gui/juce_AudioThumbnail.cpp b/modules/juce_audio_utils/gui/juce_AudioThumbnail.cpp index 6d289d114e..ccec9c1bf9 100644 --- a/modules/juce_audio_utils/gui/juce_AudioThumbnail.cpp +++ b/modules/juce_audio_utils/gui/juce_AudioThumbnail.cpp @@ -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 (audioFileStream))); } bool readNextBlock() diff --git a/modules/juce_audio_utils/players/juce_SoundPlayer.cpp b/modules/juce_audio_utils/players/juce_SoundPlayer.cpp index 9df3ff3b19..6b5c8c0c87 100644 --- a/modules/juce_audio_utils/players/juce_SoundPlayer.cpp +++ b/modules/juce_audio_utils/players/juce_SoundPlayer.cpp @@ -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 (resourceData, resourceSize, false); + play (formatManager.createReaderFor (std::move (mem)), true); } } diff --git a/modules/juce_core/files/juce_File.cpp b/modules/juce_core/files/juce_File.cpp index 7041ecd886..a7eab140fc 100644 --- a/modules/juce_core/files/juce_File.cpp +++ b/modules/juce_core/files/juce_File.cpp @@ -726,22 +726,24 @@ bool File::startAsProcess (const String& parameters) const } //============================================================================== -FileInputStream* File::createInputStream() const +std::unique_ptr File::createInputStream() const { - std::unique_ptr fin (new FileInputStream (*this)); + auto fin = std::make_unique (*this); if (fin->openedOk()) - return fin.release(); + return fin; return nullptr; } -FileOutputStream* File::createOutputStream (size_t bufferSize) const +std::unique_ptr File::createOutputStream (size_t bufferSize) const { - std::unique_ptr out (new FileOutputStream (*this, bufferSize)); + auto fout = std::make_unique (*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 diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index e52b138a75..6a09f7d8e6 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -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 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 createOutputStream (size_t bufferSize = 0x8000) const; //============================================================================== /** Loads a file's contents into memory as a block of binary data. diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index 56f2a3cecb..7b532a7a48 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -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 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 (const_cast(*this)); + return std::make_unique> (const_cast(*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&& to unique_ptr + // if we just `return wi` here. + return std::unique_ptr (std::move (wi)); } #if JUCE_ANDROID OutputStream* juce_CreateContentURIOutputStream (const URL&); #endif -OutputStream* URL::createOutputStream() const +std::unique_ptr URL::createOutputStream() const { if (isLocalFile()) { #if JUCE_IOS // We may need to refresh the embedded bookmark. - return new iOSFileStreamWrapper (const_cast (*this)); + return std::make_unique> (const_cast (*this)); #else - return new FileOutputStream (getLocalFile()); + return std::make_unique (getLocalFile()); #endif } #if JUCE_ANDROID - return juce_CreateContentURIOutputStream (*this); + return std::unique_ptr (juce_CreateContentURIOutputStream (*this)); #else return nullptr; #endif diff --git a/modules/juce_core/network/juce_URL.h b/modules/juce_core/network/juce_URL.h index 6e60882222..38b4621985 100644 --- a/modules/juce_core/network/juce_URL.h +++ b/modules/juce_core/network/juce_URL.h @@ -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 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 createOutputStream() const; //============================================================================== /** Represents a download task. diff --git a/modules/juce_core/streams/juce_FileInputSource.cpp b/modules/juce_core/streams/juce_FileInputSource.cpp index feb4081a8d..ed58508afb 100644 --- a/modules/juce_core/streams/juce_FileInputSource.cpp +++ b/modules/juce_core/streams/juce_FileInputSource.cpp @@ -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 diff --git a/modules/juce_core/streams/juce_URLInputSource.cpp b/modules/juce_core/streams/juce_URLInputSource.cpp index 0ecddbb9f4..e4b5105c9c 100644 --- a/modules/juce_core/streams/juce_URLInputSource.cpp +++ b/modules/juce_core/streams/juce_URLInputSource.cpp @@ -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 diff --git a/modules/juce_core/zip/juce_ZipFile.cpp b/modules/juce_core/zip/juce_ZipFile.cpp index 4c4922553b..37692d0d44 100644 --- a/modules/juce_core/zip/juce_ZipFile.cpp +++ b/modules/juce_core/zip/juce_ZipFile.cpp @@ -541,7 +541,7 @@ private: { if (stream == nullptr) { - stream.reset (file.createInputStream()); + stream = file.createInputStream(); if (stream == nullptr) return false; diff --git a/modules/juce_dsp/frequency/juce_Convolution.cpp b/modules/juce_dsp/frequency/juce_Convolution.cpp index 95e051088c..fb2a721367 100644 --- a/modules/juce_dsp/frequency/juce_Convolution.cpp +++ b/modules/juce_dsp/frequency/juce_Convolution.cpp @@ -95,7 +95,7 @@ struct ConvolutionEngine numInputSegments = (blockSize > 128 ? numSegments : 3 * numSegments); - FFTobject.reset (new FFT (roundToInt (std::log2 (FFTSize)))); + FFTobject = std::make_unique (roundToInt (std::log2 (FFTSize))); bufferInput.setSize (1, static_cast (FFTSize)); bufferOutput.setSize (1, static_cast (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 (currentInfo.sourceData, (size_t) currentInfo.sourceDataSize, false)); } else if (currentInfo.sourceType == SourceType::sourceAudioFile) { - if (! (copyAudioStreamInAudioBuffer (new FileInputStream (currentInfo.fileImpulseResponse)))) - return; + copyAudioStreamInAudioBuffer (std::make_unique (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 stream) { AudioFormatManager manager; manager.registerBasicFormats(); - std::unique_ptr formatReader (manager.createReaderFor (stream)); + std::unique_ptr formatReader (manager.createReaderFor (std::move (stream))); if (formatReader != nullptr) { diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index d425a89308..2d003e3565 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -1197,7 +1197,7 @@ private: auto linkedFile = originalFile.getParentDirectory().getChildFile (link); if (linkedFile.existsAsFile()) - inputStream.reset (linkedFile.createInputStream()); + inputStream = linkedFile.createInputStream(); } if (inputStream != nullptr)