From 737fe89691a3d8dfa779862c9daaa002e00f3dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 7 Feb 2020 14:03:43 +0100 Subject: Q{File,FileInfo,Dir}: add std::filesystem::path overloads Add some overloads where (I thought) it makes sense for QDir and QFile to accept std::filesystem::path objects. Currently my thinking is to not add overloads for static functions where std::filesystem can already do the same job, e.g. create directory or file. Template and enable_if is needed due to both QString and std::filesystem::path being able to be constructed from string literals. The common shared code is currently in QFile because QDir had an implicit include of QFile, made explicit in this patch, and QFileInfo has an include to QFile as well. The QT_HAS_STD_FILESYSTEM macro is visible in user-code which I currently take advantage of in the tests, and users could too. Change-Id: I8d05d3c34c6c17e20972a6a2053862b8891d6c3c Reviewed-by: Thiago Macieira --- src/corelib/io/qdir.cpp | 65 +++++++++++++++++++++++++- src/corelib/io/qdir.h | 44 +++++++++++++++++- src/corelib/io/qfile.cpp | 50 ++++++++++++++++++++ src/corelib/io/qfile.h | 107 ++++++++++++++++++++++++++++++++++++++++++- src/corelib/io/qfileinfo.cpp | 81 +++++++++++++++++++++++++++++++- src/corelib/io/qfileinfo.h | 38 +++++++++++++++ 6 files changed, 380 insertions(+), 5 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 5ce3fbcca8..20af40f973 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -585,8 +585,8 @@ QDir::QDir(const QString &path) : d_ptr(new QDirPrivate(path)) also sorts the names using \a sort. The default \a nameFilter is an empty string, which excludes - nothing; the default \a filters is \l AllEntries, which also means - exclude nothing. The default \a sort is \l Name | \l IgnoreCase, + nothing; the default \a filters is \l AllEntries, which also + excludes nothing. The default \a sort is \l Name | \l IgnoreCase, i.e. sort by name case-insensitively. If \a path is an empty string, QDir uses "." (the current @@ -2551,4 +2551,65 @@ QDebug operator<<(QDebug debug, const QDir &dir) } #endif // QT_NO_DEBUG_STREAM +/*! + \fn QDir::QDir(const std::filesystem::path &path) + \since 6.0 + Constructs a QDir pointing to the given directory \a path. If path + is empty the program's working directory, ("."), is used. + + \sa currentPath() +*/ +/*! + \fn QDir::QDir(const std::filesystem::path &path, + const QString &nameFilter, + SortFlags sort, + Filters filters) + \since 6.0 + + Constructs a QDir with path \a path, that filters its entries by + name using \a nameFilter and by attributes using \a filters. It + also sorts the names using \a sort. + + The default \a nameFilter is an empty string, which excludes + nothing; the default \a filters is \l AllEntries, which also + excludes nothing. The default \a sort is \l Name | \l IgnoreCase, + i.e. sort by name case-insensitively. + + If \a path is empty, QDir uses "." (the current + directory). If \a nameFilter is an empty string, QDir uses the + name filter "*" (all files). + + Note that \a path need not exist. + + \sa exists(), setPath(), setNameFilters(), setFilter(), setSorting() +*/ +/*! + \fn void QDir::setPath(const std::filesystem::path &path) + \since 6.0 + \overload +*/ +/*! + \fn void QDir::addSearchPath(const QString &prefix, const std::filesystem::path &path) + \since 6.0 + \overload +*/ +/*! + \fn std::filesystem::path QDir::filesystemPath() const + \since 6.0 + Returns path() as \c{std::filesystem::path}. + \sa path() +*/ +/*! + \fn std::filesystem::path QDir::filesystemAbsolutePath() const + \since 6.0 + Returns absolutePath() as \c{std::filesystem::path}. + \sa absolutePath() +*/ +/*! + \fn std::filesystem::path QDir::filesystemCanonicalPath() const + \since 6.0 + Returns canonicalPath() as \c{std::filesystem::path}. + \sa canonicalPath() +*/ + QT_END_NAMESPACE diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h index 45a40995f8..f0dda73ebb 100644 --- a/src/corelib/io/qdir.h +++ b/src/corelib/io/qdir.h @@ -41,13 +41,13 @@ #define QDIR_H #include +#include #include #include #include QT_BEGIN_NAMESPACE - class QDirIterator; class QDirPrivate; @@ -102,6 +102,22 @@ public: QDir(const QString &path = QString()); QDir(const QString &path, const QString &nameFilter, SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries); +#ifdef Q_CLANG_QDOC + QDir(const std::filesystem::path &path); + QDir(const std::filesystem::path &path, const QString &nameFilter, + SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path)) + { + } + template = 0> + QDir(const T &path, const QString &nameFilter, + SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries) + : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter) + { + } +#endif // QT_CONFIG(cxx17_filesystem) ~QDir(); QDir &operator=(const QDir &); @@ -115,9 +131,26 @@ public: { qSwap(d_ptr, other.d_ptr); } void setPath(const QString &path); +#ifdef Q_CLANG_QDOC + void setPath(const std::filesystem::path &path); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + void setPath(const T &path) + { + setPath(QtPrivate::fromFilesystemPath(path)); + } +#endif // QT_CONFIG(cxx17_filesystem) QString path() const; QString absolutePath() const; QString canonicalPath() const; +#if QT_CONFIG(cxx17_filesystem) + std::filesystem::path filesystemPath() const + { return QtPrivate::toFilesystemPath(path()); } + std::filesystem::path filesystemAbsolutePath() const + { return QtPrivate::toFilesystemPath(absolutePath()); } + std::filesystem::path filesystemCanonicalPath() const + { return QtPrivate::toFilesystemPath(canonicalPath()); } +#endif // QT_CONFIG(cxx17_filesystem) #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED_X("Use QDir::addSearchPath() instead") @@ -126,6 +159,15 @@ public: static void setSearchPaths(const QString &prefix, const QStringList &searchPaths); static void addSearchPath(const QString &prefix, const QString &path); +#ifdef Q_CLANG_QDOC + static void addSearchPath(const QString &prefix, const std::filesystem::path &path); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + static void addSearchPath(const QString &prefix, const T &path) + { + addSearchPath(prefix, QtPrivate::fromFilesystemPath(path)); + } +#endif // QT_CONFIG(cxx17_filesystem) static QStringList searchPaths(const QString &prefix); QString dirName() const; diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 0cdc5bd6d3..2a9c24bbd5 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -1210,6 +1210,56 @@ qint64 QFile::size() const return QFileDevice::size(); // for now } +/*! + \fn QFile::QFile(const std::filesystem::path &name) + \since 6.0 + + Constructs a new file object to represent the file with the given \a name. +*/ +/*! + \fn QFile::QFile(const std::filesystem::path &name, QObject *parent) + \since 6.0 + + Constructs a new file object with the given \a parent to represent the + file with the specified \a name. +*/ +/*! + \fn std::filesystem::path QFile::filesystemFileName() const + \since 6.0 + Returns fileName() as \c{std::filesystem::path}. +*/ +/*! + \fn void QFile::setFileName(const std::filesystem::path &name) + \since 6.0 + \overload +*/ +/*! + \fn bool QFile::rename(const std::filesystem::path &newName) + \since 6.0 + \overload +*/ +/*! + \fn bool QFile::link(const std::filesystem::path &newName) + \since 6.0 + \overload +*/ +/*! + \fn bool QFile::copy(const std::filesystem::path &newName) + \since 6.0 + \overload +*/ +/*! + \fn QFile::Permissions QFile::permissions(const std::filesystem::path &filename) + \since 6.0 + \overload +*/ +/*! + \fn bool QFile::setPermissions(const std::filesystem::path &filename, Permissions permissionSpec) + \since 6.0 + \overload +*/ + + QT_END_NAMESPACE #ifndef QT_NO_QOBJECT diff --git a/src/corelib/io/qfile.h b/src/corelib/io/qfile.h index 917fec4e1a..dab20b85a1 100644 --- a/src/corelib/io/qfile.h +++ b/src/corelib/io/qfile.h @@ -45,12 +45,44 @@ #include #include +#if QT_CONFIG(cxx17_filesystem) +#include +#endif + #ifdef open #error qfile.h must be included before any header file that defines open #endif QT_BEGIN_NAMESPACE +#if QT_CONFIG(cxx17_filesystem) +namespace QtPrivate { +inline QString fromFilesystemPath(const std::filesystem::path &path) +{ +#ifdef Q_OS_WIN + return QString::fromStdWString(path.native()); +#else + return QString::fromStdString(path.native()); +#endif +} + +inline std::filesystem::path toFilesystemPath(const QString &path) +{ +#ifdef Q_OS_WIN + return std::filesystem::path(path.toStdU16String()); +#else + return std::filesystem::path(path.toStdString()); +#endif +} + +// Both std::filesystem::path and QString (without QT_NO_CAST_FROM_ASCII) can be implicitly +// constructed from string literals so we force the std::fs::path parameter to only +// accept std::fs::path with no implicit conversions. +template +using ForceFilesystemPath = typename std::enable_if_t, int>; +} +#endif // QT_CONFIG(cxx17_filesystem) + class QTemporaryFile; class QFilePrivate; @@ -64,14 +96,45 @@ class Q_CORE_EXPORT QFile : public QFileDevice public: QFile(); QFile(const QString &name); +#ifdef Q_CLANG_QDOC + QFile(const std::filesystem::path &name); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + QFile(const T &name) : QFile(QtPrivate::fromFilesystemPath(name)) + { + } +#endif // QT_CONFIG(cxx17_filesystem) + #ifndef QT_NO_QOBJECT explicit QFile(QObject *parent); QFile(const QString &name, QObject *parent); -#endif + +#ifdef Q_CLANG_QDOC + QFile(const std::filesystem::path &path, QObject *parent); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + QFile(const T &path, QObject *parent) : QFile(QtPrivate::fromFilesystemPath(path), parent) + { + } +#endif // QT_CONFIG(cxx17_filesystem) +#endif // !QT_NO_QOBJECT ~QFile(); QString fileName() const override; +#if QT_CONFIG(cxx17_filesystem) + std::filesystem::path filesystemFileName() const + { return QtPrivate::toFilesystemPath(fileName()); } +#endif void setFileName(const QString &name); +#ifdef Q_CLANG_QDOC + void setFileName(const std::filesystem::path &name); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + void setFileName(const T &name) + { + setFileName(QtPrivate::fromFilesystemPath(name)); + } +#endif // QT_CONFIG(cxx17_filesystem) #if defined(Q_OS_DARWIN) // Mac always expects filenames in UTF-8... and decomposed... @@ -129,12 +192,39 @@ public: static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr); bool rename(const QString &newName); +#ifdef Q_CLANG_QDOC + bool rename(const std::filesystem::path &newName); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + bool rename(const T &newName) + { + return rename(QtPrivate::fromFilesystemPath(newName)); + } +#endif // QT_CONFIG(cxx17_filesystem) static bool rename(const QString &oldName, const QString &newName); bool link(const QString &newName); +#ifdef Q_CLANG_QDOC + bool link(const std::filesystem::path &newName); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + bool link(const T &newName) + { + return link(QtPrivate::fromFilesystemPath(newName)); + } +#endif // QT_CONFIG(cxx17_filesystem) static bool link(const QString &oldname, const QString &newName); bool copy(const QString &newName); +#ifdef Q_CLANG_QDOC + bool copy(const std::filesystem::path &newName); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + bool copy(const T &newName) + { + return copy(QtPrivate::fromFilesystemPath(newName)); + } +#endif // QT_CONFIG(cxx17_filesystem) static bool copy(const QString &fileName, const QString &newName); bool open(OpenMode flags) override; @@ -150,6 +240,21 @@ public: static Permissions permissions(const QString &filename); bool setPermissions(Permissions permissionSpec) override; static bool setPermissions(const QString &filename, Permissions permissionSpec); +#ifdef Q_CLANG_QDOC + static Permissions permissions(const std::filesystem::path &filename); + static bool setPermissions(const std::filesystem::path &filename, Permissions permissionSpec); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + static Permissions permissions(const T &filename) + { + return permissions(QtPrivate::fromFilesystemPath(filename)); + } + template = 0> + static bool setPermissions(const T &filename, Permissions permissionSpec) + { + return setPermissions(QtPrivate::fromFilesystemPath(filename), permissionSpec); + } +#endif // QT_CONFIG(cxx17_filesystem) protected: #ifdef QT_NO_QOBJECT diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index 64b1557231..479532fc36 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -363,7 +363,7 @@ QFileInfo::QFileInfo(const QFile &file) : d_ptr(new QFileInfoPrivate(file.fileNa /*! Constructs a new QFileInfo that gives information about the given - \a file in the directory \a dir. + \a file relative to the directory \a dir. If \a dir has a relative path, the QFileInfo will also have a relative path. @@ -1576,4 +1576,83 @@ QDebug operator<<(QDebug dbg, const QFileInfo &fi) } #endif +/*! + \fn QFileInfo::QFileInfo(const std::filesystem::path &file) + \since 6.0 + + Constructs a new QFileInfo that gives information about the given + \a file. + + \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath() +*/ +/*! + \fn QFileInfo::QFileInfo(const QDir &dir, const std::filesystem::path &file) + \since 6.0 + + Constructs a new QFileInfo that gives information about the given + \a file relative to the directory \a dir. + + If \a dir has a relative path, the QFileInfo will also have a + relative path. + + If \a file is an absolute path, then the directory specified + by \a dir will be disregarded. +*/ +/*! + \fn void QFileInfo::setFile(const std::filesystem::path &file) + \since 6.0 + + Sets the file that the QFileInfo provides information about to \a + file. +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemFilePath() const + \since 6.0 + + Returns filePath() as a \c{std::filesystem::path}. + \sa filePath() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemAbsoluteFilePath() const + \since 6.0 + + Returns absoluteFilePath() as a \c{std::filesystem::path}. + \sa absoluteFilePath() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemCanonicalFilePath() const + \since 6.0 + + Returns canonicalFilePath() as a \c{std::filesystem::path}. + \sa canonicalFilePath() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemPath() const + \since 6.0 + + Returns path() as a \c{std::filesystem::path}. + \sa path() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemAbsolutePath() const + \since 6.0 + + Returns absolutePath() as a \c{std::filesystem::path}. + \sa absolutePath() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemCanonicalPath() const + \since 6.0 + + Returns canonicalPath() as a \c{std::filesystem::path}. + \sa canonicalPath() +*/ +/*! + \fn std::filesystem::path QFileInfo::filesystemSymLinkTarget() const + \since 6.0 + + Returns symLinkTarget() as a \c{std::filesystem::path}. + \sa symLinkTarget() +*/ + QT_END_NAMESPACE diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 7c7ff56ae4..2bed64eb1a 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -64,6 +64,18 @@ public: QFileInfo(const QFile &file); QFileInfo(const QDir &dir, const QString &file); QFileInfo(const QFileInfo &fileinfo); +#ifdef Q_CLANG_QDOC + QFileInfo(const std::filesystem::path &file); + QFileInfo(const QDir &dir, const std::filesystem::path &file); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + QFileInfo(const T &file) : QFileInfo(QtPrivate::fromFilesystemPath(file)) { } + + template = 0> + QFileInfo(const QDir &dir, const T &file) : QFileInfo(dir, QtPrivate::fromFilesystemPath(file)) + { + } +#endif // QT_CONFIG(cxx17_filesystem) ~QFileInfo(); QFileInfo &operator=(const QFileInfo &fileinfo); @@ -78,6 +90,13 @@ public: void setFile(const QString &file); void setFile(const QFile &file); void setFile(const QDir &dir, const QString &file); +#ifdef Q_CLANG_QDOC + void setFile(const std::filesystem::path &file); +#elif QT_CONFIG(cxx17_filesystem) + template = 0> + void setFile(const T &file) { setFile(QtPrivate::fromFilesystemPath(file)); } +#endif // QT_CONFIG(cxx17_filesystem) + bool exists() const; static bool exists(const QString &file); void refresh(); @@ -85,6 +104,14 @@ public: QString filePath() const; QString absoluteFilePath() const; QString canonicalFilePath() const; +#if QT_CONFIG(cxx17_filesystem) + std::filesystem::path filesystemFilePath() const + { return QtPrivate::toFilesystemPath(filePath()); } + std::filesystem::path filesystemAbsoluteFilePath() const + { return QtPrivate::toFilesystemPath(absoluteFilePath()); } + std::filesystem::path filesystemCanonicalFilePath() const + { return QtPrivate::toFilesystemPath(canonicalFilePath()); } +#endif // QT_CONFIG(cxx17_filesystem) QString fileName() const; QString baseName() const; QString completeBaseName() const; @@ -95,6 +122,13 @@ public: QString path() const; QString absolutePath() const; QString canonicalPath() const; +#if QT_CONFIG(cxx17_filesystem) + std::filesystem::path filesystemPath() const { return QtPrivate::toFilesystemPath(path()); } + std::filesystem::path filesystemAbsolutePath() const + { return QtPrivate::toFilesystemPath(absolutePath()); } + std::filesystem::path filesystemCanonicalPath() const + { return QtPrivate::toFilesystemPath(canonicalPath()); } +#endif // QT_CONFIG(cxx17_filesystem) QDir dir() const; QDir absoluteDir() const; @@ -122,6 +156,10 @@ public: QString readLink() const; #endif QString symLinkTarget() const; +#if QT_CONFIG(cxx17_filesystem) + std::filesystem::path filesystemSymLinkTarget() const + { return QtPrivate::toFilesystemPath(symLinkTarget()); } +#endif // QT_CONFIG(cxx17_filesystem) QString owner() const; uint ownerId() const; -- cgit v1.2.3