From patchwork Fri Oct 27 12:43:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 831275 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-465330-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="xdAk3OEU"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yNk8r2sx4z9t2h for ; Fri, 27 Oct 2017 23:44:27 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=rXksqKoYS8YsREiE1KaXKUHfThA62ywS4hsrCdWdQCLND2yx0HHpH so9el0qxIKDo2QKi4fS4f8rQWQYsQqsGnbP1lg49Lvsv/y/GF9h8eDhaIFIWfxpT h+6DfUYIZaTaGV8yQV6gYFWqCOJqK78X6MVSriyL4jke3HbUDwQ+U8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=cY9d48DJ0IDRBdZAtepSmY/mWMg=; b=xdAk3OEUgCd6yN0i1O9y zwsmbJziBDYg5sYe7BL3ZImNPlQoqz4suzZ4BUCWCxfRzRcJorL7TAngMC/Gk6OF Rg63f5Ni/GmmjIsL486QmsGWfxDdaAX3hqIGMRpwJbaHeh02WGyvrWsX0JP86oUH 6nYc7GLHsmClWLQo8jQHNXA= Received: (qmail 63206 invoked by alias); 27 Oct 2017 12:44:06 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 63186 invoked by uid 89); 27 Oct 2017 12:44:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=closes X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 27 Oct 2017 12:44:01 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 82C91806A5; Fri, 27 Oct 2017 12:44:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 82C91806A5 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jwakely@redhat.com Received: from localhost (unknown [10.33.36.5]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED0975D756; Fri, 27 Oct 2017 12:43:59 +0000 (UTC) Date: Fri, 27 Oct 2017 13:43:59 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Provide filesystem::path overloads for file streams (LWG 2676, partial) Message-ID: <20171027124359.GA13254@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.1 (2017-09-22) This implements part of LWG 2676. I haven't added the new members taking wide character strings, because they're only needed on Windows, where the Filesystem library doesn't work yet. I'll send a follow-up patch about those overloads. I've implemented these new functions as templates, which means they work with both std::filesystem::path and std::experimental::filesystem::path. There's another benefit, which is that we don't need to include in , and we don't need to export new symbols from libstdc++.so for these functions (we have explicit instantation definitions in the library). This isn't entirely conforming, because a type that is convertible to filesystem::path will not match these new function templates. We can revisit it once the symbols are added to libstdc++.so but this should be OK for now. * include/std/fstream (basic_filebuf::_If_path): New SFINAE helper. (basic_filebuf::open(const Path&, const ios_base::openmode&)) (basic_ifstream(const Path&, const ios_base::openmode&)) (basic_ifstream::open(const Path&, const ios_base::openmode&)) (basic_ofstream(const Path&, const ios_base::openmode&)) (basic_ofstream::open(const Path&, const ios_base::openmode&)) (basic_fstream(const Path&, const ios_base::openmode&)) (basic_fstream::open(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. Tested powerpc64le-linux, committed to trunk. commit 67bafe2c8409f91c2de758e37830b2d57dedbf02 Author: Jonathan Wakely Date: Tue Oct 24 19:08:15 2017 +0100 Provide filesystem::path overloads for file streams (LWG 2676, partial) * include/std/fstream (basic_filebuf::_If_path): New SFINAE helper. (basic_filebuf::open(const Path&, const ios_base::openmode&)) (basic_ifstream(const Path&, const ios_base::openmode&)) (basic_ifstream::open(const Path&, const ios_base::openmode&)) (basic_ofstream(const Path&, const ios_base::openmode&)) (basic_ofstream::open(const Path&, const ios_base::openmode&)) (basic_fstream(const Path&, const ios_base::openmode&)) (basic_fstream::open(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. diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream index 52830945fe2..3205f81fb47 100644 --- a/libstdc++-v3/include/std/fstream +++ b/libstdc++-v3/include/std/fstream @@ -216,6 +216,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } +#if __cplusplus >= 201703L + template().make_preferred().native())> + using _If_path = enable_if_t, _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 + _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>> + 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 + 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>> + 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 + 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>> + 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 + 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 index 00000000000..56fffde5f9b --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_filebuf/open/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..4442c28c56b --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_fstream/cons/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..8d0127be2b8 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_fstream/open/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..24286f5eeaf --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ifstream/cons/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..192e0fe9e85 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ifstream/open/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..c6b6b237dfd --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ofstream/cons/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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 index 00000000000..a3fc0c7ff68 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/basic_ofstream/open/char/path.cc @@ -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 +// . + +// { dg-options "-std=gnu++17 -lstdc++fs" } +// { dg-do run { target c++17 } } +// { dg-require-fileio "" } +// { dg-require-filesystem-ts "" } + +#include +#include +#include + +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(); +}