Add string_view overloads for the make_address*() functions.

This commit is contained in:
Christopher Kohlhoff 2016-09-22 09:35:07 +10:00
parent 9c3c09ab68
commit 41cf38be81
9 changed files with 134 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include "asio/detail/config.hpp" #include "asio/detail/config.hpp"
#include <string> #include <string>
#include "asio/detail/throw_exception.hpp" #include "asio/detail/throw_exception.hpp"
#include "asio/detail/string_view.hpp"
#include "asio/detail/type_traits.hpp" #include "asio/detail/type_traits.hpp"
#include "asio/error_code.hpp" #include "asio/error_code.hpp"
#include "asio/ip/address_v4.hpp" #include "asio/ip/address_v4.hpp"
@ -205,6 +206,27 @@ ASIO_DECL address make_address(const std::string& str);
ASIO_DECL address make_address( ASIO_DECL address make_address(
const std::string& str, asio::error_code& ec); const std::string& str, asio::error_code& ec);
#if defined(ASIO_HAS_STD_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
ASIO_DECL address make_address(string_view str);
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
ASIO_DECL address make_address(
string_view str, asio::error_code& ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(ASIO_NO_IOSTREAM) #if !defined(ASIO_NO_IOSTREAM)
/// Output an address as a string. /// Output an address as a string.

View File

@ -20,6 +20,7 @@
#include "asio/detail/array.hpp" #include "asio/detail/array.hpp"
#include "asio/detail/cstdint.hpp" #include "asio/detail/cstdint.hpp"
#include "asio/detail/socket_types.hpp" #include "asio/detail/socket_types.hpp"
#include "asio/detail/string_view.hpp"
#include "asio/detail/winsock_init.hpp" #include "asio/detail/winsock_init.hpp"
#include "asio/error_code.hpp" #include "asio/error_code.hpp"
@ -276,6 +277,25 @@ ASIO_DECL address_v4 make_address_v4(const std::string& str);
ASIO_DECL address_v4 make_address_v4( ASIO_DECL address_v4 make_address_v4(
const std::string& str, asio::error_code& ec); const std::string& str, asio::error_code& ec);
#if defined(ASIO_HAS_STD_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
ASIO_DECL address_v4 make_address_v4(string_view str);
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
ASIO_DECL address_v4 make_address_v4(
string_view str, asio::error_code& ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(ASIO_NO_IOSTREAM) #if !defined(ASIO_NO_IOSTREAM)
/// Output an address as a string. /// Output an address as a string.

View File

@ -19,6 +19,7 @@
#include <string> #include <string>
#include "asio/detail/array.hpp" #include "asio/detail/array.hpp"
#include "asio/detail/socket_types.hpp" #include "asio/detail/socket_types.hpp"
#include "asio/detail/string_view.hpp"
#include "asio/detail/winsock_init.hpp" #include "asio/detail/winsock_init.hpp"
#include "asio/error_code.hpp" #include "asio/error_code.hpp"
#include "asio/ip/address_v4.hpp" #include "asio/ip/address_v4.hpp"
@ -265,6 +266,25 @@ ASIO_DECL address_v6 make_address_v6(const std::string& str);
ASIO_DECL address_v6 make_address_v6( ASIO_DECL address_v6 make_address_v6(
const std::string& str, asio::error_code& ec); const std::string& str, asio::error_code& ec);
#if defined(ASIO_HAS_STD_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
ASIO_DECL address_v6 make_address_v6(string_view str);
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
ASIO_DECL address_v6 make_address_v6(
string_view str, asio::error_code& ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
/// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6 /// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
/// addresses. /// addresses.
enum v4_mapped_t { v4_mapped }; enum v4_mapped_t { v4_mapped };

View File

@ -134,6 +134,21 @@ address make_address(const std::string& str,
return make_address(str.c_str(), ec); return make_address(str.c_str(), ec);
} }
#if defined(ASIO_HAS_STD_STRING_VIEW)
address make_address(string_view str)
{
return make_address(static_cast<std::string>(str));
}
address make_address(string_view str,
asio::error_code& ec)
{
return make_address(static_cast<std::string>(str), ec);
}
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
asio::ip::address_v4 address::to_v4() const asio::ip::address_v4 address::to_v4() const
{ {
if (type_ != ipv4) if (type_ != ipv4)

View File

@ -187,6 +187,21 @@ address_v4 make_address_v4(
return make_address_v4(str.c_str(), ec); return make_address_v4(str.c_str(), ec);
} }
#if defined(ASIO_HAS_STD_STRING_VIEW)
address_v4 make_address_v4(string_view str)
{
return make_address_v4(static_cast<std::string>(str));
}
address_v4 make_address_v4(string_view str,
asio::error_code& ec)
{
return make_address_v4(static_cast<std::string>(str), ec);
}
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
} // namespace ip } // namespace ip
} // namespace asio } // namespace asio

View File

@ -303,6 +303,21 @@ address_v6 make_address_v6(
return make_address_v6(str.c_str(), ec); return make_address_v6(str.c_str(), ec);
} }
#if defined(ASIO_HAS_STD_STRING_VIEW)
address_v6 make_address_v6(string_view str)
{
return make_address_v6(static_cast<std::string>(str));
}
address_v6 make_address_v6(string_view str,
asio::error_code& ec)
{
return make_address_v6(static_cast<std::string>(str), ec);
}
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
address_v4 make_address_v4( address_v4 make_address_v4(
v4_mapped_t, const address_v6& v6_addr) v4_mapped_t, const address_v6& v6_addr)
{ {

View File

@ -108,6 +108,15 @@ void test()
addr1 = ip::make_address("127.0.0.1", ec); addr1 = ip::make_address("127.0.0.1", ec);
addr1 = ip::make_address(string_value); addr1 = ip::make_address(string_value);
addr1 = ip::make_address(string_value, ec); addr1 = ip::make_address(string_value, ec);
#if defined(ASIO_HAS_STD_STRING_VIEW)
# if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::string_view string_view_value("127.0.0.1");
# endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
addr1 = ip::make_address(string_view_value);
addr1 = ip::make_address(string_view_value, ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// address I/O. // address I/O.

View File

@ -132,6 +132,15 @@ void test()
addr1 = ip::make_address_v4("127.0.0.1", ec); addr1 = ip::make_address_v4("127.0.0.1", ec);
addr1 = ip::make_address_v4(string_value); addr1 = ip::make_address_v4(string_value);
addr1 = ip::make_address_v4(string_value, ec); addr1 = ip::make_address_v4(string_value, ec);
#if defined(ASIO_HAS_STD_STRING_VIEW)
# if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::string_view string_view_value("127.0.0.1");
# endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
addr1 = ip::make_address_v4(string_view_value);
addr1 = ip::make_address_v4(string_view_value, ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// address_v4 I/O. // address_v4 I/O.

View File

@ -144,6 +144,15 @@ void test()
addr1 = ip::make_address_v6("0::0", ec); addr1 = ip::make_address_v6("0::0", ec);
addr1 = ip::make_address_v6(string_value); addr1 = ip::make_address_v6(string_value);
addr1 = ip::make_address_v6(string_value, ec); addr1 = ip::make_address_v6(string_value, ec);
#if defined(ASIO_HAS_STD_STRING_VIEW)
# if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("0::0");
# else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::string_view string_view_value("0::0");
# endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
addr1 = ip::make_address_v6(string_view_value);
addr1 = ip::make_address_v6(string_view_value, ec);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// address_v6 IPv4-mapped conversion. // address_v6 IPv4-mapped conversion.
#if defined(ASIO_NO_DEPRECATED) #if defined(ASIO_NO_DEPRECATED)