Boost Asio总结数据缓冲区class mutable_buffer和const_buffer

Posted thefist11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Boost Asio总结数据缓冲区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_DEBUGGING
    return 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_DEBUGGING
    return 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总结数据缓冲区class mutable_buffer和const_buffer的主要内容,如果未能解决你的问题,请参考以下文章

Boost Asio总结(15)class basic_stream_socket

Boost Asio总结class work

Boost Asio总结class tcp

Boost Asio总结(14)class basic_endpoint

Boost Asio总结class strand

为啥 boost::asio::read 缓冲区数据大小小于读取大小?