dl
dl_buffer.h
浏览该文件的文档.
1
13#pragma once
14
15#include <vector>
16#include <string>
17#include <span>
18#include <cstring>
19
20#include "base/dl_concept.h"
21#include "io/dl_log.h"
23
24namespace dl
25{
26constexpr size_t BUFFER_SSO_SIZE = 16;
27
28class Buffer
29{
30public:
31 std::byte* data() { return _data ? _data : _ssoData; }
32 const std::byte* data() const { return _data ? _data : _ssoData; }
33
34 size_t size() const { return _length; }
35
36 void resize(size_t len)
37 {
38 if (!_data)
39 {
40 if (len <= BUFFER_SSO_SIZE)
41 {
42 _length = len;
43 return;
44 }
45
46 if (_length == 0)
47 {
48 _data = g_bufferPool.Malloc(len, _index, _size);
49 _length = len;
50 }
51 else
52 {
53 std::byte temp[BUFFER_SSO_SIZE];
54 memcpy(temp, _ssoData, _length);
55
56 _data = g_bufferPool.Malloc(len, _index, _size);
57 memset(_data, 0, len);
58 memcpy(_data, temp, _length);
59 _length = len;
60 }
61 }
62 else
63 {
64 if (len <= _size)
65 {
66 _length = len;
67 return;
68 }
69 size_t old_length = _length;
70 _length = len;
71 _data = g_bufferPool.Realloc(_data, _length, _index, _size);
72 memset(_data + old_length, 0, _length - old_length);
73 }
74 }
75 void reserve(size_t size)
76 {
77 size_t len = _length;
78 resize(size);
79 _length = len;
80 }
81 bool empty() { return _length == 0; }
82
83 void clear()
84 {
85 _length = 0;
86 }
87 auto begin() { return _data ? _data : _ssoData; }
88 auto end() { return (_data ? _data : _ssoData) + _length; }
89
90 void append(void* p, size_t len)
91 {
92 if (p == nullptr || len == 0)
93 return;
94 size_t old_length = _length;
95 resize(_length + len);
96 memcpy(data() + old_length, p, len);
97 }
98
99 std::byte& operator[](size_t index)
100 {
101 return data()[index];
102 }
103
105 {
106 resize(b._length);
107 memcpy(data(), b.data(), b.size());
108 return *this;
109 }
110
112 _data{ nullptr },
113 _length{0}
114 {
115 memset(_ssoData, 0, BUFFER_SSO_SIZE);
116 }
117
118 Buffer(size_t len) : Buffer()
119 {
120 resize(len);
121 }
122
123 Buffer(const Buffer& b) : Buffer()
124 {
125 resize(b._length);
126 memcpy(data(), b.data(), b.size());
127 }
128
129 Buffer(const std::byte* beg, const std::byte* end) : Buffer()
130 {
131 resize(end - beg);
132 memcpy(data(), beg, _length);
133 }
134
136 {
137 if (!_data)
138 return;
139 g_bufferPool.Free(_data, _index, _size);
140 }
141private:
142 std::byte* _data;
143 size_t _length;
144 union
145 {
146 struct
147 {
148 size_t _index; // 池索引
149 size_t _size; // 实际大小
150 };
152 };
153};
154
156{
157public:
158 std::byte* data() { return _data; }
159 const std::byte* data() const { return _data; }
160
161 size_t size() const { return _length; }
162
163 bool empty() { return _length == 0; }
164
165 auto begin() { return _data; }
166 auto end() { return _data + _length; }
167
168 std::byte& operator[](size_t index)
169 {
170 return _data[index];
171 }
172
174 _data{ nullptr },
175 _length{ 0 }
176 {}
177
178 BufferView(const std::byte* beg, const std::byte* end)
179 {
180 _length = end - beg;
181 _data = (std::byte*)beg;
182 }
183
184 BufferView(const std::byte* beg, size_t len)
185 {
186 _length = len;
187 _data = (std::byte*)beg;
188 }
189
191 {
192 _data = (std::byte*)b.data();
193 _length = b.size();
194 }
195private:
196 std::byte* _data;
197 size_t _length;
198};
199
202{
203 return buf;
204}
205
206// Buffer -> BufferView 隐式转换,要求buf不是右值
207// BufferView -> Buffer 使用 CreateBuffer{ buf_view }
208
209
210
212inline BufferView CreateBufferView(std::string_view str)
213{
214 return { (std::byte*)str.data(), str.size() };
215}
216
217inline BufferView CreateBufferView(char* p, size_t i)
218{
219 return { (std::byte*)p, i };
220}
221inline BufferView CreateBufferView(unsigned char* p, size_t i)
222{
223 return { (std::byte*)p, i };
224}
226{
227 return { buf.data(), i };
228}
229template<typename T, size_t N>
230BufferView CreateBufferView(const std::array<T, N>& v)
231{
232 return { (std::byte*)v.data(), v.size() };
233}
234template<typename T>
235BufferView CreateBufferView(const std::vector<T>& v)
236{
237 return { (std::byte*)v.data(), v.size() };
238}
239
240template<IsPod T>
242{
243 return { (std::byte*)&a, (std::byte*)(&a + 1) };
244}
245
247inline void BufferAppend(Buffer& buf, BufferView buf_app)
248{
249 size_t size_old = buf.size();
250 size_t size_app = buf_app.size();
251 buf.resize(size_old + size_app);
252 memcpy(buf.data() + size_old, buf_app.data(), size_app);
253}
254
257{
258 return { buf.data(), buf.data() + buf.size() };
259}
260
262Buffer CreateBufferHex(std::string_view str);
263
265//template<size_t N>
266//std::array<std::byte, N> CreateArrayHex(std::string_view str)
267//{
268// std::array<std::byte, N> ret = {};
269// Buffer buf = CreateBufferHex(str);
270// if (buf.size() != N)
271// log_warn("长度不匹配!");
272// memcpy(ret.data(), buf.data(), (std::min)(buf.size(), N));
273// return ret;
274//}
275//
276//template<size_t N>
277//void from_string_hex(std::string_view str, std::array<std::byte, N>& arr)
278//{
279// arr = CreateArrayHex<N>(str);
280//}
281
282template<IsPod T>
283inline void BufferCast(BufferView buf, T& a)
284{
285 if (sizeof(T) != buf.size())
286 {
287 log_err("数据大小不匹配:{}->{}", buf.size(), sizeof(T));
288 return;
289 }
290 memcpy(&a, buf.data(), sizeof(T));
291}
292
294//struct BufferRef
295//{
296// BufferRef(Buffer& buf)
297// {
298// _p = buf.data();
299// _size = buf.size();
300// }
301// BufferRef(unsigned char* p, size_t size)
302// {
303// _p = (Byte*)p;
304// _size = size;
305// }
306// BufferRef(std::vector<char>& vec)
307// {
308// _p = (Byte*)vec.data();
309// _size = vec.size();
310// }
311//
312// Byte* data() { return _p; }
313// size_t size() { return _size; }
314// bool empty() { return _size == 0 || !_p; }
315//private:
316// Byte* _p;
317// size_t _size;
318//};
319//
321//std::string toString(const Buffer& buf, bool add_tag = false);
322//
323//
324//Buffer createBuffer(unsigned char* p, size_t size);
325//Buffer createBuffer(std::vector<char>& vec);
326//Buffer createBuffer(const std::string& path_name);
327//
329//struct BufferView
330//{
331// BufferView(BufferRef buf)
332// {
333// _p = buf.data();
334// _size = buf.size();
335// }
336// BufferView(Buffer& buf)
337// {
338// _p = buf.data();
339// _size = buf.size();
340// }
341// BufferView(const unsigned char* p, size_t size)
342// {
343// _p = (const Byte*)p;
344// _size = size;
345// }
346// BufferView(const std::vector<char>& vec)
347// {
348// _p = (const Byte*)vec.data();
349// _size = vec.size();
350// }
351// BufferView(const std::string& str)
352// {
353// _p = (const Byte*)str.data();
354// _size = str.size();
355// }
356//
357// const Byte* data() { return _p; }
358// size_t size() { return _size; }
359// bool empty() { return _size == 0 || !_p; }
360//private:
361// const Byte* _p;
362// size_t _size;
363//};
364
365}
366
size_t size() const
Buffer & operator=(const Buffer &b)
void reserve(size_t size)
Buffer(const std::byte *beg, const std::byte *end)
void resize(size_t len)
Buffer(const Buffer &b)
std::byte _ssoData[BUFFER_SSO_SIZE]
std::byte * data()
Buffer(size_t len)
std::byte & operator[](size_t index)
const std::byte * data() const
void append(void *p, size_t len)
size_t size() const
BufferView(const std::byte *beg, const std::byte *end)
BufferView(const Buffer &b)
BufferView(const std::byte *beg, size_t len)
std::byte & operator[](size_t index)
const std::byte * data() const
std::byte * data()
用于模板限定,更好的报错信息
日志系统
#define log_err(...)
void BufferCast(BufferView buf, T &a)
从16进制字符串创建array
Buffer CreateBufferHex(std::string_view str)
从16进制字符串创建buffer
BufferPool g_bufferPool
Buffer CreateBuffer(BufferView buf)
创建副本
constexpr size_t BUFFER_SSO_SIZE
void BufferAppend(Buffer &buf, BufferView buf_app)
追加数据
BufferView CreateBufferView(const Buffer &buf)
创建buffer视图(右值不能默认转换)