Skip to content

Commit

Permalink
QTemporaryFile: Add support for std::filesystem::path
Browse files Browse the repository at this point in the history
Since it hides QFile's overloads this was not supported for
QTemporaryFile.

[ChangeLog][QtCore][QTemporaryFile] Added support for passing
std::filesystem::path to rename and createNativeFile.

Change-Id: I909ff1d5b9c586824c9901d7dad278dfad09ffc3
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
Morten242 committed Oct 9, 2023
1 parent 7d663d2 commit 0fa4af0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/corelib/io/qtemporaryfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ QTemporaryFile::QTemporaryFile()
{
}

/*!
\fn QTemporaryFile::QTemporaryFile(const std::filesystem::path &templateName, QObject *parent)
\overload
\since 6.7
*/

/*!
Constructs a QTemporaryFile with a template filename of \a
templateName. Upon opening the temporary file this will be used to create
Expand Down Expand Up @@ -791,6 +797,12 @@ QString QTemporaryFile::fileTemplate() const
return d->templateName;
}

/*!
\fn void QTemporaryFile::setFileTemplate(const std::filesystem::path &name)
\overload
\since 6.7
*/

/*!
Sets the static portion of the file name to \a name. If the file
template contains XXXXXX that will automatically be replaced with
Expand All @@ -812,6 +824,12 @@ void QTemporaryFile::setFileTemplate(const QString &name)
d->templateName = name;
}

/*!
\fn bool QTemporaryFile::rename(const std::filesystem::path &newName)
\overload
\since 6.7
*/

/*!
Renames the current temporary file to \a newName and returns true if it
succeeded.
Expand Down Expand Up @@ -860,7 +878,11 @@ bool QTemporaryFile::rename(const QString &newName)
Works on the given \a fileName rather than an existing QFile
object.
*/

/*!
\fn QTemporaryFile *QTemporaryFile::createNativeFile(const std::filesystem::path &fileName)
\overload
\since 6.7
*/

/*!
If \a file is not already a native file, then a QTemporaryFile is created
Expand Down
35 changes: 34 additions & 1 deletion src/corelib/io/qtemporaryfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ class Q_CORE_EXPORT QTemporaryFile : public QFile
#ifndef QT_NO_QOBJECT
explicit QTemporaryFile(QObject *parent);
QTemporaryFile(const QString &templateName, QObject *parent);
#endif

# if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
Q_WEAK_OVERLOAD
explicit QTemporaryFile(const std::filesystem::path &templateName, QObject *parent = nullptr)
: QTemporaryFile(QtPrivate::fromFilesystemPath(templateName), parent)
{
}
# endif // QT_CONFIG(cxx17_filesystem)
#endif // !QT_NO_QOBJECT

~QTemporaryFile();

bool autoRemove() const;
Expand All @@ -44,14 +53,38 @@ class Q_CORE_EXPORT QTemporaryFile : public QFile
QString fileName() const override;
QString fileTemplate() const;
void setFileTemplate(const QString &name);
#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
Q_WEAK_OVERLOAD
void setFileTemplate(const std::filesystem::path &name)
{
return setFileTemplate(QtPrivate::fromFilesystemPath(name));
}
#endif // QT_CONFIG(cxx17_filesystem)

// Hides QFile::rename
bool rename(const QString &newName);

#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
Q_WEAK_OVERLOAD
bool rename(const std::filesystem::path &newName)
{
return rename(QtPrivate::fromFilesystemPath(newName));
}
#endif // QT_CONFIG(cxx17_filesystem)

inline static QTemporaryFile *createNativeFile(const QString &fileName)
{ QFile file(fileName); return createNativeFile(file); }
static QTemporaryFile *createNativeFile(QFile &file);

#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
Q_WEAK_OVERLOAD
static QTemporaryFile *createNativeFile(const std::filesystem::path &fileName)
{
QFile file(fileName);
return createNativeFile(file);
}
#endif // QT_CONFIG(cxx17_filesystem)

protected:
bool open(OpenMode flags) override;

Expand Down
45 changes: 45 additions & 0 deletions tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private slots:
void QTBUG_4796_data();
void QTBUG_4796();
void guaranteeUnique();
void stdfilesystem();
private:
QTemporaryDir m_temporaryDir;
QString m_previousCurrent;
Expand Down Expand Up @@ -1036,5 +1037,49 @@ void tst_QTemporaryFile::guaranteeUnique()
QVERIFY(dir.rmdir(takenFileName));
}

void tst_QTemporaryFile::stdfilesystem()
{
#if !QT_CONFIG(cxx17_filesystem)
QSKIP("std::filesystem not available");
#else
// ctor
{
std::filesystem::path testFile("testFile1.XXXXXX");
QTemporaryFile file(testFile);
QCOMPARE(file.fileTemplate(), QtPrivate::fromFilesystemPath(testFile));
}
// rename
{
QTemporaryFile file("testFile1.XXXXXX");
QVERIFY(file.open());
QByteArray payload = "abc123 I am a string";
file.write(payload);
QVERIFY(file.rename(std::filesystem::path("./test")));
file.close();

QFile f(u"./test"_s);
QVERIFY(f.exists());
QVERIFY(f.open(QFile::ReadOnly));
QCOMPARE(f.readAll(), payload);
}
// createNativeFile
{
std::filesystem::path resource(":/resources/test.txt");
std::unique_ptr<QTemporaryFile> tmp(QTemporaryFile::createNativeFile(resource));
QVERIFY(tmp);
QFile file(resource);
QVERIFY(file.open(QFile::ReadOnly));
QCOMPARE(tmp->readAll(), file.readAll());
}
// setFileTemplate
{
QTemporaryFile file;
std::filesystem::path testFile("testFile1.XXXXXX");
file.setFileTemplate(testFile);
QCOMPARE(file.fileTemplate(), QtPrivate::fromFilesystemPath(testFile));
}
#endif
}

QTEST_MAIN(tst_QTemporaryFile)
#include "tst_qtemporaryfile.moc"

0 comments on commit 0fa4af0

Please sign in to comment.