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.h | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src/corelib/io/qdir.h') 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; -- cgit v1.2.3