From 48375432be49d1c605d8d9db7f7cf574f9ee84a0 Mon Sep 17 00:00:00 2001 From: Anthony Nicholls Date: Fri, 13 Oct 2023 17:23:08 +0100 Subject: [PATCH] TemporaryFile: Make single argument constructors explicit --- .../juce_core/files/juce_TemporaryFile.cpp | 6 ++++ modules/juce_core/files/juce_TemporaryFile.h | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/modules/juce_core/files/juce_TemporaryFile.cpp b/modules/juce_core/files/juce_TemporaryFile.cpp index 6614054b62..91a049661b 100644 --- a/modules/juce_core/files/juce_TemporaryFile.cpp +++ b/modules/juce_core/files/juce_TemporaryFile.cpp @@ -44,6 +44,12 @@ static File createTempFile (const File& parentDirectory, String name, return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0); } +TemporaryFile::TemporaryFile() : TemporaryFile { String{} } {} + +TemporaryFile::TemporaryFile (const String& suffix) : TemporaryFile { suffix, 0 } {} + +TemporaryFile::TemporaryFile (const File& target) : TemporaryFile { target, 0 } {} + TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags) : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory), "temp_" + String::toHexString (Random::getSystemRandom().nextInt()), diff --git a/modules/juce_core/files/juce_TemporaryFile.h b/modules/juce_core/files/juce_TemporaryFile.h index 33377f7c12..dffe03dbd7 100644 --- a/modules/juce_core/files/juce_TemporaryFile.h +++ b/modules/juce_core/files/juce_TemporaryFile.h @@ -87,6 +87,16 @@ public: }; //============================================================================== + /** Creates a randomly-named temporary file in the default temp directory. + */ + TemporaryFile(); + + /** Creates a randomly-named temporary file in the default temp directory. + + @param suffix a file suffix to use for the file + */ + explicit TemporaryFile (const String& suffix); + /** Creates a randomly-named temporary file in the default temp directory. @param suffix a file suffix to use for the file @@ -94,8 +104,24 @@ public: The file will not be created until you write to it. And remember that when this object is deleted, the file will also be deleted! */ - TemporaryFile (const String& suffix = String(), - int optionFlags = 0); + TemporaryFile (const String& suffix, + int optionFlags); + + /** Creates a temporary file in the same directory as a specified file. + + This is useful if you have a file that you want to overwrite, but don't + want to harm the original file if the write operation fails. You can + use this to create a temporary file next to the target file, then + write to the temporary file, and finally use overwriteTargetFileWithTemporary() + to replace the target file with the one you've just written. + + This class won't create any files until you actually write to them. And remember + that when this object is deleted, the temporary file will also be deleted! + + @param targetFile the file that you intend to overwrite - the temporary + file will be created in the same directory as this + */ + explicit TemporaryFile (const File& targetFile); /** Creates a temporary file in the same directory as a specified file. @@ -113,7 +139,7 @@ public: @param optionFlags a combination of the values listed in the OptionFlags enum */ TemporaryFile (const File& targetFile, - int optionFlags = 0); + int optionFlags); /** Creates a temporary file using an explicit filename. The other constructors are a better choice than this one, unless for some reason