Adbase
Adinf C++ base library V2
adbase::Buffer Class Reference

Buffer 可以存取多种类型的数据,保证二进制安全,并且内存可以自动分配 More...

#include <Buffer.hpp>

Public Member Functions

 Buffer (size_t initialSize=kInitialSize)
 
size_t readableBytes () const
 获取 buffer 长度 More...
 
size_t writableBytes () const
 获取 buffer 可写长度 More...
 
size_t prependableBytes () const
 buffer 已读取向前可写长度 More...
 
const char * peek () const
 获取 buffer 指针 More...
 
void retrieve (size_t len)
 删除指定长度的数据 , 按照当前读取的指针位置向后删除, 当删除的长度大于 buffer 中的数据时会清空整个 buffer More...
 
void retrieveUntil (const char *end)
 清空数据到指定位置 More...
 
void retrieveInt64 ()
 清空 int64_t 长度的数据 More...
 
void retrieveInt32 ()
 清空 int32_t 长度的数据 More...
 
void retrieveInt16 ()
 清空 int16_t 长度的数据 More...
 
void retrieveInt8 ()
 清空 int8_t 长度的数据 More...
 
void retrieveAll ()
 删除 buffer 中所有数据 More...
 
std::string retrieveAllAsString ()
 将 buffer 转化为 std::string 类型 More...
 
std::string retrieveAsString (size_t len)
 将指定长度的 buffer 转化为 std::string 类型 More...
 
void append (const std::string &str)
 添加 std::string 类型字符串到 buffer 中 More...
 
void append (const char *data, size_t len)
 添加 char* 到 buffer 中 More...
 
void append (const void *data, size_t len)
 添加 void* 到 buffer 中 More...
 
void ensureWritableBytes (size_t len)
 确保 buffer 的写入空间够用,如果不够自动分配内存 More...
 
const char * beginWrite () const
 
char * beginWrite ()
 
void hasWritten (size_t len)
 
void appendInt64 (int64_t x)
 Append int64_t using network endian. More...
 
void appendInt32 (int32_t x)
 Append int32_t using network endian. More...
 
void appendInt16 (int16_t x)
 
void appendInt8 (int8_t x)
 
int64_t readInt64 ()
 Read int64_t from network endian. More...
 
int32_t readInt32 ()
 Read int32_t from network endian. More...
 
int16_t readInt16 ()
 
int8_t readInt8 ()
 
int64_t peekInt64 () const
 Peek int64_t from network endian. More...
 
int32_t peekInt32 () const
 Peek int32_t from network endian. More...
 
int16_t peekInt16 () const
 
int8_t peekInt8 () const
 
void prependInt64 (int64_t x)
 Prepend int64_t using network endian. More...
 
void prependInt32 (int32_t x)
 Prepend int32_t using network endian. More...
 
void prependInt16 (int16_t x)
 
void prependInt8 (int8_t x)
 
void prepend (const void *data, size_t len)
 
void shrink ()
 
size_t internalCapacity () const
 

Static Public Attributes

static const size_t kInitialSize = 1024
 

Detailed Description

Buffer 可以存取多种类型的数据,保证二进制安全,并且内存可以自动分配

内部结构
+-------------------+------------------+------------------+
| prependable bytes | readable bytes | writable bytes |
| | (CONTENT) | |
+-------------------+------------------+------------------+
| | | |
0 <= readerIndex <= writerIndex <= size

Definition at line 34 of file Buffer.hpp.

Constructor & Destructor Documentation

adbase::Buffer::Buffer ( size_t  initialSize = kInitialSize)
inlineexplicit

Definition at line 38 of file Buffer.hpp.

Member Function Documentation

void adbase::Buffer::append ( const std::string &  str)
inline
void adbase::Buffer::append ( const char *  data,
size_t  len 
)
inline

添加 char* 到 buffer 中

Definition at line 123 of file Buffer.hpp.

References beginWrite(), ensureWritableBytes(), and hasWritten().

void adbase::Buffer::append ( const void *  data,
size_t  len 
)
inline

添加 void* 到 buffer 中

Definition at line 130 of file Buffer.hpp.

References append().

void adbase::Buffer::appendInt16 ( int16_t  x)
inline

Definition at line 169 of file Buffer.hpp.

References append(), and adbase::hostToNetwork16().

void adbase::Buffer::appendInt32 ( int32_t  x)
inline

Append int32_t using network endian.

Definition at line 164 of file Buffer.hpp.

References append(), and adbase::hostToNetwork32().

void adbase::Buffer::appendInt64 ( int64_t  x)
inline

Append int64_t using network endian.

Definition at line 156 of file Buffer.hpp.

References append(), and adbase::hostToNetwork64().

void adbase::Buffer::appendInt8 ( int8_t  x)
inline

Definition at line 174 of file Buffer.hpp.

References append().

const char* adbase::Buffer::beginWrite ( ) const
inline

Definition at line 141 of file Buffer.hpp.

Referenced by append().

char* adbase::Buffer::beginWrite ( )
inline

Definition at line 145 of file Buffer.hpp.

void adbase::Buffer::ensureWritableBytes ( size_t  len)
inline

确保 buffer 的写入空间够用,如果不够自动分配内存

Definition at line 135 of file Buffer.hpp.

References writableBytes().

Referenced by append().

void adbase::Buffer::hasWritten ( size_t  len)
inline

Definition at line 149 of file Buffer.hpp.

Referenced by append().

size_t adbase::Buffer::internalCapacity ( ) const
inline

Definition at line 281 of file Buffer.hpp.

References prependableBytes(), readableBytes(), and writableBytes().

int16_t adbase::Buffer::peekInt16 ( ) const
inline

Definition at line 232 of file Buffer.hpp.

References adbase::networkToHost16(), peek(), and readableBytes().

Referenced by readInt16().

int32_t adbase::Buffer::peekInt32 ( ) const
inline

Peek int32_t from network endian.

Require: buf->readableBytes() >= sizeof(int32_t)

Definition at line 225 of file Buffer.hpp.

References adbase::networkToHost32(), peek(), and readableBytes().

Referenced by readInt32().

int64_t adbase::Buffer::peekInt64 ( ) const
inline

Peek int64_t from network endian.

Require: buf->readableBytes() >= sizeof(int64_t)

Definition at line 214 of file Buffer.hpp.

References adbase::networkToHost64(), peek(), and readableBytes().

Referenced by readInt64().

int8_t adbase::Buffer::peekInt8 ( ) const
inline

Definition at line 239 of file Buffer.hpp.

References peek(), and readableBytes().

Referenced by readInt8().

void adbase::Buffer::prepend ( const void *  data,
size_t  len 
)
inline

Definition at line 270 of file Buffer.hpp.

References prependableBytes().

Referenced by prependInt16(), prependInt32(), prependInt64(), and prependInt8().

size_t adbase::Buffer::prependableBytes ( ) const
inline

buffer 已读取向前可写长度

Definition at line 55 of file Buffer.hpp.

Referenced by internalCapacity(), and prepend().

void adbase::Buffer::prependInt16 ( int16_t  x)
inline

Definition at line 261 of file Buffer.hpp.

References adbase::hostToNetwork16(), and prepend().

void adbase::Buffer::prependInt32 ( int32_t  x)
inline

Prepend int32_t using network endian.

Definition at line 256 of file Buffer.hpp.

References adbase::hostToNetwork32(), and prepend().

void adbase::Buffer::prependInt64 ( int64_t  x)
inline

Prepend int64_t using network endian.

Definition at line 248 of file Buffer.hpp.

References adbase::hostToNetwork64(), and prepend().

void adbase::Buffer::prependInt8 ( int8_t  x)
inline

Definition at line 266 of file Buffer.hpp.

References prepend().

int16_t adbase::Buffer::readInt16 ( )
inline

Definition at line 198 of file Buffer.hpp.

References peekInt16(), and retrieveInt16().

int32_t adbase::Buffer::readInt32 ( )
inline

Read int32_t from network endian.

Require: buf->readableBytes() >= sizeof(int32_t)

Definition at line 192 of file Buffer.hpp.

References peekInt32(), and retrieveInt32().

int64_t adbase::Buffer::readInt64 ( )
inline

Read int64_t from network endian.

Require: buf->readableBytes() >= sizeof(int32_t)

Definition at line 182 of file Buffer.hpp.

References peekInt64(), and retrieveInt64().

int8_t adbase::Buffer::readInt8 ( )
inline

Definition at line 204 of file Buffer.hpp.

References peekInt8(), and retrieveInt8().

void adbase::Buffer::retrieve ( size_t  len)
inline

删除指定长度的数据 , 按照当前读取的指针位置向后删除, 当删除的长度大于 buffer 中的数据时会清空整个 buffer

Definition at line 66 of file Buffer.hpp.

References readableBytes(), and retrieveAll().

Referenced by adbase::mc::Context::getAsciiBuffer(), adbase::mc::Ascii::processData(), retrieveAsString(), retrieveInt16(), retrieveInt32(), retrieveInt64(), retrieveInt8(), and retrieveUntil().

void adbase::Buffer::retrieveAll ( )
inline
std::string adbase::Buffer::retrieveAllAsString ( )
inline

将 buffer 转化为 std::string 类型

Definition at line 106 of file Buffer.hpp.

References readableBytes(), and retrieveAsString().

Referenced by adbase::mc::ClientHandler::get(), and adbase::mc::Ascii::processData().

std::string adbase::Buffer::retrieveAsString ( size_t  len)
inline

将指定长度的 buffer 转化为 std::string 类型

Definition at line 111 of file Buffer.hpp.

References peek(), and retrieve().

Referenced by retrieveAllAsString().

void adbase::Buffer::retrieveInt16 ( )
inline

清空 int16_t 长度的数据

Definition at line 90 of file Buffer.hpp.

References retrieve().

Referenced by readInt16().

void adbase::Buffer::retrieveInt32 ( )
inline

清空 int32_t 长度的数据

Definition at line 85 of file Buffer.hpp.

References retrieve().

Referenced by readInt32().

void adbase::Buffer::retrieveInt64 ( )
inline

清空 int64_t 长度的数据

Definition at line 80 of file Buffer.hpp.

References retrieve().

Referenced by readInt64().

void adbase::Buffer::retrieveInt8 ( )
inline

清空 int8_t 长度的数据

Definition at line 95 of file Buffer.hpp.

References retrieve().

Referenced by readInt8().

void adbase::Buffer::retrieveUntil ( const char *  end)
inline

清空数据到指定位置

Definition at line 75 of file Buffer.hpp.

References peek(), and retrieve().

void adbase::Buffer::shrink ( )
inline

Definition at line 277 of file Buffer.hpp.

size_t adbase::Buffer::writableBytes ( ) const
inline

获取 buffer 可写长度

Definition at line 50 of file Buffer.hpp.

Referenced by ensureWritableBytes(), and internalCapacity().

Member Data Documentation

const size_t adbase::Buffer::kInitialSize = 1024
static

Definition at line 36 of file Buffer.hpp.


The documentation for this class was generated from the following file: