跳转至

byte_buffer 类

顾名思义,二进制字节buffer。

概念

  • 对象内存模型和std::vector类似
  • 支持精确resize
  • resize时默认不填充0(uninitialized and for overwrite)
  • 支持释放内存管理权 release_pointer
  • 支持两种allocator
  • yasio::default_byte_allocator (new/delete)
  • yasio::crt_byte_allocator (malloc/free)
  • 可存储一切连续内存,例如:
    std:vector<int> vints{1,2,3};
    yasio::byte_buffer vints_bytes{vints.begin(), vints.end(), std::true_type{}}'
    

语法

namespace yasio { 
template <typename _Elem, typename _Alloc = default_byte_allocator<_Elem>,
        enable_if_t<is_byte_type<_Elem>::value, int> = 0>
class basic_byte_buffer;
using sbyte_buffer = basic_byte_buffer<char>;
using byte_buffer  = basic_byte_buffer<unsigned char>;
}

成员

公共构造函数

Name Description
basic_byte_buffer::basic_byte_buffer 构造1个basic_byte_buffer 对象

公共方法

Name Description
basic_byte_buffer::assign 销毁对象并回收到对象池
basic_byte_buffer::swap 销毁对象并回收到对象池
basic_byte_buffer::insert 销毁对象并回收到对象池
basic_byte_buffer::append 销毁对象并回收到对象池
basic_byte_buffer::push_back 销毁对象并回收到对象池
basic_byte_buffer::erase 销毁对象并回收到对象池
basic_byte_buffer::front 销毁对象并回收到对象池
basic_byte_buffer::back 销毁对象并回收到对象池
basic_byte_buffer::begin 销毁对象并回收到对象池
basic_byte_buffer::end 销毁对象并回收到对象池
basic_byte_buffer::data 销毁对象并回收到对象池
basic_byte_buffer::capacity 销毁对象并回收到对象池
basic_byte_buffer::size 销毁对象并回收到对象池
basic_byte_buffer::clear 销毁对象并回收到对象池
basic_byte_buffer::empty 销毁对象并回收到对象池
basic_byte_buffer::at 销毁对象并回收到对象池
basic_byte_buffer::resize 销毁对象并回收到对象池
basic_byte_buffer::resize_fit 销毁对象并回收到对象池
basic_byte_buffer::reserve 销毁对象并回收到对象池
basic_byte_buffer::shrink_to_fit 销毁对象并回收到对象池
basic_byte_buffer::release_pointer 销毁对象并回收到对象池

操作符

Name Description
basic_byte_buffer::operator[] 销毁对象并回收到对象池
basic_byte_buffer::operator= 从对象池创建1个对象

要求

头文件: yasio/core/byte_buffer.hpp

basic_byte_buffer::basic_byte_buffer

构造 byte_buffer 对象。

basic_byte_buffer() {}
  explicit basic_byte_buffer(size_type count);
  basic_byte_buffer(size_type count, std::true_type /*fit*/);
  basic_byte_buffer(size_type count, const_reference val);
  basic_byte_buffer(size_type count, const_reference val, std::true_type /*fit*/);
  template <typename _Iter>
  basic_byte_buffer(_Iter first, _Iter last);
  template <typename _Iter>
  basic_byte_buffer(_Iter first, _Iter last, std::true_type /*fit*/);
  basic_byte_buffer(const basic_byte_buffer& rhs);
  basic_byte_buffer(const basic_byte_buffer& rhs, std::true_type /*fit*/);
  basic_byte_buffer(basic_byte_buffer&& rhs) YASIO__NOEXCEPT { assign(std::move(rhs)); }
  template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
  basic_byte_buffer(std::initializer_list<_Ty> rhs);
  template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
  basic_byte_buffer(std::initializer_list<_Ty> rhs, std::true_type /*fit*/);

参数

count
初始字节数。

fit
分配合适大小标记。

first 要复制的元素范围内的第一个元素的位置。

last
超出要复制的元素范围的第一个元素的位置。

rhs
要成为副本的构造的源。

basic_byte_buffer::assign

清除byte_buffer并将指定的元素复制到该空byte_buffer。

template <typename _Iter>
void assign(const _Iter first, const _Iter last);
template <typename _Iter>
void assign(const _Iter first, const _Iter last, std::true_type /*fit*/);
void assign(const basic_byte_buffer& rhs) { _Assign_range(rhs.begin(), rhs.end()); }
void assign(const basic_byte_buffer& rhs, std::true_type);
void assign(basic_byte_buffer&& rhs) { _Assign_rv(std::move(rhs)); }
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
void assign(std::initializer_list<_Ty> rhs);
template <typename _Ty, enable_if_t<std::is_integral<_Ty>::value, int> = 0>
void assign(std::initializer_list<_Ty> rhs, std::true_type /*fit*/);

参数

first
要复制的元素范围内的第一个元素的位置。

*last
超出要复制的元素范围的第一个元素的位置。

rhs
要成为副本的构造的源。

basic_byte_buffer::swap

交换两个byte_buffer的元素。

void swap(basic_byte_buffer& rhs);

参数

rhs
一个提供要交换的元素的byte_buffer。

basic_byte_buffer::insert

在指定位置插入字节数据。

template <typename _Iter>
iterator insert(iterator _Where, _Iter first, const _Iter last);

参数

_Where
要插入的位置。

first
要复制的元素范围内的第一个元素的位置。

last
超出要复制的元素范围的第一个元素的位置。

返回值

返回插入元素位置迭代器。

basic_byte_buffer::append

向byte_buffer追加字节数据。

template <typename _Iter>
basic_byte_buffer& append(_Iter first, const _Iter last);

参数

first
要复制的元素范围内的第一个元素的位置。

last
超出要复制的元素范围的第一个元素的位置。

返回值

当前byte_buffer对象。

basic_byte_buffer::push_back

向byte_buffer尾部追加1个字节。

void push_back(value_type v);

参数

v
要追加的字节值。

basic_byte_buffer::erase

删除指定迭代器元素。

iterator erase(iterator _Where);

参数

_Where
要删除元素迭代器。

返回值

返回删除元素的下一个迭代器。

basic_byte_buffer::front

获取byte_buffer第一个字节。

value_type& front();

返回值

返回byte_buffer第一个字节。

basic_byte_buffer::back

获取byte_buffer最后一个字节。

value_type& front();

返回值

byte_buffer最后一个字节。

begin

Returns a random-access iterator to the first element in the byte_buffer.

const_iterator begin() const;

iterator begin();

Return value

A random-access iterator addressing the first element in the basic_byte_buffer or to the location succeeding an empty basic_byte_buffer. Always compare the value returned with basic_byte_buffer::end to ensure it's valid.

end

Returns a past-the-end iterator that points to the element following the last element of the vector.

iterator end();

const_iterator end() const;

Return value

A past-the-end iterator for the vector. It points to the element following the last element of the vector. That element is a placeholder and shouldn't be dereferenced. Only use it for comparisons. If the vector is empty, then basic_byte_buffer::end() == basic_byte_buffer::begin().