-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwtf_buffer.cpp
79 lines (65 loc) · 1.34 KB
/
wtf_buffer.cpp
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
#include "wtf_buffer.h"
wtf_buffer::wtf_buffer(size_t size) : _buf(size)
{
end = _buf.data();
}
wtf_buffer::wtf_buffer(std::vector<char> buf) : _buf(std::move(buf))
{
end = _buf.data();
}
size_t wtf_buffer::capacity() const noexcept
{
return _buf.size();
}
size_t wtf_buffer::size() const noexcept
{
return static_cast<size_t>(end - _buf.data());
}
size_t wtf_buffer::available() const noexcept
{
return _buf.size() - size();
}
char *wtf_buffer::data() noexcept
{
return _buf.data();
}
const char *wtf_buffer::data() const noexcept
{
return _buf.data();
}
void wtf_buffer::reserve(size_t size)
{
size_t s = this->size();
_buf.resize(size);
end = _buf.data() + s;
}
void wtf_buffer::resize(size_t size)
{
if (size > capacity())
_buf.resize(size);
end = _buf.data() + size;
}
void wtf_buffer::clear() noexcept
{
end = _buf.data();
if (on_clear)
on_clear();
}
void wtf_buffer::swap(wtf_buffer &other) noexcept
{
_buf.swap(other._buf);
std::swap(end, other.end);
// DO NOT swap on_clear!
}
wtf_buffer::wtf_buffer(wtf_buffer &&src) : end(src.end), _buf(std::move(src._buf))
{
// end stays valid
//_buf.swap(src._buf);
}
wtf_buffer &wtf_buffer::operator=(wtf_buffer &&src)
{
// end stays valid
if (this != &src)
_buf.swap(src._buf);
return *this;
}