]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Add comparison operators to std::filesystem types
authorJonathan Wakely <jwakely@redhat.com>
Wed, 15 Apr 2020 20:01:42 +0000 (21:01 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 15 Apr 2020 20:01:42 +0000 (21:01 +0100)
Some more C++20 changes from P1614R2, "The Mothership has Landed".

* include/bits/fs_dir.h (file_status): Define operator== for C++20.
(directory_entry): Define operator<=> and remove redundant comparison
operators for C++20.
* include/bits/fs_fwd.h (space_info): Define operator== for C++20.
* include/bits/fs_path.h (path): Define operator<=> and remove
redundant comparison operators for C++20.
* testsuite/27_io/filesystem/path/compare/compare.cc: Fix comment.
* testsuite/27_io/filesystem/path/compare/lwg2936.cc: Likewise.
* testsuite/27_io/filesystem/path/compare/path.cc: Likewise.
* testsuite/27_io/filesystem/path/compare/strings.cc: Likewise.

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/fs_dir.h
libstdc++-v3/include/bits/fs_fwd.h
libstdc++-v3/include/bits/fs_path.h
libstdc++-v3/testsuite/27_io/filesystem/path/compare/compare.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/lwg2936.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/path.cc
libstdc++-v3/testsuite/27_io/filesystem/path/compare/strings.cc
libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc [new file with mode: 0644]

index ac446acaaac2a587f3f8e5a241896245840bfaa4..f9e4c2d87dde708343662ade5f9a62fc2f1e1bf1 100644 (file)
@@ -1,5 +1,16 @@
 2020-04-15  Jonathan Wakely  <jwakely@redhat.com>
 
+       * include/bits/fs_dir.h (file_status): Define operator== for C++20.
+       (directory_entry): Define operator<=> and remove redundant comparison
+       operators for C++20.
+       * include/bits/fs_fwd.h (space_info): Define operator== for C++20.
+       * include/bits/fs_path.h (path): Define operator<=> and remove
+       redundant comparison operators for C++20.
+       * testsuite/27_io/filesystem/path/compare/compare.cc: Fix comment.
+       * testsuite/27_io/filesystem/path/compare/lwg2936.cc: Likewise.
+       * testsuite/27_io/filesystem/path/compare/path.cc: Likewise.
+       * testsuite/27_io/filesystem/path/compare/strings.cc: Likewise.
+
        * include/bits/allocator.h (operator!=): Do not define for C++20.
        * include/bits/locale_classes.h (operator!=): Likewise.
        * include/bits/std_function.h (operator==(nullptr_t, const function&))
index df03f892f6002d705dd736d3f56aea3e7d6845e2..686dfce6e5fd9f6252a84e30251767fb0cfabe5e 100644 (file)
 # include <bits/unique_ptr.h>
 # include <bits/shared_ptr.h>
 
+#if __cplusplus > 201703L
+# include <compare>    // std::strong_ordering
+#endif
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -72,6 +76,11 @@ namespace filesystem
     void       type(file_type __ft) noexcept { _M_type = __ft; }
     void       permissions(perms __prms) noexcept { _M_perms = __prms; }
 
+#if __cpp_lib_three_way_comparison
+    friend bool
+    operator==(const file_status&, const file_status&) noexcept = default;
+#endif
+
   private:
     file_type  _M_type;
     perms      _M_perms;
@@ -272,18 +281,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
     symlink_status(error_code& __ec) const noexcept
     { return filesystem::symlink_status(_M_path, __ec); }
 
-    bool
-    operator< (const directory_entry& __rhs) const noexcept
-    { return _M_path < __rhs._M_path; }
-
     bool
     operator==(const directory_entry& __rhs) const noexcept
     { return _M_path == __rhs._M_path; }
 
+#if __cpp_lib_three_way_comparison
+    strong_ordering
+    operator<=>(const directory_entry& __rhs) const noexcept
+    { return _M_path <=> __rhs._M_path; }
+#else
     bool
     operator!=(const directory_entry& __rhs) const noexcept
     { return _M_path != __rhs._M_path; }
 
+    bool
+    operator< (const directory_entry& __rhs) const noexcept
+    { return _M_path < __rhs._M_path; }
+
     bool
     operator<=(const directory_entry& __rhs) const noexcept
     { return _M_path <= __rhs._M_path; }
@@ -295,6 +309,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
     bool
     operator>=(const directory_entry& __rhs) const noexcept
     { return _M_path >= __rhs._M_path; }
+#endif
 
   private:
     friend class _Dir;
index 6363eca867c22355d62ade980051096269afe91d..d94cc4149067096269cf2161db17e58249ea5a5e 100644 (file)
@@ -66,6 +66,10 @@ _GLIBCXX_END_NAMESPACE_CXX11
     uintmax_t capacity;
     uintmax_t free;
     uintmax_t available;
+
+#if __cpp_impl_three_way_comparison >= 201907L
+    friend bool operator==(const space_info&, const space_info&) = default;
+#endif
   };
 
   enum class file_type : signed char {
index fb6e8a5247f6520392d59dc6e75c388864358de2..ee6ab15cc4c222b42be117d51a98eb3b41092548 100644 (file)
 #include <bits/shared_ptr.h>
 #include <bits/unique_ptr.h>
 
+#if __cplusplus > 201703L
+# include <compare>
+#endif
+
 #if defined(_WIN32) && !defined(__CYGWIN__)
 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
 # include <algorithm>
@@ -451,6 +455,20 @@ namespace __detail
 
     // non-member operators
 
+    /// Compare paths
+    friend bool operator==(const path& __lhs, const path& __rhs) noexcept
+    { return __lhs.compare(__rhs) == 0; }
+
+#if __cpp_lib_three_way_comparison
+    /// Compare paths
+    friend strong_ordering
+    operator<=>(const path& __lhs, const path& __rhs) noexcept
+    { return __lhs.compare(__rhs) <=> 0; }
+#else
+    /// Compare paths
+    friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
+    { return !(__lhs == __rhs); }
+
     /// Compare paths
     friend bool operator<(const path& __lhs, const path& __rhs) noexcept
     { return __lhs.compare(__rhs) < 0; }
@@ -466,14 +484,7 @@ namespace __detail
     /// Compare paths
     friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
     { return !(__lhs < __rhs); }
-
-    /// Compare paths
-    friend bool operator==(const path& __lhs, const path& __rhs) noexcept
-    { return __lhs.compare(__rhs) == 0; }
-
-    /// Compare paths
-    friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
-    { return !(__lhs == __rhs); }
+#endif
 
     /// Append one path to another
     friend path operator/(const path& __lhs, const path& __rhs)
index 88a9d0c32037b346ceed0f0610d341a6c56ec12a..3c9974aeaccaf83440777615d804ec8f8162886f 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index 44ebac7b07a35eeb634454c551f03b554b8060cb..b9978edd8f705dd4fe1b515a2f10e811b03e0f31 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index c58722cdc69ac61c7fc553d504802401ed7db722..6f4166b641db823f64670ca07fd4775575521ba4 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
index e014665b6d71a03222c9f2e190203cda91371ef4..e9c900c49e4e6beb76532b3d1f7c5d3c06e709df 100644 (file)
@@ -18,7 +18,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 8.4.8 path compare [path.compare]
+// C++17 30.10.8.4.8 path compare [fs.path.compare]
 
 #include <filesystem>
 #include <testsuite_hooks.h>
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp.cc
new file mode 100644 (file)
index 0000000..d9adfad
--- /dev/null
@@ -0,0 +1,79 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// C++17 30.10.8.6 path non-member functions [fs.path.nonmember]
+
+#include <filesystem>
+#include <testsuite_hooks.h>
+#include <testsuite_fs.h>
+
+using std::filesystem::path;
+
+void
+test01()
+{
+  path p("/foo/bar");
+  VERIFY( p == p );
+  VERIFY( p == "/foo//bar" );
+
+  path q("/foo/baz");
+  VERIFY( p < q );
+  VERIFY( q > p );
+
+  path r("/foo/bar/.");
+  VERIFY( p < r );
+
+  VERIFY( path("a/b/") == path("a/b//") );
+}
+
+void
+test02()
+{
+  const path p0 = "/a/a/b/b";
+  for (const path& p : __gnu_test::test_paths)
+  {
+    VERIFY( p.compare(p) == 0 );
+    int cmp = p.compare(p0);
+    if (cmp == 0)
+      VERIFY( p0 == p );
+    else if (cmp < 0)
+      VERIFY( p0 > p );
+    else if (cmp > 0)
+      VERIFY( p0 < p );
+  }
+}
+
+void
+test03()
+{
+  VERIFY( path("/") == path("////") );
+  VERIFY( path("/a") > path("/") );
+  VERIFY( path("/") < path("/a") );
+  VERIFY( path("/ab") > path("/a") );
+  VERIFY( path("/ab") > path("/a/b") );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}
diff --git a/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc b/libstdc++-v3/testsuite/27_io/filesystem/path/nonmember/cmp_c++20.cc
new file mode 100644 (file)
index 0000000..34e8bf8
--- /dev/null
@@ -0,0 +1,80 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// C++20 29.11.7.7 Non-member functions [fs.path.nonmember]
+
+#include <filesystem>
+#include <testsuite_hooks.h>
+#include <testsuite_fs.h>
+
+using std::filesystem::path;
+
+void
+test01()
+{
+  path p("/foo/bar");
+  VERIFY( p == p );
+  VERIFY( p == "/foo//bar" );
+  VERIFY( std::is_eq(p <=> p) );
+  VERIFY( std::is_eq(p <=> "/foo//bar") );
+
+  path q("/foo/baz");
+  VERIFY( p < q );
+  VERIFY( q > p );
+  VERIFY( std::is_lt(p <=> q) );
+  VERIFY( std::is_gt(q <=> p) );
+
+  path r("/foo/bar/.");
+  VERIFY( p < r );
+  VERIFY( std::is_lt(p <=> r) );
+
+  VERIFY( path("a/b/") == path("a/b//") );
+  VERIFY( std::is_eq(path("a/b/") <=> path("a/b//")) );
+}
+
+void
+test02()
+{
+  const path p0 = "/a/a/b/b";
+  for (const path& p : __gnu_test::test_paths)
+  {
+    VERIFY( std::is_eq(p <=> p) );
+    VERIFY( (p <=> p0) == (p.compare(p0) <=> 0) );
+    VERIFY( (p0 <=> p) == (p0.compare(p) <=> 0) );
+  }
+}
+
+void
+test03()
+{
+  VERIFY( std::is_eq(path("/") <=> path("////")) );
+  VERIFY( std::is_gt(path("/a") <=> path("/")) );
+  VERIFY( std::is_lt(path("/") <=> path("/a")) );
+  VERIFY( std::is_gt(path("/ab") <=> path("/a")) );
+  VERIFY( std::is_gt(path("/ab") <=> path("/a/b")) );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}
This page took 0.078079 seconds and 5 git commands to generate.