Chris@16: // Chris@16: // buffer.hpp Chris@16: // ~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_BUFFER_HPP Chris@16: #define BOOST_ASIO_BUFFER_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_MSVC) Chris@16: # if defined(_HAS_ITERATOR_DEBUGGING) && (_HAS_ITERATOR_DEBUGGING != 0) Chris@16: # if !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING) Chris@16: # define BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: # endif // !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING) Chris@16: # endif // defined(_HAS_ITERATOR_DEBUGGING) Chris@16: #endif // defined(BOOST_ASIO_MSVC) Chris@16: Chris@16: #if defined(__GNUC__) Chris@16: # if defined(_GLIBCXX_DEBUG) Chris@16: # if !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING) Chris@16: # define BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: # endif // !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING) Chris@16: # endif // defined(_GLIBCXX_DEBUG) Chris@16: #endif // defined(__GNUC__) Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: # include Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_BOOST_WORKAROUND) Chris@16: # include Chris@16: # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) \ Chris@16: || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590)) Chris@16: # define BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND Chris@16: # endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) Chris@16: // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590)) Chris@16: #endif // defined(BOOST_ASIO_HAS_BOOST_WORKAROUND) Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: class mutable_buffer; Chris@16: class const_buffer; Chris@16: Chris@16: namespace detail { Chris@16: void* buffer_cast_helper(const mutable_buffer&); Chris@16: const void* buffer_cast_helper(const const_buffer&); Chris@16: std::size_t buffer_size_helper(const mutable_buffer&); Chris@16: std::size_t buffer_size_helper(const const_buffer&); Chris@16: } // namespace detail Chris@16: Chris@16: /// Holds a buffer that can be modified. Chris@16: /** Chris@16: * The mutable_buffer class provides a safe representation of a buffer that can Chris@16: * be modified. It does not own the underlying data, and so is cheap to copy or Chris@16: * assign. Chris@16: * Chris@16: * @par Accessing Buffer Contents Chris@16: * Chris@16: * The contents of a buffer may be accessed using the @ref buffer_size Chris@16: * and @ref buffer_cast functions: Chris@16: * Chris@16: * @code boost::asio::mutable_buffer b1 = ...; Chris@16: * std::size_t s1 = boost::asio::buffer_size(b1); Chris@16: * unsigned char* p1 = boost::asio::buffer_cast(b1); Chris@16: * @endcode Chris@16: * Chris@16: * The boost::asio::buffer_cast function permits violations of type safety, so Chris@16: * uses of it in application code should be carefully considered. Chris@16: */ Chris@16: class mutable_buffer Chris@16: { Chris@16: public: Chris@16: /// Construct an empty buffer. Chris@16: mutable_buffer() Chris@16: : data_(0), Chris@16: size_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a buffer to represent a given memory range. Chris@16: mutable_buffer(void* data, std::size_t size) Chris@16: : data_(data), Chris@16: size_(size) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: mutable_buffer(void* data, std::size_t size, Chris@16: boost::asio::detail::function debug_check) Chris@16: : data_(data), Chris@16: size_(size), Chris@16: debug_check_(debug_check) Chris@16: { Chris@16: } Chris@16: Chris@16: const boost::asio::detail::function& get_debug_check() const Chris@16: { Chris@16: return debug_check_; Chris@16: } Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: Chris@16: private: Chris@16: friend void* boost::asio::detail::buffer_cast_helper( Chris@16: const mutable_buffer& b); Chris@16: friend std::size_t boost::asio::detail::buffer_size_helper( Chris@16: const mutable_buffer& b); Chris@16: Chris@16: void* data_; Chris@16: std::size_t size_; Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: boost::asio::detail::function debug_check_; Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: inline void* buffer_cast_helper(const mutable_buffer& b) Chris@16: { Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: if (b.size_ && b.debug_check_) Chris@16: b.debug_check_(); Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: return b.data_; Chris@16: } Chris@16: Chris@16: inline std::size_t buffer_size_helper(const mutable_buffer& b) Chris@16: { Chris@16: return b.size_; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: /// Adapts a single modifiable buffer so that it meets the requirements of the Chris@16: /// MutableBufferSequence concept. Chris@16: class mutable_buffers_1 Chris@16: : public mutable_buffer Chris@16: { Chris@16: public: Chris@16: /// The type for each element in the list of buffers. Chris@16: typedef mutable_buffer value_type; Chris@16: Chris@16: /// A random-access iterator type that may be used to read elements. Chris@16: typedef const mutable_buffer* const_iterator; Chris@16: Chris@16: /// Construct to represent a given memory range. Chris@16: mutable_buffers_1(void* data, std::size_t size) Chris@16: : mutable_buffer(data, size) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct to represent a single modifiable buffer. Chris@16: explicit mutable_buffers_1(const mutable_buffer& b) Chris@16: : mutable_buffer(b) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Get a random-access iterator to the first element. Chris@16: const_iterator begin() const Chris@16: { Chris@16: return this; Chris@16: } Chris@16: Chris@16: /// Get a random-access iterator for one past the last element. Chris@16: const_iterator end() const Chris@16: { Chris@16: return begin() + 1; Chris@16: } Chris@16: }; Chris@16: Chris@16: /// Holds a buffer that cannot be modified. Chris@16: /** Chris@16: * The const_buffer class provides a safe representation of a buffer that cannot Chris@16: * be modified. It does not own the underlying data, and so is cheap to copy or Chris@16: * assign. Chris@16: * Chris@16: * @par Accessing Buffer Contents Chris@16: * Chris@16: * The contents of a buffer may be accessed using the @ref buffer_size Chris@16: * and @ref buffer_cast functions: Chris@16: * Chris@16: * @code boost::asio::const_buffer b1 = ...; Chris@16: * std::size_t s1 = boost::asio::buffer_size(b1); Chris@16: * const unsigned char* p1 = boost::asio::buffer_cast(b1); Chris@16: * @endcode Chris@16: * Chris@16: * The boost::asio::buffer_cast function permits violations of type safety, so Chris@16: * uses of it in application code should be carefully considered. Chris@16: */ Chris@16: class const_buffer Chris@16: { Chris@16: public: Chris@16: /// Construct an empty buffer. Chris@16: const_buffer() Chris@16: : data_(0), Chris@16: size_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a buffer to represent a given memory range. Chris@16: const_buffer(const void* data, std::size_t size) Chris@16: : data_(data), Chris@16: size_(size) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a non-modifiable buffer from a modifiable one. Chris@16: const_buffer(const mutable_buffer& b) Chris@16: : data_(boost::asio::detail::buffer_cast_helper(b)), Chris@16: size_(boost::asio::detail::buffer_size_helper(b)) Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , debug_check_(b.get_debug_check()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: const_buffer(const void* data, std::size_t size, Chris@16: boost::asio::detail::function debug_check) Chris@16: : data_(data), Chris@16: size_(size), Chris@16: debug_check_(debug_check) Chris@16: { Chris@16: } Chris@16: Chris@16: const boost::asio::detail::function& get_debug_check() const Chris@16: { Chris@16: return debug_check_; Chris@16: } Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: Chris@16: private: Chris@16: friend const void* boost::asio::detail::buffer_cast_helper( Chris@16: const const_buffer& b); Chris@16: friend std::size_t boost::asio::detail::buffer_size_helper( Chris@16: const const_buffer& b); Chris@16: Chris@16: const void* data_; Chris@16: std::size_t size_; Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: boost::asio::detail::function debug_check_; Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: inline const void* buffer_cast_helper(const const_buffer& b) Chris@16: { Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: if (b.size_ && b.debug_check_) Chris@16: b.debug_check_(); Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: return b.data_; Chris@16: } Chris@16: Chris@16: inline std::size_t buffer_size_helper(const const_buffer& b) Chris@16: { Chris@16: return b.size_; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: /// Adapts a single non-modifiable buffer so that it meets the requirements of Chris@16: /// the ConstBufferSequence concept. Chris@16: class const_buffers_1 Chris@16: : public const_buffer Chris@16: { Chris@16: public: Chris@16: /// The type for each element in the list of buffers. Chris@16: typedef const_buffer value_type; Chris@16: Chris@16: /// A random-access iterator type that may be used to read elements. Chris@16: typedef const const_buffer* const_iterator; Chris@16: Chris@16: /// Construct to represent a given memory range. Chris@16: const_buffers_1(const void* data, std::size_t size) Chris@16: : const_buffer(data, size) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct to represent a single non-modifiable buffer. Chris@16: explicit const_buffers_1(const const_buffer& b) Chris@16: : const_buffer(b) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Get a random-access iterator to the first element. Chris@16: const_iterator begin() const Chris@16: { Chris@16: return this; Chris@16: } Chris@16: Chris@16: /// Get a random-access iterator for one past the last element. Chris@16: const_iterator end() const Chris@16: { Chris@16: return begin() + 1; Chris@16: } Chris@16: }; Chris@16: Chris@16: /// An implementation of both the ConstBufferSequence and MutableBufferSequence Chris@16: /// concepts to represent a null buffer sequence. Chris@16: class null_buffers Chris@16: { Chris@16: public: Chris@16: /// The type for each element in the list of buffers. Chris@16: typedef mutable_buffer value_type; Chris@16: Chris@16: /// A random-access iterator type that may be used to read elements. Chris@16: typedef const mutable_buffer* const_iterator; Chris@16: Chris@16: /// Get a random-access iterator to the first element. Chris@16: const_iterator begin() const Chris@16: { Chris@16: return &buf_; Chris@16: } Chris@16: Chris@16: /// Get a random-access iterator for one past the last element. Chris@16: const_iterator end() const Chris@16: { Chris@16: return &buf_; Chris@16: } Chris@16: Chris@16: private: Chris@16: mutable_buffer buf_; Chris@16: }; Chris@16: Chris@16: /** @defgroup buffer_size boost::asio::buffer_size Chris@16: * Chris@16: * @brief The boost::asio::buffer_size function determines the total number of Chris@16: * bytes in a buffer or buffer sequence. Chris@16: */ Chris@16: /*@{*/ Chris@16: Chris@16: /// Get the number of bytes in a modifiable buffer. Chris@16: inline std::size_t buffer_size(const mutable_buffer& b) Chris@16: { Chris@16: return detail::buffer_size_helper(b); Chris@16: } Chris@16: Chris@16: /// Get the number of bytes in a modifiable buffer. Chris@16: inline std::size_t buffer_size(const mutable_buffers_1& b) Chris@16: { Chris@16: return detail::buffer_size_helper(b); Chris@16: } Chris@16: Chris@16: /// Get the number of bytes in a non-modifiable buffer. Chris@16: inline std::size_t buffer_size(const const_buffer& b) Chris@16: { Chris@16: return detail::buffer_size_helper(b); Chris@16: } Chris@16: Chris@16: /// Get the number of bytes in a non-modifiable buffer. Chris@16: inline std::size_t buffer_size(const const_buffers_1& b) Chris@16: { Chris@16: return detail::buffer_size_helper(b); Chris@16: } Chris@16: Chris@16: /// Get the total number of bytes in a buffer sequence. Chris@16: /** Chris@16: * The @c BufferSequence template parameter may meet either of the @c Chris@16: * ConstBufferSequence or @c MutableBufferSequence type requirements. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_size(const BufferSequence& b) Chris@16: { Chris@16: std::size_t total_buffer_size = 0; Chris@16: Chris@16: typename BufferSequence::const_iterator iter = b.begin(); Chris@16: typename BufferSequence::const_iterator end = b.end(); Chris@16: for (; iter != end; ++iter) Chris@16: total_buffer_size += detail::buffer_size_helper(*iter); Chris@16: Chris@16: return total_buffer_size; Chris@16: } Chris@16: Chris@16: /*@}*/ Chris@16: Chris@16: /** @defgroup buffer_cast boost::asio::buffer_cast Chris@16: * Chris@16: * @brief The boost::asio::buffer_cast function is used to obtain a pointer to Chris@16: * the underlying memory region associated with a buffer. Chris@16: * Chris@16: * @par Examples: Chris@16: * Chris@16: * To access the memory of a non-modifiable buffer, use: Chris@16: * @code boost::asio::const_buffer b1 = ...; Chris@16: * const unsigned char* p1 = boost::asio::buffer_cast(b1); Chris@16: * @endcode Chris@16: * Chris@16: * To access the memory of a modifiable buffer, use: Chris@16: * @code boost::asio::mutable_buffer b2 = ...; Chris@16: * unsigned char* p2 = boost::asio::buffer_cast(b2); Chris@16: * @endcode Chris@16: * Chris@16: * The boost::asio::buffer_cast function permits violations of type safety, so Chris@16: * uses of it in application code should be carefully considered. Chris@16: */ Chris@16: /*@{*/ Chris@16: Chris@16: /// Cast a non-modifiable buffer to a specified pointer to POD type. Chris@16: template Chris@16: inline PointerToPodType buffer_cast(const mutable_buffer& b) Chris@16: { Chris@16: return static_cast(detail::buffer_cast_helper(b)); Chris@16: } Chris@16: Chris@16: /// Cast a non-modifiable buffer to a specified pointer to POD type. Chris@16: template Chris@16: inline PointerToPodType buffer_cast(const const_buffer& b) Chris@16: { Chris@16: return static_cast(detail::buffer_cast_helper(b)); Chris@16: } Chris@16: Chris@16: /*@}*/ Chris@16: Chris@16: /// Create a new modifiable buffer that is offset from the start of another. Chris@16: /** Chris@16: * @relates mutable_buffer Chris@16: */ Chris@16: inline mutable_buffer operator+(const mutable_buffer& b, std::size_t start) Chris@16: { Chris@16: if (start > buffer_size(b)) Chris@16: return mutable_buffer(); Chris@16: char* new_data = buffer_cast(b) + start; Chris@16: std::size_t new_size = buffer_size(b) - start; Chris@16: return mutable_buffer(new_data, new_size Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: ); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that is offset from the start of another. Chris@16: /** Chris@16: * @relates mutable_buffer Chris@16: */ Chris@16: inline mutable_buffer operator+(std::size_t start, const mutable_buffer& b) Chris@16: { Chris@16: if (start > buffer_size(b)) Chris@16: return mutable_buffer(); Chris@16: char* new_data = buffer_cast(b) + start; Chris@16: std::size_t new_size = buffer_size(b) - start; Chris@16: return mutable_buffer(new_data, new_size Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: ); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that is offset from the start of another. Chris@16: /** Chris@16: * @relates const_buffer Chris@16: */ Chris@16: inline const_buffer operator+(const const_buffer& b, std::size_t start) Chris@16: { Chris@16: if (start > buffer_size(b)) Chris@16: return const_buffer(); Chris@16: const char* new_data = buffer_cast(b) + start; Chris@16: std::size_t new_size = buffer_size(b) - start; Chris@16: return const_buffer(new_data, new_size Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: ); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that is offset from the start of another. Chris@16: /** Chris@16: * @relates const_buffer Chris@16: */ Chris@16: inline const_buffer operator+(std::size_t start, const const_buffer& b) Chris@16: { Chris@16: if (start > buffer_size(b)) Chris@16: return const_buffer(); Chris@16: const char* new_data = buffer_cast(b) + start; Chris@16: std::size_t new_size = buffer_size(b) - start; Chris@16: return const_buffer(new_data, new_size Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: ); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: class buffer_debug_check Chris@16: { Chris@16: public: Chris@16: buffer_debug_check(Iterator iter) Chris@16: : iter_(iter) Chris@16: { Chris@16: } Chris@16: Chris@16: ~buffer_debug_check() Chris@16: { Chris@16: #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC == 1400) Chris@16: // MSVC 8's string iterator checking may crash in a std::string::iterator Chris@16: // object's destructor when the iterator points to an already-destroyed Chris@16: // std::string object, unless the iterator is cleared first. Chris@16: iter_ = Iterator(); Chris@16: #endif // defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC == 1400) Chris@16: } Chris@16: Chris@16: void operator()() Chris@16: { Chris@16: *iter_; Chris@16: } Chris@16: Chris@16: private: Chris@16: Iterator iter_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: Chris@16: /** @defgroup buffer boost::asio::buffer Chris@16: * Chris@16: * @brief The boost::asio::buffer function is used to create a buffer object to Chris@16: * represent raw memory, an array of POD elements, a vector of POD elements, Chris@16: * or a std::string. Chris@16: * Chris@16: * A buffer object represents a contiguous region of memory as a 2-tuple Chris@16: * consisting of a pointer and size in bytes. A tuple of the form {void*, Chris@16: * size_t} specifies a mutable (modifiable) region of memory. Similarly, a Chris@16: * tuple of the form {const void*, size_t} specifies a const Chris@16: * (non-modifiable) region of memory. These two forms correspond to the classes Chris@16: * mutable_buffer and const_buffer, respectively. To mirror C++'s conversion Chris@16: * rules, a mutable_buffer is implicitly convertible to a const_buffer, and the Chris@16: * opposite conversion is not permitted. Chris@16: * Chris@16: * The simplest use case involves reading or writing a single buffer of a Chris@16: * specified size: Chris@16: * Chris@16: * @code sock.send(boost::asio::buffer(data, size)); @endcode Chris@16: * Chris@16: * In the above example, the return value of boost::asio::buffer meets the Chris@16: * requirements of the ConstBufferSequence concept so that it may be directly Chris@16: * passed to the socket's write function. A buffer created for modifiable Chris@16: * memory also meets the requirements of the MutableBufferSequence concept. Chris@16: * Chris@16: * An individual buffer may be created from a builtin array, std::vector, Chris@16: * std::array or boost::array of POD elements. This helps prevent buffer Chris@16: * overruns by automatically determining the size of the buffer: Chris@16: * Chris@16: * @code char d1[128]; Chris@16: * size_t bytes_transferred = sock.receive(boost::asio::buffer(d1)); Chris@16: * Chris@16: * std::vector d2(128); Chris@16: * bytes_transferred = sock.receive(boost::asio::buffer(d2)); Chris@16: * Chris@16: * std::array d3; Chris@16: * bytes_transferred = sock.receive(boost::asio::buffer(d3)); Chris@16: * Chris@16: * boost::array d4; Chris@16: * bytes_transferred = sock.receive(boost::asio::buffer(d4)); @endcode Chris@16: * Chris@16: * In all three cases above, the buffers created are exactly 128 bytes long. Chris@16: * Note that a vector is @e never automatically resized when creating or using Chris@16: * a buffer. The buffer size is determined using the vector's size() Chris@16: * member function, and not its capacity. Chris@16: * Chris@16: * @par Accessing Buffer Contents Chris@16: * Chris@16: * The contents of a buffer may be accessed using the @ref buffer_size and Chris@16: * @ref buffer_cast functions: Chris@16: * Chris@16: * @code boost::asio::mutable_buffer b1 = ...; Chris@16: * std::size_t s1 = boost::asio::buffer_size(b1); Chris@16: * unsigned char* p1 = boost::asio::buffer_cast(b1); Chris@16: * Chris@16: * boost::asio::const_buffer b2 = ...; Chris@16: * std::size_t s2 = boost::asio::buffer_size(b2); Chris@16: * const void* p2 = boost::asio::buffer_cast(b2); @endcode Chris@16: * Chris@16: * The boost::asio::buffer_cast function permits violations of type safety, so Chris@16: * uses of it in application code should be carefully considered. Chris@16: * Chris@16: * For convenience, the @ref buffer_size function also works on buffer Chris@16: * sequences (that is, types meeting the ConstBufferSequence or Chris@16: * MutableBufferSequence type requirements). In this case, the function returns Chris@16: * the total size of all buffers in the sequence. Chris@16: * Chris@16: * @par Buffer Copying Chris@16: * Chris@16: * The @ref buffer_copy function may be used to copy raw bytes between Chris@16: * individual buffers and buffer sequences. Chris@16: * Chris@16: * In particular, when used with the @ref buffer_size, the @ref buffer_copy Chris@16: * function can be used to linearise a sequence of buffers. For example: Chris@16: * Chris@16: * @code vector buffers = ...; Chris@16: * Chris@16: * vector data(boost::asio::buffer_size(buffers)); Chris@16: * boost::asio::buffer_copy(boost::asio::buffer(data), buffers); @endcode Chris@16: * Chris@101: * Note that @ref buffer_copy is implemented in terms of @c memcpy, and Chris@101: * consequently it cannot be used to copy between overlapping memory regions. Chris@101: * Chris@16: * @par Buffer Invalidation Chris@16: * Chris@16: * A buffer object does not have any ownership of the memory it refers to. It Chris@16: * is the responsibility of the application to ensure the memory region remains Chris@16: * valid until it is no longer required for an I/O operation. When the memory Chris@16: * is no longer available, the buffer is said to have been invalidated. Chris@16: * Chris@16: * For the boost::asio::buffer overloads that accept an argument of type Chris@16: * std::vector, the buffer objects returned are invalidated by any vector Chris@16: * operation that also invalidates all references, pointers and iterators Chris@16: * referring to the elements in the sequence (C++ Std, 23.2.4) Chris@16: * Chris@16: * For the boost::asio::buffer overloads that accept an argument of type Chris@16: * std::basic_string, the buffer objects returned are invalidated according to Chris@16: * the rules defined for invalidation of references, pointers and iterators Chris@16: * referring to elements of the sequence (C++ Std, 21.3). Chris@16: * Chris@16: * @par Buffer Arithmetic Chris@16: * Chris@16: * Buffer objects may be manipulated using simple arithmetic in a safe way Chris@16: * which helps prevent buffer overruns. Consider an array initialised as Chris@16: * follows: Chris@16: * Chris@16: * @code boost::array a = { 'a', 'b', 'c', 'd', 'e' }; @endcode Chris@16: * Chris@16: * A buffer object @c b1 created using: Chris@16: * Chris@16: * @code b1 = boost::asio::buffer(a); @endcode Chris@16: * Chris@16: * represents the entire array, { 'a', 'b', 'c', 'd', 'e' }. An Chris@16: * optional second argument to the boost::asio::buffer function may be used to Chris@16: * limit the size, in bytes, of the buffer: Chris@16: * Chris@16: * @code b2 = boost::asio::buffer(a, 3); @endcode Chris@16: * Chris@16: * such that @c b2 represents the data { 'a', 'b', 'c' }. Even if the Chris@16: * size argument exceeds the actual size of the array, the size of the buffer Chris@16: * object created will be limited to the array size. Chris@16: * Chris@16: * An offset may be applied to an existing buffer to create a new one: Chris@16: * Chris@16: * @code b3 = b1 + 2; @endcode Chris@16: * Chris@16: * where @c b3 will set to represent { 'c', 'd', 'e' }. If the offset Chris@16: * exceeds the size of the existing buffer, the newly created buffer will be Chris@16: * empty. Chris@16: * Chris@16: * Both an offset and size may be specified to create a buffer that corresponds Chris@16: * to a specific range of bytes within an existing buffer: Chris@16: * Chris@16: * @code b4 = boost::asio::buffer(b1 + 1, 3); @endcode Chris@16: * Chris@16: * so that @c b4 will refer to the bytes { 'b', 'c', 'd' }. Chris@16: * Chris@16: * @par Buffers and Scatter-Gather I/O Chris@16: * Chris@16: * To read or write using multiple buffers (i.e. scatter-gather I/O), multiple Chris@16: * buffer objects may be assigned into a container that supports the Chris@16: * MutableBufferSequence (for read) or ConstBufferSequence (for write) concepts: Chris@16: * Chris@16: * @code Chris@16: * char d1[128]; Chris@16: * std::vector d2(128); Chris@16: * boost::array d3; Chris@16: * Chris@16: * boost::array bufs1 = { Chris@16: * boost::asio::buffer(d1), Chris@16: * boost::asio::buffer(d2), Chris@16: * boost::asio::buffer(d3) }; Chris@16: * bytes_transferred = sock.receive(bufs1); Chris@16: * Chris@16: * std::vector bufs2; Chris@16: * bufs2.push_back(boost::asio::buffer(d1)); Chris@16: * bufs2.push_back(boost::asio::buffer(d2)); Chris@16: * bufs2.push_back(boost::asio::buffer(d3)); Chris@16: * bytes_transferred = sock.send(bufs2); @endcode Chris@16: */ Chris@16: /*@{*/ Chris@16: Chris@16: /// Create a new modifiable buffer from an existing buffer. Chris@16: /** Chris@16: * @returns mutable_buffers_1(b). Chris@16: */ Chris@16: inline mutable_buffers_1 buffer(const mutable_buffer& b) Chris@16: { Chris@16: return mutable_buffers_1(b); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer from an existing buffer. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * buffer_cast(b), Chris@16: * min(buffer_size(b), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: inline mutable_buffers_1 buffer(const mutable_buffer& b, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(buffer_cast(b), Chris@16: buffer_size(b) < max_size_in_bytes Chris@16: ? buffer_size(b) : max_size_in_bytes Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer from an existing buffer. Chris@16: /** Chris@16: * @returns const_buffers_1(b). Chris@16: */ Chris@16: inline const_buffers_1 buffer(const const_buffer& b) Chris@16: { Chris@16: return const_buffers_1(b); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer from an existing buffer. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * buffer_cast(b), Chris@16: * min(buffer_size(b), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: inline const_buffers_1 buffer(const const_buffer& b, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(buffer_cast(b), Chris@16: buffer_size(b) < max_size_in_bytes Chris@16: ? buffer_size(b) : max_size_in_bytes Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , b.get_debug_check() Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given memory range. Chris@16: /** Chris@16: * @returns mutable_buffers_1(data, size_in_bytes). Chris@16: */ Chris@16: inline mutable_buffers_1 buffer(void* data, std::size_t size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1(mutable_buffer(data, size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given memory range. Chris@16: /** Chris@16: * @returns const_buffers_1(data, size_in_bytes). Chris@16: */ Chris@16: inline const_buffers_1 buffer(const void* data, Chris@16: std::size_t size_in_bytes) Chris@16: { Chris@16: return const_buffers_1(const_buffer(data, size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * static_cast(data), Chris@16: * N * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(PodType (&data)[N]) Chris@16: { Chris@16: return mutable_buffers_1(mutable_buffer(data, N * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * static_cast(data), Chris@16: * min(N * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(PodType (&data)[N], Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data, Chris@16: N * sizeof(PodType) < max_size_in_bytes Chris@16: ? N * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * static_cast(data), Chris@16: * N * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const PodType (&data)[N]) Chris@16: { Chris@16: return const_buffers_1(const_buffer(data, N * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * static_cast(data), Chris@16: * min(N * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const PodType (&data)[N], Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data, Chris@16: N * sizeof(PodType) < max_size_in_bytes Chris@16: ? N * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) Chris@16: Chris@16: // Borland C++ and Sun Studio think the overloads: Chris@16: // Chris@16: // unspecified buffer(boost::array& array ...); Chris@16: // Chris@16: // and Chris@16: // Chris@16: // unspecified buffer(boost::array& array ...); Chris@16: // Chris@16: // are ambiguous. This will be worked around by using a buffer_types traits Chris@16: // class that contains typedefs for the appropriate buffer and container Chris@16: // classes, based on whether PodType is const or non-const. Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct buffer_types_base; Chris@16: Chris@16: template <> Chris@16: struct buffer_types_base Chris@16: { Chris@16: typedef mutable_buffer buffer_type; Chris@16: typedef mutable_buffers_1 container_type; Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct buffer_types_base Chris@16: { Chris@16: typedef const_buffer buffer_type; Chris@16: typedef const_buffers_1 container_type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct buffer_types Chris@16: : public buffer_types_base::value> Chris@16: { Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: inline typename detail::buffer_types::container_type Chris@16: buffer(boost::array& data) Chris@16: { Chris@16: typedef typename boost::asio::detail::buffer_types::buffer_type Chris@16: buffer_type; Chris@16: typedef typename boost::asio::detail::buffer_types::container_type Chris@16: container_type; Chris@16: return container_type( Chris@16: buffer_type(data.c_array(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename detail::buffer_types::container_type Chris@16: buffer(boost::array& data, std::size_t max_size_in_bytes) Chris@16: { Chris@16: typedef typename boost::asio::detail::buffer_types::buffer_type Chris@16: buffer_type; Chris@16: typedef typename boost::asio::detail::buffer_types::container_type Chris@16: container_type; Chris@16: return container_type( Chris@16: buffer_type(data.c_array(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: #else // defined(BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(boost::array& data) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.c_array(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(boost::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.c_array(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(boost::array& data) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(boost::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: #endif // defined(BOOST_ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const boost::array& data) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const boost::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(std::array& data) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.data(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(std::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.data(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(std::array& data) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(std::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const std::array& data) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), data.size() * sizeof(PodType))); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD array. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer(const std::array& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes)); Chris@16: } Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD vector. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.size() ? &data[0] : 0, Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: * Chris@16: * @note The buffer is invalidated by any vector operation that would also Chris@16: * invalidate iterators. Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(std::vector& data) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType) Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::vector::iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new modifiable buffer that represents the given POD vector. Chris@16: /** Chris@16: * @returns A mutable_buffers_1 value equivalent to: Chris@16: * @code mutable_buffers_1( Chris@16: * data.size() ? &data[0] : 0, Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: * Chris@16: * @note The buffer is invalidated by any vector operation that would also Chris@16: * invalidate iterators. Chris@16: */ Chris@16: template Chris@16: inline mutable_buffers_1 buffer(std::vector& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return mutable_buffers_1( Chris@16: mutable_buffer(data.size() ? &data[0] : 0, Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::vector::iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD vector. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.size() ? &data[0] : 0, Chris@16: * data.size() * sizeof(PodType)); @endcode Chris@16: * Chris@16: * @note The buffer is invalidated by any vector operation that would also Chris@16: * invalidate iterators. Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer( Chris@16: const std::vector& data) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType) Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::vector::const_iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given POD vector. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.size() ? &data[0] : 0, Chris@16: * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode Chris@16: * Chris@16: * @note The buffer is invalidated by any vector operation that would also Chris@16: * invalidate iterators. Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer( Chris@16: const std::vector& data, std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.size() ? &data[0] : 0, Chris@16: data.size() * sizeof(PodType) < max_size_in_bytes Chris@16: ? data.size() * sizeof(PodType) : max_size_in_bytes Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::vector::const_iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given string. Chris@16: /** Chris@16: * @returns const_buffers_1(data.data(), data.size() * sizeof(Elem)). Chris@16: * Chris@16: * @note The buffer is invalidated by any non-const operation called on the Chris@16: * given string object. Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer( Chris@16: const std::basic_string& data) Chris@16: { Chris@16: return const_buffers_1(const_buffer(data.data(), data.size() * sizeof(Elem) Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::basic_string::const_iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /// Create a new non-modifiable buffer that represents the given string. Chris@16: /** Chris@16: * @returns A const_buffers_1 value equivalent to: Chris@16: * @code const_buffers_1( Chris@16: * data.data(), Chris@16: * min(data.size() * sizeof(Elem), max_size_in_bytes)); @endcode Chris@16: * Chris@16: * @note The buffer is invalidated by any non-const operation called on the Chris@16: * given string object. Chris@16: */ Chris@16: template Chris@16: inline const_buffers_1 buffer( Chris@16: const std::basic_string& data, Chris@16: std::size_t max_size_in_bytes) Chris@16: { Chris@16: return const_buffers_1( Chris@16: const_buffer(data.data(), Chris@16: data.size() * sizeof(Elem) < max_size_in_bytes Chris@16: ? data.size() * sizeof(Elem) : max_size_in_bytes Chris@16: #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING) Chris@16: , detail::buffer_debug_check< Chris@16: typename std::basic_string::const_iterator Chris@16: >(data.begin()) Chris@16: #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING Chris@16: )); Chris@16: } Chris@16: Chris@16: /*@}*/ Chris@16: Chris@16: /** @defgroup buffer_copy boost::asio::buffer_copy Chris@16: * Chris@16: * @brief The boost::asio::buffer_copy function is used to copy bytes from a Chris@16: * source buffer (or buffer sequence) to a target buffer (or buffer sequence). Chris@16: * Chris@16: * The @c buffer_copy function is available in two forms: Chris@16: * Chris@16: * @li A 2-argument form: @c buffer_copy(target, source) Chris@16: * Chris@16: * @li A 3-argument form: @c buffer_copy(target, source, max_bytes_to_copy) Chris@16: Chris@16: * Both forms return the number of bytes actually copied. The number of bytes Chris@16: * copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c If specified, @c max_bytes_to_copy. Chris@16: * Chris@16: * This prevents buffer overflow, regardless of the buffer sizes used in the Chris@16: * copy operation. Chris@101: * Chris@101: * Note that @ref buffer_copy is implemented in terms of @c memcpy, and Chris@101: * consequently it cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: /*@{*/ Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const const_buffer& source) Chris@16: { Chris@16: using namespace std; // For memcpy. Chris@16: std::size_t target_size = buffer_size(target); Chris@16: std::size_t source_size = buffer_size(source); Chris@16: std::size_t n = target_size < source_size ? target_size : source_size; Chris@16: memcpy(buffer_cast(target), buffer_cast(source), n); Chris@16: return n; Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const const_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(target, static_cast(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const mutable_buffer& source) Chris@16: { Chris@16: return buffer_copy(target, const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const mutable_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(target, const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer sequence to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const ConstBufferSequence& source) Chris@16: { Chris@16: std::size_t total_bytes_copied = 0; Chris@16: Chris@16: typename ConstBufferSequence::const_iterator source_iter = source.begin(); Chris@16: typename ConstBufferSequence::const_iterator source_end = source.end(); Chris@16: Chris@16: for (mutable_buffer target_buffer(target); Chris@16: buffer_size(target_buffer) && source_iter != source_end; ++source_iter) Chris@16: { Chris@16: const_buffer source_buffer(*source_iter); Chris@16: std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer); Chris@16: total_bytes_copied += bytes_copied; Chris@16: target_buffer = target_buffer + bytes_copied; Chris@16: } Chris@16: Chris@16: return total_bytes_copied; Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const const_buffer& source) Chris@16: { Chris@16: return buffer_copy(static_cast(target), source); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const const_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(static_cast(target), Chris@16: static_cast(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const mutable_buffer& source) Chris@16: { Chris@16: return buffer_copy(static_cast(target), Chris@16: const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const mutable_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(static_cast(target), Chris@16: const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer sequence to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const ConstBufferSequence& source) Chris@16: { Chris@16: return buffer_copy(static_cast(target), source); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const const_buffer& source) Chris@16: { Chris@16: std::size_t total_bytes_copied = 0; Chris@16: Chris@16: typename MutableBufferSequence::const_iterator target_iter = target.begin(); Chris@16: typename MutableBufferSequence::const_iterator target_end = target.end(); Chris@16: Chris@16: for (const_buffer source_buffer(source); Chris@16: buffer_size(source_buffer) && target_iter != target_end; ++target_iter) Chris@16: { Chris@16: mutable_buffer target_buffer(*target_iter); Chris@16: std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer); Chris@16: total_bytes_copied += bytes_copied; Chris@16: source_buffer = source_buffer + bytes_copied; Chris@16: } Chris@16: Chris@16: return total_bytes_copied; Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const const_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(target, static_cast(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const mutable_buffer& source) Chris@16: { Chris@16: return buffer_copy(target, const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer to a target buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const mutable_buffers_1& source) Chris@16: { Chris@16: return buffer_copy(target, const_buffer(source)); Chris@16: } Chris@16: Chris@16: /// Copies bytes from a source buffer sequence to a target buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const ConstBufferSequence& source) Chris@16: { Chris@16: std::size_t total_bytes_copied = 0; Chris@16: Chris@16: typename MutableBufferSequence::const_iterator target_iter = target.begin(); Chris@16: typename MutableBufferSequence::const_iterator target_end = target.end(); Chris@16: std::size_t target_buffer_offset = 0; Chris@16: Chris@16: typename ConstBufferSequence::const_iterator source_iter = source.begin(); Chris@16: typename ConstBufferSequence::const_iterator source_end = source.end(); Chris@16: std::size_t source_buffer_offset = 0; Chris@16: Chris@16: while (target_iter != target_end && source_iter != source_end) Chris@16: { Chris@16: mutable_buffer target_buffer = Chris@16: mutable_buffer(*target_iter) + target_buffer_offset; Chris@16: Chris@16: const_buffer source_buffer = Chris@16: const_buffer(*source_iter) + source_buffer_offset; Chris@16: Chris@16: std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer); Chris@16: total_bytes_copied += bytes_copied; Chris@16: Chris@16: if (bytes_copied == buffer_size(target_buffer)) Chris@16: { Chris@16: ++target_iter; Chris@16: target_buffer_offset = 0; Chris@16: } Chris@16: else Chris@16: target_buffer_offset += bytes_copied; Chris@16: Chris@16: if (bytes_copied == buffer_size(source_buffer)) Chris@16: { Chris@16: ++source_iter; Chris@16: source_buffer_offset = 0; Chris@16: } Chris@16: else Chris@16: source_buffer_offset += bytes_copied; Chris@16: } Chris@16: Chris@16: return total_bytes_copied; Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const const_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const const_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const mutable_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const mutable_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer sequence to a target Chris@16: /// buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const mutable_buffer& target, Chris@16: const ConstBufferSequence& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const const_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const const_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const mutable_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const mutable_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer sequence to a target Chris@16: /// buffer. Chris@16: /** Chris@16: * @param target A modifiable buffer representing the memory region to which Chris@16: * the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const mutable_buffers_1& target, Chris@16: const ConstBufferSequence& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(buffer(target, max_bytes_to_copy), source); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer Chris@16: /// sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const const_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(target, buffer(source, max_bytes_to_copy)); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer Chris@16: /// sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer representing the memory region from Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const const_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(target, buffer(source, max_bytes_to_copy)); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer Chris@16: /// sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const mutable_buffer& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(target, buffer(source, max_bytes_to_copy)); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer to a target buffer Chris@16: /// sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A modifiable buffer representing the memory region from which Chris@16: * the bytes will be copied. The contents of the source buffer will not be Chris@16: * modified. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: inline std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const mutable_buffers_1& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: return buffer_copy(target, buffer(source, max_bytes_to_copy)); Chris@16: } Chris@16: Chris@16: /// Copies a limited number of bytes from a source buffer sequence to a target Chris@16: /// buffer sequence. Chris@16: /** Chris@16: * @param target A modifiable buffer sequence representing the memory regions to Chris@16: * which the bytes will be copied. Chris@16: * Chris@16: * @param source A non-modifiable buffer sequence representing the memory Chris@16: * regions from which the bytes will be copied. Chris@16: * Chris@16: * @param max_bytes_to_copy The maximum number of bytes to be copied. Chris@16: * Chris@16: * @returns The number of bytes copied. Chris@16: * Chris@16: * @note The number of bytes copied is the lesser of: Chris@16: * Chris@16: * @li @c buffer_size(target) Chris@16: * Chris@16: * @li @c buffer_size(source) Chris@16: * Chris@16: * @li @c max_bytes_to_copy Chris@101: * Chris@101: * This function is implemented in terms of @c memcpy, and consequently it Chris@101: * cannot be used to copy between overlapping memory regions. Chris@16: */ Chris@16: template Chris@16: std::size_t buffer_copy(const MutableBufferSequence& target, Chris@16: const ConstBufferSequence& source, std::size_t max_bytes_to_copy) Chris@16: { Chris@16: std::size_t total_bytes_copied = 0; Chris@16: Chris@16: typename MutableBufferSequence::const_iterator target_iter = target.begin(); Chris@16: typename MutableBufferSequence::const_iterator target_end = target.end(); Chris@16: std::size_t target_buffer_offset = 0; Chris@16: Chris@16: typename ConstBufferSequence::const_iterator source_iter = source.begin(); Chris@16: typename ConstBufferSequence::const_iterator source_end = source.end(); Chris@16: std::size_t source_buffer_offset = 0; Chris@16: Chris@16: while (total_bytes_copied != max_bytes_to_copy Chris@16: && target_iter != target_end && source_iter != source_end) Chris@16: { Chris@16: mutable_buffer target_buffer = Chris@16: mutable_buffer(*target_iter) + target_buffer_offset; Chris@16: Chris@16: const_buffer source_buffer = Chris@16: const_buffer(*source_iter) + source_buffer_offset; Chris@16: Chris@16: std::size_t bytes_copied = buffer_copy(target_buffer, Chris@16: source_buffer, max_bytes_to_copy - total_bytes_copied); Chris@16: total_bytes_copied += bytes_copied; Chris@16: Chris@16: if (bytes_copied == buffer_size(target_buffer)) Chris@16: { Chris@16: ++target_iter; Chris@16: target_buffer_offset = 0; Chris@16: } Chris@16: else Chris@16: target_buffer_offset += bytes_copied; Chris@16: Chris@16: if (bytes_copied == buffer_size(source_buffer)) Chris@16: { Chris@16: ++source_iter; Chris@16: source_buffer_offset = 0; Chris@16: } Chris@16: else Chris@16: source_buffer_offset += bytes_copied; Chris@16: } Chris@16: Chris@16: return total_bytes_copied; Chris@16: } Chris@16: Chris@16: /*@}*/ Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BUFFER_HPP