-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathbuffer.h
132 lines (107 loc) · 3.47 KB
/
buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#ifndef TBOX_UTIL_BUFFER_H_20171028
#define TBOX_UTIL_BUFFER_H_20171028
#include <stdint.h>
namespace tbox {
namespace util {
/**
* 缓冲区类
*
* buffer_ptr_ buffer_size_
* | |
* v V
* +----+----------------+----------------+
* | | readable bytes | writable bytes |
* +----+----------------+----------------+
* ^ ^
* | |
* read_index_ write_index_
*
* 使用示例:
* Buffer b;
* b.append("abc", 4); //! 往缓冲中写入4字节的数据
* char buff[10];
* b.fetch(buff, 4); //! 从缓冲中取出4字节的数据
*
* b.ensureWritableSize(10); //! 预留10个字节
* memset(b.writableBegin(), 0xcc, 10); //! 将该10个字节全置为0xcc
* b.hasWritten(10); //! 标该已写入10个字节
*
* \warnning 多线程使用需在外部加锁
*/
class Buffer {
public:
static const size_t kInitialSize = 256;
explicit Buffer(size_t reverse_size = kInitialSize);
virtual ~Buffer();
Buffer(const Buffer &other);
Buffer(Buffer &&other);
Buffer& operator = (const Buffer &other);
Buffer& operator = (Buffer &&other);
void swap(Buffer &other);
void reset();
public:
/**
* 写缓冲操作
*/
//! 获取可写空间大小
inline size_t writableSize() const { return buffer_size_ - write_index_; }
//! 保障有指定容量的可写空间
bool ensureWritableSize(size_t write_size);
//! 获取写首地址
inline uint8_t* writableBegin() const {
return (buffer_ptr_ != nullptr) ? (buffer_ptr_ + write_index_) : nullptr;
}
//! 标记已写入数据大小
void hasWritten(size_t write_size);
//! 往缓冲区追加指定的数据块,返回实现写入的大小
size_t append(const void *p_data, size_t data_size);
/**
* 读缓冲操作
*/
//! 获取可读区域大小
inline size_t readableSize() const { return write_index_ - read_index_; }
//! 获取可读区首地址
inline uint8_t* readableBegin() const {
return (buffer_ptr_ != nullptr) ? (buffer_ptr_ + read_index_) : nullptr;
}
//! 标记已读数据大小
void hasRead(size_t read_size);
//! 标记已读取全部数据
void hasReadAll();
//! 从缓冲区读取指定的数据块,返回实际读到的数据大小
size_t fetch(void *p_buff, size_t buff_size);
/**
* 其它
*/
//! 缩减缓冲多余容量
void shrink();
protected:
void cloneFrom(const Buffer &other);
private:
uint8_t *buffer_ptr_ = nullptr; //! 缓冲区地址
size_t buffer_size_ = 0; //! 缓冲区大小
size_t read_index_ = 0; //! 读位置偏移
size_t write_index_ = 0; //! 写位置偏移
};
}
}
#endif //TBOX_UTIL_BUFFER_H_20171028