Skip to content

Commit

Permalink
Use std::string_view for C++17 or later, and std::experimental::strin…
Browse files Browse the repository at this point in the history
…g_view for C++14.

Define the preprocessor macro BOOST_ASIO_DISABLE_STD_STRING_VIEW to
force the use of std::experimental::string_view (assuming it is
available) when compiling in C++17 mode.
  • Loading branch information
chriskohlhoff committed Apr 1, 2018
1 parent 69a6480 commit b5b17a6
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 76 deletions.
4 changes: 2 additions & 2 deletions include/boost/asio/buffer.hpp
Expand Up @@ -1476,7 +1476,7 @@ inline BOOST_ASIO_CONST_BUFFER buffer(
);
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create a new modifiable buffer that represents the given string_view.
Expand Down Expand Up @@ -1521,7 +1521,7 @@ inline BOOST_ASIO_CONST_BUFFER buffer(
);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

/*@}*/
Expand Down
51 changes: 40 additions & 11 deletions include/boost/asio/detail/config.hpp
Expand Up @@ -771,32 +771,61 @@
# endif // !defined(BOOST_ASIO_DISABLE_STD_FUTURE)
#endif // !defined(BOOST_ASIO_HAS_STD_FUTURE)

// Standard library support for experimental::string_view.
// Standard library support for std::string_view.
#if !defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# if !defined(BOOST_ASIO_DISABLE_STD_STRING_VIEW)
# if defined(__clang__)
# if (__cplusplus >= 201703)
# if __has_include(<string_view>)
# define BOOST_ASIO_HAS_STD_STRING_VIEW 1
# endif // __has_include(<string_view>)
# endif // (__cplusplus >= 201703)
# endif // defined(__clang__)
# if defined(__GNUC__)
# if (__GNUC__ >= 7)
# if (__cplusplus >= 201703)
# define BOOST_ASIO_HAS_STD_STRING_VIEW 1
# endif // (__cplusplus >= 201703)
# endif // (__GNUC__ >= 7)
# endif // defined(__GNUC__)
# if defined(BOOST_ASIO_MSVC)
# if (_MSC_VER >= 1910 && _HAS_CXX17)
# define BOOST_ASIO_HAS_STD_STRING_VIEW
# endif // (_MSC_VER >= 1910 && _HAS_CXX17)
# endif // defined(BOOST_ASIO_MSVC)
# endif // !defined(BOOST_ASIO_DISABLE_STD_STRING_VIEW)
#endif // !defined(BOOST_ASIO_HAS_STD_STRING_VIEW)

// Standard library support for std::experimental::string_view.
#if !defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# if !defined(BOOST_ASIO_DISABLE_STD_EXPERIMENTAL_STRING_VIEW)
# if defined(__clang__)
# if (__cplusplus >= 201402)
# if __has_include(<experimental/string_view>)
# define BOOST_ASIO_HAS_STD_STRING_VIEW 1
# define BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW 1
# endif // __has_include(<experimental/string_view>)
# endif // (__cplusplus >= 201402)
# endif // defined(__clang__)
# if defined(__GNUC__)
# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)) || (__GNUC__ > 4)
# if (__cplusplus >= 201402)
# define BOOST_ASIO_HAS_STD_STRING_VIEW 1
# define BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW 1
# endif // (__cplusplus >= 201402)
# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)
# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)) || (__GNUC__ > 4)
# endif // defined(__GNUC__)
# if defined(BOOST_ASIO_MSVC)
# if (_MSC_VER >= 1910 && _HAS_CXX17)
# define BOOST_ASIO_HAS_STD_STRING_VIEW
# endif // (_MSC_VER >= 1910)
# endif // defined(BOOST_ASIO_MSVC)
# endif // !defined(BOOST_ASIO_DISABLE_STD_STRING_VIEW)
#endif // !defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# endif // !defined(BOOST_ASIO_DISABLE_STD_EXPERIMENTAL_STRING_VIEW)
#endif // !defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)

// Standard library has a string_view that we can use.
#if !defined(BOOST_ASIO_HAS_STRING_VIEW)
# if !defined(BOOST_ASIO_DISABLE_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# define BOOST_ASIO_HAS_STRING_VIEW 1
# elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# define BOOST_ASIO_HAS_STRING_VIEW 1
# endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# endif // !defined(BOOST_ASIO_DISABLE_STRING_VIEW)
#endif // !defined(BOOST_ASIO_HAS_STRING_VIEW)

// Standard library support for iostream move construction and assignment.
#if !defined(BOOST_ASIO_HAS_STD_IOSTREAM_MOVE)
Expand Down
24 changes: 13 additions & 11 deletions include/boost/asio/detail/string_view.hpp
Expand Up @@ -17,31 +17,33 @@

#include <boost/asio/detail/config.hpp>

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

#if defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# include <experimental/string_view>
#else // defined(BOOST_ASIO_HAS_EXPERIMENTAL_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# include <string_view>
#endif // defined(BOOST_ASIO_HAS_EXPERIMENTAL_STRING_VIEW)
#elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# include <experimental/string_view>
#else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# error BOOST_ASIO_HAS_STRING_VIEW is set but no string_view is available
#endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)

namespace boost {
namespace asio {

#if defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
using std::experimental::basic_string_view;
using std::experimental::string_view;
#else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
using std::basic_string_view;
using std::string_view;
#elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
using std::experimental::basic_string_view;
using std::experimental::string_view;
#endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)

} // namespace asio
} // namespace boost

# define BOOST_ASIO_STRING_VIEW_PARAM boost::asio::string_view
#else // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#else // defined(BOOST_ASIO_HAS_STRING_VIEW)
# define BOOST_ASIO_STRING_VIEW_PARAM const std::string&
#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

#endif // BOOST_ASIO_DETAIL_STRING_VIEW_HPP
4 changes: 2 additions & 2 deletions include/boost/asio/ip/address.hpp
Expand Up @@ -207,7 +207,7 @@ BOOST_ASIO_DECL address make_address(const std::string& str);
BOOST_ASIO_DECL address make_address(
const std::string& str, boost::system::error_code& ec);

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an address from an IPv4 address string in dotted decimal form,
Expand All @@ -225,7 +225,7 @@ BOOST_ASIO_DECL address make_address(string_view str);
BOOST_ASIO_DECL address make_address(
string_view str, boost::system::error_code& ec);

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

#if !defined(BOOST_ASIO_NO_IOSTREAM)
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/address_v4.hpp
Expand Up @@ -278,7 +278,7 @@ BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
BOOST_ASIO_DECL address_v4 make_address_v4(
const std::string& str, boost::system::error_code& ec);

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an IPv4 address from an IP address string in dotted decimal form.
Expand All @@ -294,7 +294,7 @@ BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
BOOST_ASIO_DECL address_v4 make_address_v4(
string_view str, boost::system::error_code& ec);

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

#if !defined(BOOST_ASIO_NO_IOSTREAM)
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/address_v6.hpp
Expand Up @@ -267,7 +267,7 @@ BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
BOOST_ASIO_DECL address_v6 make_address_v6(
const std::string& str, boost::system::error_code& ec);

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an IPv6 address from an IP address string.
Expand All @@ -283,7 +283,7 @@ BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
BOOST_ASIO_DECL address_v6 make_address_v6(
string_view str, boost::system::error_code& ec);

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

/// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/impl/address.ipp
Expand Up @@ -135,7 +135,7 @@ address make_address(const std::string& str,
return make_address(str.c_str(), ec);
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

address make_address(string_view str)
{
Expand All @@ -148,7 +148,7 @@ address make_address(string_view str,
return make_address(static_cast<std::string>(str), ec);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

boost::asio::ip::address_v4 address::to_v4() const
{
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/impl/address_v4.ipp
Expand Up @@ -188,7 +188,7 @@ address_v4 make_address_v4(
return make_address_v4(str.c_str(), ec);
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

address_v4 make_address_v4(string_view str)
{
Expand All @@ -201,7 +201,7 @@ address_v4 make_address_v4(string_view str,
return make_address_v4(static_cast<std::string>(str), ec);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

} // namespace ip
} // namespace asio
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/impl/address_v6.ipp
Expand Up @@ -304,7 +304,7 @@ address_v6 make_address_v6(
return make_address_v6(str.c_str(), ec);
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

address_v6 make_address_v6(string_view str)
{
Expand All @@ -317,7 +317,7 @@ address_v6 make_address_v6(string_view str,
return make_address_v6(static_cast<std::string>(str), ec);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

address_v4 make_address_v4(
v4_mapped_t, const address_v6& v6_addr)
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/impl/network_v4.ipp
Expand Up @@ -193,7 +193,7 @@ network_v4 make_network_v4(const std::string& str,
return network_v4(addr, static_cast<unsigned short>(prefix_len));
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

network_v4 make_network_v4(string_view str)
{
Expand All @@ -206,7 +206,7 @@ network_v4 make_network_v4(string_view str,
return make_network_v4(static_cast<std::string>(str), ec);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

} // namespace ip
} // namespace asio
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/impl/network_v6.ipp
Expand Up @@ -162,7 +162,7 @@ network_v6 make_network_v6(const std::string& str,
return network_v6(addr, static_cast<unsigned short>(prefix_len));
}

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)

network_v6 make_network_v6(string_view str)
{
Expand All @@ -175,7 +175,7 @@ network_v6 make_network_v6(string_view str,
return make_network_v6(static_cast<std::string>(str), ec);
}

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

} // namespace ip
} // namespace asio
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/network_v4.hpp
Expand Up @@ -208,7 +208,7 @@ BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str);
BOOST_ASIO_DECL network_v4 make_network_v4(
const std::string& str, boost::system::error_code& ec);

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an IPv4 network from a string containing IP address and prefix
Expand All @@ -226,7 +226,7 @@ BOOST_ASIO_DECL network_v4 make_network_v4(string_view str);
BOOST_ASIO_DECL network_v4 make_network_v4(
string_view str, boost::system::error_code& ec);

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

#if !defined(BOOST_ASIO_NO_IOSTREAM)
Expand Down
4 changes: 2 additions & 2 deletions include/boost/asio/ip/network_v6.hpp
Expand Up @@ -182,7 +182,7 @@ BOOST_ASIO_DECL network_v6 make_network_v6(const std::string& str);
BOOST_ASIO_DECL network_v6 make_network_v6(
const std::string& str, boost::system::error_code& ec);

#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW) \
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an IPv6 network from a string containing IP address and prefix
Expand All @@ -200,7 +200,7 @@ BOOST_ASIO_DECL network_v6 make_network_v6(string_view str);
BOOST_ASIO_DECL network_v6 make_network_v6(
string_view str, boost::system::error_code& ec);

#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

#if !defined(BOOST_ASIO_NO_IOSTREAM)
Expand Down
12 changes: 5 additions & 7 deletions test/buffer.cpp
Expand Up @@ -62,12 +62,10 @@ void test()
std::vector<mutable_buffer> mutable_buffer_sequence;
std::vector<const_buffer> const_buffer_sequence;
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_data(string_data);
# else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::string_view string_view_data(string_data);
# endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_data(string_data);
#endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)

// mutable_buffer constructors.

Expand Down Expand Up @@ -204,10 +202,10 @@ void test()
mb1 = buffer(string_data, 1024);
cb1 = buffer(const_string_data);
cb1 = buffer(const_string_data, 1024);
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)
cb1 = buffer(string_view_data);
cb1 = buffer(string_view_data, 1024);
#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

// buffer_copy function overloads.

Expand Down
10 changes: 5 additions & 5 deletions test/ip/address.cpp
Expand Up @@ -108,15 +108,15 @@ void test()
addr1 = ip::make_address("127.0.0.1", ec);
addr1 = ip::make_address(string_value);
addr1 = ip::make_address(string_value, ec);
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
std::string_view string_view_value("127.0.0.1");
# elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
addr1 = ip::make_address(string_view_value);
addr1 = ip::make_address(string_view_value, ec);
#endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

// address I/O.

Expand Down
10 changes: 5 additions & 5 deletions test/ip/address_v4.cpp
Expand Up @@ -132,15 +132,15 @@ void test()
addr1 = ip::make_address_v4("127.0.0.1", ec);
addr1 = ip::make_address_v4(string_value);
addr1 = ip::make_address_v4(string_value, ec);
#if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
#if defined(BOOST_ASIO_HAS_STRING_VIEW)
# if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
std::string_view string_view_value("127.0.0.1");
# elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_value("127.0.0.1");
# endif // defined(BOOST_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(BOOST_ASIO_HAS_STD_STRING_VIEW)
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)

// address_v4 I/O.

Expand Down

0 comments on commit b5b17a6

Please sign in to comment.