📄 buffer.hpp
字号:
}private: Iterator iter_;};} // namespace detail#endif // ASIO_ENABLE_BUFFER_DEBUGGING/** @defgroup buffer asio::buffer * * @brief The asio::buffer function is used to create a buffer object to * represent raw memory, an array of POD elements, or a vector of POD elements. * * The simplest use case involves reading or writing a single buffer of a * specified size: * * @code sock.write(asio::buffer(data, size)); @endcode * * In the above example, the return value of asio::buffer meets the * requirements of the ConstBufferSequence concept so that it may be directly * passed to the socket's write function. A buffer created for modifiable * memory also meets the requirements of the MutableBufferSequence concept. * * An individual buffer may be created from a builtin array, std::vector or * boost::array of POD elements. This helps prevent buffer overruns by * automatically determining the size of the buffer: * * @code char d1[128]; * size_t bytes_transferred = sock.read(asio::buffer(d1)); * * std::vector<char> d2(128); * bytes_transferred = sock.read(asio::buffer(d2)); * * boost::array<char, 128> d3; * bytes_transferred = sock.read(asio::buffer(d3)); @endcode * * To read or write using multiple buffers (i.e. scatter-gather I/O), multiple * buffer objects may be assigned into a container that supports the * MutableBufferSequence (for read) or ConstBufferSequence (for write) concepts: * * @code * char d1[128]; * std::vector<char> d2(128); * boost::array<char, 128> d3; * * boost::array<mutable_buffer, 3> bufs1 = { * asio::buffer(d1), * asio::buffer(d2), * asio::buffer(d3) }; * bytes_transferred = sock.read(bufs1); * * std::vector<const_buffer> bufs2; * bufs2.push_back(asio::buffer(d1)); * bufs2.push_back(asio::buffer(d2)); * bufs2.push_back(asio::buffer(d3)); * bytes_transferred = sock.write(bufs2); @endcode *//*@{*//// Create a new modifiable buffer from an existing buffer.inline mutable_buffers_1 buffer(const mutable_buffer& b){ return mutable_buffers_1(b);}/// Create a new modifiable buffer from an existing buffer.inline mutable_buffers_1 buffer(const mutable_buffer& b, std::size_t max_size_in_bytes){ return mutable_buffers_1( mutable_buffer(buffer_cast<void*>(b), buffer_size(b) < max_size_in_bytes ? buffer_size(b) : max_size_in_bytes#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , b.get_debug_check()#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new non-modifiable buffer from an existing buffer.inline const_buffers_1 buffer(const const_buffer& b){ return const_buffers_1(b);}/// Create a new non-modifiable buffer from an existing buffer.inline const_buffers_1 buffer(const const_buffer& b, std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(buffer_cast<const void*>(b), buffer_size(b) < max_size_in_bytes ? buffer_size(b) : max_size_in_bytes#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , b.get_debug_check()#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new modifiable buffer that represents the given memory range.inline mutable_buffers_1 buffer(void* data, std::size_t size_in_bytes){ return mutable_buffers_1(mutable_buffer(data, size_in_bytes));}/// Create a new non-modifiable buffer that represents the given memory range.inline const_buffers_1 buffer(const void* data, std::size_t size_in_bytes){ return const_buffers_1(const_buffer(data, size_in_bytes));}/// Create a new modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline mutable_buffers_1 buffer(PodType (&data)[N]){ return mutable_buffers_1(mutable_buffer(data, N * sizeof(PodType)));} /// Create a new modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline mutable_buffers_1 buffer(PodType (&data)[N], std::size_t max_size_in_bytes){ return mutable_buffers_1( mutable_buffer(data, N * sizeof(PodType) < max_size_in_bytes ? N * sizeof(PodType) : max_size_in_bytes));} /// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(const PodType (&data)[N]){ return const_buffers_1(const_buffer(data, N * sizeof(PodType)));}/// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(const PodType (&data)[N], std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(data, N * sizeof(PodType) < max_size_in_bytes ? N * sizeof(PodType) : max_size_in_bytes));}#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) \ || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))// Borland C++ and Sun Studio think the overloads://// unspecified buffer(boost::array<PodType, N>& array ...);//// and//// unspecified buffer(boost::array<const PodType, N>& array ...);//// are ambiguous. This will be worked around by using a buffer_types traits// class that contains typedefs for the appropriate buffer and container// classes, based on whether PodType is const or non-const.namespace detail {template <bool IsConst>struct buffer_types_base;template <>struct buffer_types_base<false>{ typedef mutable_buffer buffer_type; typedef mutable_buffers_1 container_type;};template <>struct buffer_types_base<true>{ typedef const_buffer buffer_type; typedef const_buffers_1 container_type;};template <typename PodType>struct buffer_types : public buffer_types_base<boost::is_const<PodType>::value>{};} // namespace detailtemplate <typename PodType, std::size_t N>inline typename detail::buffer_types<PodType>::container_typebuffer(boost::array<PodType, N>& data){ typedef typename asio::detail::buffer_types<PodType>::buffer_type buffer_type; typedef typename asio::detail::buffer_types<PodType>::container_type container_type; return container_type( buffer_type(data.c_array(), data.size() * sizeof(PodType)));}template <typename PodType, std::size_t N>inline typename detail::buffer_types<PodType>::container_typebuffer(boost::array<PodType, N>& data, std::size_t max_size_in_bytes){ typedef typename asio::detail::buffer_types<PodType>::buffer_type buffer_type; typedef typename asio::detail::buffer_types<PodType>::container_type container_type; return container_type( buffer_type(data.c_array(), data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes));}#else // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))/// Create a new modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline mutable_buffers_1 buffer(boost::array<PodType, N>& data){ return mutable_buffers_1( mutable_buffer(data.c_array(), data.size() * sizeof(PodType)));}/// Create a new modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline mutable_buffers_1 buffer(boost::array<PodType, N>& data, std::size_t max_size_in_bytes){ return mutable_buffers_1( mutable_buffer(data.c_array(), data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes));}/// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(boost::array<const PodType, N>& data){ return const_buffers_1( const_buffer(data.data(), data.size() * sizeof(PodType)));}/// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(boost::array<const PodType, N>& data, std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(data.data(), data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes));}#endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))/// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(const boost::array<PodType, N>& data){ return const_buffers_1( const_buffer(data.data(), data.size() * sizeof(PodType)));}/// Create a new non-modifiable buffer that represents the given POD array.template <typename PodType, std::size_t N>inline const_buffers_1 buffer(const boost::array<PodType, N>& data, std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(data.data(), data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes));}/// Create a new modifiable buffer that represents the given POD vector./** * @note The buffer is invalidated by any vector operation that would also * invalidate iterators. */template <typename PodType, typename Allocator>inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data){ return mutable_buffers_1( mutable_buffer(&data[0], data.size() * sizeof(PodType)#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check< typename std::vector<PodType, Allocator>::iterator >(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new modifiable buffer that represents the given POD vector./** * @note The buffer is invalidated by any vector operation that would also * invalidate iterators. */template <typename PodType, typename Allocator>inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data, std::size_t max_size_in_bytes){ return mutable_buffers_1( mutable_buffer(&data[0], data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check< typename std::vector<PodType, Allocator>::iterator >(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new non-modifiable buffer that represents the given POD vector./** * @note The buffer is invalidated by any vector operation that would also * invalidate iterators. */template <typename PodType, typename Allocator>inline const_buffers_1 buffer( const std::vector<PodType, Allocator>& data){ return const_buffers_1( const_buffer(&data[0], data.size() * sizeof(PodType)#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check< typename std::vector<PodType, Allocator>::const_iterator >(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new non-modifiable buffer that represents the given POD vector./** * @note The buffer is invalidated by any vector operation that would also * invalidate iterators. */template <typename PodType, typename Allocator>inline const_buffers_1 buffer( const std::vector<PodType, Allocator>& data, std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(&data[0], data.size() * sizeof(PodType) < max_size_in_bytes ? data.size() * sizeof(PodType) : max_size_in_bytes#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check< typename std::vector<PodType, Allocator>::const_iterator >(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new non-modifiable buffer that represents the given string./** * @note The buffer is invalidated by any non-const operation called on the * given string object. */inline const_buffers_1 buffer(const std::string& data){ return const_buffers_1(const_buffer(data.data(), data.size()#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check<std::string::const_iterator>(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/// Create a new non-modifiable buffer that represents the given string./** * @note The buffer is invalidated by any non-const operation called on the * given string object. */inline const_buffers_1 buffer(const std::string& data, std::size_t max_size_in_bytes){ return const_buffers_1( const_buffer(data.data(), data.size() < max_size_in_bytes ? data.size() : max_size_in_bytes#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) , detail::buffer_debug_check<std::string::const_iterator>(data.begin())#endif // ASIO_ENABLE_BUFFER_DEBUGGING ));}/*@}*/} // namespace asio#include "asio/detail/pop_options.hpp"#endif // ASIO_BUFFER_HPP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -