Boost Asio summary (9) data buffer class mutable_buffer and const_buffer

Stores the memory address and data length of a 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;
  }
};

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/123565244