Add buffer() overloads for basic_string_view.

This commit is contained in:
Christopher Kohlhoff 2016-09-21 17:13:07 +10:00
parent 69a03d7b6b
commit 0d7abb65fe
5 changed files with 125 additions and 0 deletions

View File

@ -217,6 +217,7 @@ nobase_include_HEADERS = \
asio/detail/std_thread.hpp \
asio/detail/strand_executor_service.hpp \
asio/detail/strand_service.hpp \
asio/detail/string_view.hpp \
asio/detail/thread_context.hpp \
asio/detail/thread_group.hpp \
asio/detail/thread.hpp \

View File

@ -24,6 +24,7 @@
#include <vector>
#include "asio/detail/array_fwd.hpp"
#include "asio/detail/is_buffer_sequence.hpp"
#include "asio/detail/string_view.hpp"
#include "asio/detail/throw_exception.hpp"
#include "asio/detail/type_traits.hpp"
@ -1474,6 +1475,54 @@ inline ASIO_CONST_BUFFER buffer(
);
}
#if defined(ASIO_HAS_STD_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create a new modifiable buffer that represents the given string_view.
/**
* @returns <tt>mutable_buffer(data.size() ? &data[0] : 0,
* data.size() * sizeof(Elem))</tt>.
*/
template <typename Elem, typename Traits>
inline ASIO_CONST_BUFFER buffer(
basic_string_view<Elem, Traits> data) ASIO_NOEXCEPT
{
return ASIO_CONST_BUFFER(data.size() ? &data[0] : 0,
data.size() * sizeof(Elem)
#if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
, detail::buffer_debug_check<
typename basic_string_view<Elem, Traits>::iterator
>(data.begin())
#endif // ASIO_ENABLE_BUFFER_DEBUGGING
);
}
/// Create a new non-modifiable buffer that represents the given string.
/**
* @returns A mutable_buffer value equivalent to:
* @code mutable_buffer(
* data.size() ? &data[0] : 0,
* min(data.size() * sizeof(Elem), max_size_in_bytes)); @endcode
*/
template <typename Elem, typename Traits>
inline ASIO_CONST_BUFFER buffer(
basic_string_view<Elem, Traits> data,
std::size_t max_size_in_bytes) ASIO_NOEXCEPT
{
return ASIO_CONST_BUFFER(data.size() ? &data[0] : 0,
data.size() * sizeof(Elem) < max_size_in_bytes
? data.size() * sizeof(Elem) : max_size_in_bytes
#if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
, detail::buffer_debug_check<
typename basic_string_view<Elem, Traits>::iterator
>(data.begin())
#endif // ASIO_ENABLE_BUFFER_DEBUGGING
);
}
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
/*@}*/
/// Adapt a basic_string to the DynamicBuffer requirements.

View File

@ -703,6 +703,28 @@
# endif // !defined(ASIO_DISABLE_STD_FUTURE)
#endif // !defined(ASIO_HAS_STD_FUTURE)
// Standard library support for experimental::string_view.
#if !defined(ASIO_HAS_STD_STRING_VIEW)
# if !defined(ASIO_DISABLE_STD_STRING_VIEW)
# if defined(__clang__)
# if (__cplusplus >= 201103)
# if __has_include(<experimental/string_view>)
# define ASIO_HAS_STD_STRING_VIEW 1
# define ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW 1
# endif // __has_include(<experimental/string_view>)
# endif // (__cplusplus >= 201103)
# endif // defined(__clang__)
# if defined(__GNUC__)
# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)) || (__GNUC__ > 4)
# if (__cplusplus >= 201300)
# define ASIO_HAS_STD_STRING_VIEW 1
# define ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW 1
# endif // (__cplusplus >= 201300)
# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)
# endif // defined(__GNUC__)
# endif // !defined(ASIO_DISABLE_STD_STRING_VIEW)
#endif // !defined(ASIO_HAS_STD_STRING_VIEW)
// Windows App target. Windows but with a limited API.
#if !defined(ASIO_WINDOWS_APP)
# if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0603)

View File

@ -0,0 +1,42 @@
//
// detail/string_view.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_DETAIL_STRING_VIEW_HPP
#define ASIO_DETAIL_STRING_VIEW_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if defined(ASIO_HAS_STD_STRING_VIEW)
#include "asio/detail/config.hpp"
#if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
# include <experimental/string_view>
#else // defined(ASIO_HAS_EXPERIMENTAL_STRING_VIEW)
# include <string_view>
#endif // defined(ASIO_HAS_EXPERIMENTAL_STRING_VIEW)
namespace asio {
#if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
using std::experimental::basic_string_view;
using std::experimental::string_view;
#else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
using std::basic_string_view;
using std::string_view;
#endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
} // namespace asio
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
#endif // ASIO_DETAIL_STRING_VIEW_HPP

View File

@ -61,6 +61,13 @@ void test()
const std::string const_string_data(1024, ' ');
std::vector<mutable_buffer> mutable_buffer_sequence;
std::vector<const_buffer> const_buffer_sequence;
#if defined(ASIO_HAS_STD_STRING_VIEW)
# if defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::experimental::string_view string_view_data(string_data);
# else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
std::string_view string_view_data(string_data);
# endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// mutable_buffer constructors.
@ -197,6 +204,10 @@ void test()
mb1 = buffer(string_data, 1024);
cb1 = buffer(const_string_data);
cb1 = buffer(const_string_data, 1024);
#if defined(ASIO_HAS_STD_STRING_VIEW)
cb1 = buffer(string_view_data);
cb1 = buffer(string_view_data, 1024);
#endif // defined(ASIO_HAS_STD_STRING_VIEW)
// buffer_copy function overloads.