From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 9CE0A3989CAB for ; Tue, 27 Apr 2021 13:10:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9CE0A3989CAB Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-533-hMQASjQ1N1SyYaPWp9LbCg-1; Tue, 27 Apr 2021 09:10:35 -0400 X-MC-Unique: hMQASjQ1N1SyYaPWp9LbCg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F3592818400; Tue, 27 Apr 2021 13:10:33 +0000 (UTC) Received: from localhost (unknown [10.33.36.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DFEB60CCC; Tue, 27 Apr 2021 13:10:33 +0000 (UTC) Date: Tue, 27 Apr 2021 14:10:32 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Minor refactoring in Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="P0j8di7fn9q3KBf5" Content-Disposition: inline X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Apr 2021 13:10:40 -0000 --P0j8di7fn9q3KBf5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline libstdc++-v3/ChangeLog: * include/experimental/internet (address_v6::bytes_type): Adjust formatting. (basic_endpoint): Define _M_is_v6() to put all checks for AF_INET6 in one place. (basic_endpoint::resize): Simplify. (operator==(const tcp&, const tcp&)): Add constexpr and noexcept. (operator!=(const tcp&, const tcp&)): Likewise. (operator==(const udp&, const udp&)): Likewise. (operator!=(const udp&, const udp&)): Likewise. * testsuite/experimental/net/internet/tcp.cc: New test. * testsuite/experimental/net/internet/udp.cc: New test. Tested x86_64-linux. Committed to trunk. --P0j8di7fn9q3KBf5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 39073938b4e85fdbdc897c32e56fb5fc59ded9b5 Author: Jonathan Wakely Date: Tue Apr 27 13:43:23 2021 libstdc++: Minor refactoring in libstdc++-v3/ChangeLog: * include/experimental/internet (address_v6::bytes_type): Adjust formatting. (basic_endpoint): Define _M_is_v6() to put all checks for AF_INET6 in one place. (basic_endpoint::resize): Simplify. (operator==(const tcp&, const tcp&)): Add constexpr and noexcept. (operator!=(const tcp&, const tcp&)): Likewise. (operator==(const udp&, const udp&)): Likewise. (operator!=(const udp&, const udp&)): Likewise. * testsuite/experimental/net/internet/tcp.cc: New test. * testsuite/experimental/net/internet/udp.cc: New test. diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 6c3fad6d2aa..f6d6ef34504 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -273,8 +273,11 @@ namespace ip // types: struct bytes_type : array { - template explicit constexpr bytes_type(_Tp... __t) - : array{{static_cast(__t)...}} { } + template + explicit constexpr + bytes_type(_Tp... __t) + : array{{static_cast(__t)...}} + { } }; // constructors: @@ -1476,15 +1479,14 @@ namespace ip // members: constexpr protocol_type protocol() const noexcept { - return _M_data._M_v4.sin_family == AF_INET6 - ? protocol_type::v6() : protocol_type::v4(); + return _M_is_v6() ? protocol_type::v6() : protocol_type::v4(); } constexpr ip::address address() const noexcept { ip::address __addr; - if (protocol().family() == AF_INET6) + if (_M_is_v6()) { __builtin_memcpy(&__addr._M_v6._M_bytes, _M_data._M_v6.sin6_addr.s6_addr, 16); @@ -1525,18 +1527,16 @@ namespace ip { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); } void* data() noexcept { return &_M_data; } + const void* data() const noexcept { return &_M_data; } + constexpr size_t size() const noexcept - { - return protocol().family() == AF_INET6 - ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); - } + { return _M_is_v6() ? sizeof(sockaddr_in6) : sizeof(sockaddr_in); } void resize(size_t __s) { - if ((protocol().family() == AF_INET6 && __s != sizeof(sockaddr_in6)) - || (protocol().family() == AF_INET && __s != sizeof(sockaddr_in))) + if (__s != size()) __throw_length_error("net::ip::basic_endpoint::resize"); } @@ -1548,6 +1548,9 @@ namespace ip sockaddr_in _M_v4; sockaddr_in6 _M_v6; } _M_data; + + constexpr bool _M_is_v6() const noexcept + { return _M_data._M_v4.sin_family == AF_INET6; } }; /** basic_endpoint comparisons @@ -2136,12 +2139,12 @@ namespace ip * @{ */ - inline bool - operator==(const tcp& __a, const tcp& __b) + constexpr bool + operator==(const tcp& __a, const tcp& __b) noexcept { return __a.family() == __b.family(); } - inline bool - operator!=(const tcp& __a, const tcp& __b) + constexpr bool + operator!=(const tcp& __a, const tcp& __b) noexcept { return !(__a == __b); } /// @} @@ -2177,12 +2180,12 @@ namespace ip * @{ */ - inline bool - operator==(const udp& __a, const udp& __b) + constexpr bool + operator==(const udp& __a, const udp& __b) noexcept { return __a.family() == __b.family(); } - inline bool - operator!=(const udp& __a, const udp& __b) + constexpr bool + operator!=(const udp& __a, const udp& __b) noexcept { return !(__a == __b); } /// @} diff --git a/libstdc++-v3/testsuite/experimental/net/internet/tcp.cc b/libstdc++-v3/testsuite/experimental/net/internet/tcp.cc new file mode 100644 index 00000000000..87d042377a0 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/tcp.cc @@ -0,0 +1,45 @@ +// { dg-do compile { target c++14 } } + +#include +#include + +#if __has_include() +using namespace std; +using std::experimental::net::ip::tcp; +using std::experimental::net::ip::basic_endpoint; +using std::experimental::net::ip::basic_resolver; +using std::experimental::net::basic_stream_socket; +using std::experimental::net::basic_socket_acceptor; +using std::experimental::net::basic_socket_iostream; + +void +test01() +{ + static_assert( ! is_default_constructible(), "" ); + static_assert( is_nothrow_copy_constructible(), "" ); + static_assert( is_nothrow_copy_assignable(), "" ); + + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + + static_assert( tcp::v4() == tcp::v4(), "" ); + static_assert( tcp::v6() == tcp::v6(), "" ); + static_assert( tcp::v4() != tcp::v6(), "" ); + static_assert( tcp::v6() != tcp::v4(), "" ); + + static_assert( noexcept(tcp::v6() == tcp::v4()), "" ); + static_assert( noexcept(tcp::v6() != tcp::v4()), "" ); + + static_assert( tcp::v4().family() == AF_INET, "" ); + static_assert( tcp::v6().family() == AF_INET6, "" ); + + static_assert( tcp::v4().type() == SOCK_STREAM, "" ); + static_assert( tcp::v6().type() == SOCK_STREAM, "" ); + + static_assert( tcp::v4().protocol() == IPPROTO_TCP, "" ); + static_assert( tcp::v6().protocol() == IPPROTO_TCP, "" ); +} +#endif diff --git a/libstdc++-v3/testsuite/experimental/net/internet/udp.cc b/libstdc++-v3/testsuite/experimental/net/internet/udp.cc new file mode 100644 index 00000000000..d5f42c7575e --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/udp.cc @@ -0,0 +1,43 @@ +// { dg-do compile { target c++14 } } + +#include +#include + +#if __has_include() +using namespace std; +using std::experimental::net::ip::udp; +using std::experimental::net::ip::basic_endpoint; +using std::experimental::net::ip::basic_resolver; +using std::experimental::net::basic_datagram_socket; +using std::experimental::net::basic_socket_acceptor; +using std::experimental::net::basic_socket_iostream; + +void +test01() +{ + static_assert( ! is_default_constructible(), "" ); + static_assert( is_nothrow_copy_constructible(), "" ); + static_assert( is_nothrow_copy_assignable(), "" ); + + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + static_assert( is_same>(), ""); + + static_assert( udp::v4() == udp::v4(), "" ); + static_assert( udp::v6() == udp::v6(), "" ); + static_assert( udp::v4() != udp::v6(), "" ); + static_assert( udp::v6() != udp::v4(), "" ); + + static_assert( noexcept(udp::v6() == udp::v4()), "" ); + static_assert( noexcept(udp::v6() != udp::v4()), "" ); + + static_assert( udp::v4().family() == AF_INET, "" ); + static_assert( udp::v6().family() == AF_INET6, "" ); + + static_assert( udp::v4().type() == SOCK_DGRAM, "" ); + static_assert( udp::v6().type() == SOCK_DGRAM, "" ); + + static_assert( udp::v4().protocol() == IPPROTO_UDP, "" ); + static_assert( udp::v6().protocol() == IPPROTO_UDP, "" ); +} +#endif --P0j8di7fn9q3KBf5--