Add non-const string buffer() overloads.

This commit is contained in:
Christopher Kohlhoff 2014-10-06 09:43:28 +11:00
parent 4e6aecd8fd
commit bf61eaffef
2 changed files with 55 additions and 3 deletions

View File

@ -1226,6 +1226,55 @@ inline const_buffers_1 buffer(
));
}
/// Create a new modifiable buffer that represents the given string.
/**
* @returns <tt>mutable_buffers_1(data.size() ? &data[0] : 0,
* data.size() * sizeof(Elem))</tt>.
*
* @note The buffer is invalidated by any non-const operation called on the
* given string object.
*/
template <typename Elem, typename Traits, typename Allocator>
inline mutable_buffers_1 buffer(
std::basic_string<Elem, Traits, Allocator>& data)
{
return mutable_buffers_1(mutable_buffer(data.size() ? &data[0] : 0,
data.size() * sizeof(Elem)
#if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
, detail::buffer_debug_check<
typename std::basic_string<Elem, Traits, Allocator>::iterator
>(data.begin())
#endif // ASIO_ENABLE_BUFFER_DEBUGGING
));
}
/// Create a new non-modifiable buffer that represents the given string.
/**
* @returns A mutable_buffers_1 value equivalent to:
* @code mutable_buffers_1(
* data.size() ? &data[0] : 0,
* min(data.size() * sizeof(Elem), max_size_in_bytes)); @endcode
*
* @note The buffer is invalidated by any non-const operation called on the
* given string object.
*/
template <typename Elem, typename Traits, typename Allocator>
inline mutable_buffers_1 buffer(
std::basic_string<Elem, Traits, Allocator>& data,
std::size_t max_size_in_bytes)
{
return mutable_buffers_1(
mutable_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 std::basic_string<Elem, Traits, Allocator>::iterator
>(data.begin())
#endif // ASIO_ENABLE_BUFFER_DEBUGGING
));
}
/// Create a new non-modifiable buffer that represents the given string.
/**
* @returns <tt>const_buffers_1(data.data(), data.size() * sizeof(Elem))</tt>.

View File

@ -57,7 +57,8 @@ void test()
#endif // defined(ASIO_HAS_STD_ARRAY)
std::vector<char> vector_data(1024);
const std::vector<char>& const_vector_data = vector_data;
const std::string string_data(1024, ' ');
std::string string_data(1024, ' ');
const std::string const_string_data(1024, ' ');
std::vector<mutable_buffer> mutable_buffer_sequence;
std::vector<const_buffer> const_buffer_sequence;
@ -162,8 +163,10 @@ void test()
mb1 = buffer(vector_data, 1024);
cb1 = buffer(const_vector_data);
cb1 = buffer(const_vector_data, 1024);
cb1 = buffer(string_data);
cb1 = buffer(string_data, 1024);
mb1 = buffer(string_data);
mb1 = buffer(string_data, 1024);
cb1 = buffer(const_string_data);
cb1 = buffer(const_string_data, 1024);
// buffer_copy function overloads.