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.
commit 39073938b4e85fdbdc897c32e56fb5fc59ded9b5 Author: Jonathan Wakely <jwak...@redhat.com> Date: Tue Apr 27 13:43:23 2021 libstdc++: Minor refactoring in <experimental/internet> 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<unsigned char, 16> { - template<typename... _Tp> explicit constexpr bytes_type(_Tp... __t) - : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}} { } + template<typename... _Tp> + explicit constexpr + bytes_type(_Tp... __t) + : array<unsigned char, 16>{{static_cast<unsigned char>(__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 <experimental/internet> +#include <type_traits> + +#if __has_include(<netinet/in.h>) +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<tcp>(), "" ); + static_assert( is_nothrow_copy_constructible<tcp>(), "" ); + static_assert( is_nothrow_copy_assignable<tcp>(), "" ); + + static_assert( is_same<tcp::endpoint, basic_endpoint<tcp>>(), ""); + static_assert( is_same<tcp::resolver, basic_resolver<tcp>>(), ""); + static_assert( is_same<tcp::socket, basic_stream_socket<tcp>>(), ""); + static_assert( is_same<tcp::acceptor, basic_socket_acceptor<tcp>>(), ""); + static_assert( is_same<tcp::iostream, basic_socket_iostream<tcp>>(), ""); + + 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 <experimental/internet> +#include <type_traits> + +#if __has_include(<netinet/in.h>) +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<udp>(), "" ); + static_assert( is_nothrow_copy_constructible<udp>(), "" ); + static_assert( is_nothrow_copy_assignable<udp>(), "" ); + + static_assert( is_same<udp::endpoint, basic_endpoint<udp>>(), ""); + static_assert( is_same<udp::resolver, basic_resolver<udp>>(), ""); + static_assert( is_same<udp::socket, basic_datagram_socket<udp>>(), ""); + + 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