Boost Asio总结(9)数据缓冲区class mutable_buffer和const_buffer
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Boost Asio总结(7)class
- 下一篇: Boost Asio总结(12)clas