Provide filesystem::path overloads for file streams (LWG 2676, partial)
authorJonathan Wakely <jwakely@redhat.com>
Fri, 27 Oct 2017 12:39:01 +0000 (13:39 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 27 Oct 2017 12:39:01 +0000 (13:39 +0100)
* include/std/fstream (basic_filebuf::_If_path): New SFINAE helper.
(basic_filebuf::open<Path>(const Path&, const ios_base::openmode&))
(basic_ifstream<Path>(const Path&, const ios_base::openmode&))
(basic_ifstream::open<Path>(const Path&, const ios_base::openmode&))
(basic_ofstream<Path>(const Path&, const ios_base::openmode&))
(basic_ofstream::open<Path>(const Path&, const ios_base::openmode&))
(basic_fstream<Path>(const Path&, const ios_base::openmode&))
(basic_fstream::open<Path>(const Path&, const ios_base::openmode&)):
New constructors and member functions.
* testsuite/27_io/basic_filebuf/open/char/path.cc: New test.
* testsuite/27_io/basic_fstream/cons/char/path.cc: New test.
* testsuite/27_io/basic_fstream/open/char/path.cc: New test.
* testsuite/27_io/basic_ifstream/cons/char/path.cc: New test.
* testsuite/27_io/basic_ifstream/open/char/path.cc: New test.
* testsuite/27_io/basic_ofstream/cons/char/path.cc: New test.
* testsuite/27_io/basic_ofstream/open/char/path.cc: New test.

From-SVN: r254148

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/fstream
libstdc++-v3/testsuite/27_io/basic_filebuf/open/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_fstream/cons/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_fstream/open/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_ifstream/cons/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_ifstream/open/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_ofstream/cons/char/path.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/basic_ofstream/open/char/path.cc [new file with mode: 0644]

index ea6b509..1821d78 100644 (file)
@@ -1,5 +1,22 @@
 2017-10-27  Jonathan Wakely  <jwakely@redhat.com>
 
+       * include/std/fstream (basic_filebuf::_If_path): New SFINAE helper.
+       (basic_filebuf::open<Path>(const Path&, const ios_base::openmode&))
+       (basic_ifstream<Path>(const Path&, const ios_base::openmode&))
+       (basic_ifstream::open<Path>(const Path&, const ios_base::openmode&))
+       (basic_ofstream<Path>(const Path&, const ios_base::openmode&))
+       (basic_ofstream::open<Path>(const Path&, const ios_base::openmode&))
+       (basic_fstream<Path>(const Path&, const ios_base::openmode&))
+       (basic_fstream::open<Path>(const Path&, const ios_base::openmode&)):
+       New constructors and member functions.
+       * testsuite/27_io/basic_filebuf/open/char/path.cc: New test.
+       * testsuite/27_io/basic_fstream/cons/char/path.cc: New test.
+       * testsuite/27_io/basic_fstream/open/char/path.cc: New test.
+       * testsuite/27_io/basic_ifstream/cons/char/path.cc: New test.
+       * testsuite/27_io/basic_ifstream/open/char/path.cc: New test.
+       * testsuite/27_io/basic_ofstream/cons/char/path.cc: New test.
+       * testsuite/27_io/basic_ofstream/open/char/path.cc: New test.
+
        * include/bits/fs_path.h (path::format): Define new enumeration type.
        (path(string_type&&), path<Source>(const Source&))
        (path<InputIterator>(InputIterator, InputIterator))
index 5283094..3205f81 100644 (file)
@@ -216,6 +216,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
       }
 
+#if __cplusplus >= 201703L
+      template<typename _Path, typename _Result = _Path, typename _Path2
+              = decltype(std::declval<_Path&>().make_preferred().native())>
+       using _If_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
+#endif // C++17
+
     public:
       // Constructors/destructor:
       /**
@@ -306,7 +312,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __filebuf_type*
       open(const std::string& __s, ios_base::openmode __mode)
       { return open(__s.c_str(), __mode); }
-#endif
+
+#if __cplusplus >= 201703L
+      /**
+       *  @brief  Opens an external file.
+       *  @param  __s  The name of the file, as a filesystem::path.
+       *  @param  __mode  The open mode flags.
+       *  @return  @c this on success, NULL on failure
+       */
+      template<typename _Path>
+       _If_path<_Path, __filebuf_type*>
+       open(const _Path& __s, ios_base::openmode __mode)
+       { return open(__s.c_str(), __mode); }
+#endif // C++17
+#endif // C++11
 
       /**
        *  @brief  Closes the currently associated file.
@@ -516,13 +535,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        this->open(__s, __mode);
       }
 
+#if __cplusplus >= 201703L
+      /**
+       *  @param  Create an input file stream.
+       *  @param  __s  filesystem::path specifying the filename.
+       *  @param  __mode  Open file in specified mode (see std::ios_base).
+       *
+       *  @c ios_base::in is automatically included in @a __mode.
+       */
+      template<typename _Path, typename = _Require<
+         is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
+       basic_ifstream(const _Path& __s,
+                      ios_base::openmode __mode = ios_base::in)
+       : basic_ifstream(__s.c_str(), __mode)
+       { }
+#endif // C++17
+
       basic_ifstream(const basic_ifstream&) = delete;
 
       basic_ifstream(basic_ifstream&& __rhs)
       : __istream_type(std::move(__rhs)),
       _M_filebuf(std::move(__rhs._M_filebuf))
       { __istream_type::set_rdbuf(&_M_filebuf); }
-#endif
+#endif // C++11
 
       /**
        *  @brief  The destructor does nothing.
@@ -621,7 +656,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          // 409. Closing an fstream should clear error state
          this->clear();
       }
-#endif
+
+#if __cplusplus >= 201703L
+      /**
+       *  @brief  Opens an external file.
+       *  @param  __s  The name of the file, as a filesystem::path.
+       *  @param  __mode  The open mode flags.
+       *
+       *  Calls @c std::basic_filebuf::open(__s,__mode|in).  If that function
+       *  fails, @c failbit is set in the stream's error state.
+       */
+      template<typename _Path>
+       auto
+       open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
+       -> decltype(_M_filebuf.open(__s, __mode))
+       { open(__s.c_str(), __mode); }
+#endif // C++17
+#endif // C++11
 
       /**
        *  @brief  Close the file.
@@ -720,6 +771,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        this->open(__s, __mode);
       }
 
+#if __cplusplus >= 201703L
+      /**
+       *  @param  Create an output file stream.
+       *  @param  __s  filesystem::path specifying the filename.
+       *  @param  __mode  Open file in specified mode (see std::ios_base).
+       *
+       *  @c ios_base::out | @c ios_base::trunc is automatically included in
+       *  @a __mode.
+       */
+      template<typename _Path, typename = _Require<
+         is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
+       basic_ofstream(const _Path& __s, ios_base::openmode __mode
+                      = ios_base::out|ios_base::trunc)
+       : basic_ofstream(__s.c_str(), __mode)
+       { }
+#endif // C++17
+
       basic_ofstream(const basic_ofstream&) = delete;
 
       basic_ofstream(basic_ofstream&& __rhs)
@@ -827,7 +895,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          // 409. Closing an fstream should clear error state
          this->clear();
       }
-#endif
+
+#if __cplusplus >= 201703L
+      /**
+       *  @brief  Opens an external file.
+       *  @param  __s  The name of the file, as a filesystem::path.
+       *  @param  __mode  The open mode flags.
+       *
+       *  Calls @c std::basic_filebuf::open(__s,__mode|out).  If that
+       *  function fails, @c failbit is set in the stream's error state.
+       */
+      template<typename _Path>
+       auto
+       open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
+       -> decltype(_M_filebuf.open(__s, __mode))
+       { open(__s.c_str(), __mode); }
+#endif // C++17
+#endif // C++11
 
       /**
        *  @brief  Close the file.
@@ -922,6 +1006,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        this->open(__s, __mode);
       }
 
+#if __cplusplus >= 201703L
+      /**
+       *  @param  Create an input/output file stream.
+       *  @param  __s  filesystem::path specifying the filename.
+       *  @param  __mode  Open file in specified mode (see std::ios_base).
+       */
+      template<typename _Path, typename = _Require<
+         is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
+       basic_fstream(const _Path& __s,
+                     ios_base::openmode __mode = ios_base::in | ios_base::out)
+       : basic_fstream(__s.c_str(), __mode)
+       { }
+#endif // C++17
+
       basic_fstream(const basic_fstream&) = delete;
 
       basic_fstream(basic_fstream&& __rhs)
@@ -1029,7 +1127,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          // 409. Closing an fstream should clear error state
          this->clear();
       }
-#endif
+
+#if __cplusplus >= 201703L
+      /**
+       *  @brief  Opens an external file.
+       *  @param  __s  The name of the file, as a filesystem::path.
+       *  @param  __mode  The open mode flags.
+       *
+       *  Calls @c std::basic_filebuf::open(__s,__mode).  If that
+       *  function fails, @c failbit is set in the stream's error state.
+       */
+      template<typename _Path>
+       auto
+       open(const _Path& __s,
+            ios_base::openmode __mode = ios_base::in | ios_base::out)
+       -> decltype(_M_filebuf.open(__s, __mode))
+       { open(__s.c_str(), __mode); }
+#endif // C++17
+#endif // C++11
 
       /**
        *  @brief  Close the file.
diff --git a/libstdc++-v3/testsuite/27_io/basic_filebuf/open/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_filebuf/open/char/path.cc
new file mode 100644 (file)
index 0000000..56fffde
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "filebuf_members-1.tst";
+
+void
+test01()
+{
+  std::filebuf fb;
+  fb.open(filename, std::ios::in);
+  VERIFY( fb.is_open() );
+}
+
+int
+main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_fstream/cons/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_fstream/cons/char/path.cc
new file mode 100644 (file)
index 0000000..4442c28
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ofstream_members-1.tst";
+
+void
+test01()
+{
+  std::fstream f(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::fstream f(filename, std::ios::out);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_fstream/open/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_fstream/open/char/path.cc
new file mode 100644 (file)
index 0000000..8d0127b
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ofstream_members-1.tst";
+
+void
+test01()
+{
+  std::fstream f;
+  f.open(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::fstream f;
+  f.open(filename, std::ios::in|std::ios::out);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_ifstream/cons/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_ifstream/cons/char/path.cc
new file mode 100644 (file)
index 0000000..24286f5
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ifstream_members-1.tst";
+
+void
+test01()
+{
+  std::ifstream f(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::ifstream f(filename, std::ios::in);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_ifstream/open/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_ifstream/open/char/path.cc
new file mode 100644 (file)
index 0000000..192e0fe
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ifstream_members-1.tst";
+
+void
+test01()
+{
+  std::ifstream f;
+  f.open(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::ifstream f;
+  f.open(filename, std::ios::in);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_ofstream/cons/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_ofstream/cons/char/path.cc
new file mode 100644 (file)
index 0000000..c6b6b23
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ofstream_members-1.tst";
+
+void
+test01()
+{
+  std::ofstream f(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::ofstream f(filename, std::ios::out);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/27_io/basic_ofstream/open/char/path.cc b/libstdc++-v3/testsuite/27_io/basic_ofstream/open/char/path.cc
new file mode 100644 (file)
index 0000000..a3fc0c7
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-options "-std=gnu++17 -lstdc++fs" }
+// { dg-do run { target c++17 } }
+// { dg-require-fileio "" }
+// { dg-require-filesystem-ts "" }
+
+#include <fstream>
+#include <filesystem>
+#include <testsuite_hooks.h>
+
+const std::filesystem::path filename = "ofstream_members-1.tst";
+
+void
+test01()
+{
+  std::ofstream f;
+  f.open(filename);
+  VERIFY( f.is_open() );
+}
+
+void
+test02()
+{
+  std::ofstream f;
+  f.open(filename, std::ios::out);
+  VERIFY( f.is_open() );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}