本文主要是介绍Boost Asio总结(9)数据缓冲区class mutable_buffer和const_buffer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
保存了一个void*的内存地址和数据长度。
/// Holds a buffer that can be modified.
class mutable_buffer
{
public:/// Construct an empty buffer.mutable_buffer() BOOST_ASIO_NOEXCEPT: data_(0),size_(0){}/// Construct a buffer to represent a given memory range.mutable_buffer(void* data, std::size_t size) BOOST_ASIO_NOEXCEPT: data_(data),size_(size){}#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)mutable_buffer(void* data, std::size_t size,boost::asio::detail::function<void()> debug_check): data_(data),size_(size),debug_check_(debug_check){}const boost::asio::detail::function<void()>& get_debug_check() const{return debug_check_;}
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING/// Get a pointer to the beginning of the memory range.void* data() const BOOST_ASIO_NOEXCEPT{
#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)if (size_ && debug_check_)debug_check_();
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGINGreturn data_;}/// Get the size of the memory range.std::size_t size() const BOOST_ASIO_NOEXCEPT{return size_;}/// Move the start of the buffer by the specified number of bytes.mutable_buffer& operator+=(std::size_t n) BOOST_ASIO_NOEXCEPT{std::size_t offset = n < size_ ? n : size_;data_ = static_cast<char*>(data_) + offset;size_ -= offset;return *this;}private:void* data_;std::size_t size_;#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)boost::asio::detail::function<void()> debug_check_;
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
};/// Holds a buffer that cannot be modified.class const_buffer
{
public:/// Construct an empty buffer.const_buffer() BOOST_ASIO_NOEXCEPT: data_(0),size_(0){}/// Construct a buffer to represent a given memory range.const_buffer(const void* data, std::size_t size) BOOST_ASIO_NOEXCEPT: data_(data),size_(size){}/// Construct a non-modifiable buffer from a modifiable one.const_buffer(const mutable_buffer& b) BOOST_ASIO_NOEXCEPT: data_(b.data()),size_(b.size())
#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING), debug_check_(b.get_debug_check())
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING{}#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)const_buffer(const void* data, std::size_t size,boost::asio::detail::function<void()> debug_check): data_(data),size_(size),debug_check_(debug_check){}const boost::asio::detail::function<void()>& get_debug_check() const{return debug_check_;}
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING/// Get a pointer to the beginning of the memory range.const void* data() const BOOST_ASIO_NOEXCEPT{
#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)if (size_ && debug_check_)debug_check_();
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGINGreturn data_;}/// Get the size of the memory range.std::size_t size() const BOOST_ASIO_NOEXCEPT{return size_;}/// Move the start of the buffer by the specified number of bytes.const_buffer& operator+=(std::size_t n) BOOST_ASIO_NOEXCEPT{std::size_t offset = n < size_ ? n : size_;data_ = static_cast<const char*>(data_) + offset;size_ -= offset;return *this;}private:const void* data_;std::size_t size_;#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)boost::asio::detail::function<void()> debug_check_;
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
};class mutable_buffers_1: public mutable_buffer
{
public:/// The type for each element in the list of buffers.typedef mutable_buffer value_type;/// A random-access iterator type that may be used to read elements.typedef const mutable_buffer* const_iterator;/// Get a random-access iterator to the first element.const_iterator begin() const BOOST_ASIO_NOEXCEPT{return this;}/// Get a random-access iterator for one past the last element.const_iterator end() const BOOST_ASIO_NOEXCEPT{return begin() + 1;}
};class const_buffers_1: public const_buffer
{
public:/// The type for each element in the list of buffers.typedef const_buffer value_type;/// A random-access iterator type that may be used to read elements.typedef const const_buffer* const_iterator;/// Construct to represent a given memory range.const_buffers_1(const void* data, std::size_t size) BOOST_ASIO_NOEXCEPT: const_buffer(data, size){}#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)const_buffers_1(const void* data, std::size_t size,boost::asio::detail::function<void()> debug_check): const_buffer(data, size, debug_check){}
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING/// Construct to represent a single non-modifiable buffer.explicit const_buffers_1(const const_buffer& b) BOOST_ASIO_NOEXCEPT: const_buffer(b){}/// Get a random-access iterator to the first element.const_iterator begin() const BOOST_ASIO_NOEXCEPT{return this;}/// Get a random-access iterator for one past the last element.const_iterator end() const BOOST_ASIO_NOEXCEPT{return begin() + 1;}
};
这篇关于Boost Asio总结(9)数据缓冲区class mutable_buffer和const_buffer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!