Add non-const string buffer() overloads.
diff --git a/asio/include/asio/buffer.hpp b/asio/include/asio/buffer.hpp index b275869..2cc06bb 100644 --- a/asio/include/asio/buffer.hpp +++ b/asio/include/asio/buffer.hpp
@@ -1226,6 +1226,55 @@ )); } +/// 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>.
diff --git a/asio/src/tests/unit/buffer.cpp b/asio/src/tests/unit/buffer.cpp index 8c64524..0ba4317 100644 --- a/asio/src/tests/unit/buffer.cpp +++ b/asio/src/tests/unit/buffer.cpp
@@ -57,7 +57,8 @@ #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 @@ 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.