Newer
Older
#include <vereign/bytes/errors.hh>
#include <memory>
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
namespace vereign::bytes {
Buffer::Buffer() noexcept
: cap_{0},
size_{0},
data_{nullptr}
{
}
Buffer::Buffer(std::size_t cap)
: cap_{cap},
size_{0},
data_{nullptr}
{
if (cap == 0) {
return;
}
data_ = reinterpret_cast<uint8_t*>(std::malloc(cap));
if (data_ == nullptr) {
throw std::bad_alloc{};
}
}
Buffer::Buffer(bytes::View src)
: cap_{src.Size()},
size_{src.Size()},
data_{nullptr}
{
if (size_ == 0) {
return;
}
data_ = reinterpret_cast<uint8_t*>(std::malloc(cap_));
if (data_ == nullptr) {
throw std::bad_alloc{};
}
std::memcpy(data_, src.Data(), src.Size());
}
Buffer::Buffer(Buffer&& other) noexcept
: cap_{other.cap_},
size_{other.size_},
data_{other.data_}
{
other.cap_ = 0;
other.size_ = 0;
other.data_ = nullptr;
}
auto Buffer::operator=(Buffer&& other) noexcept -> Buffer& {
std::swap(cap_, other.cap_);
std::swap(size_, other.size_);
std::swap(data_, other.data_);
return *this;
}
Buffer::~Buffer() {
std::free(data_);
}
auto Buffer::Size() const noexcept -> std::size_t {
return size_;
}
auto Buffer::FreeCap() const noexcept -> std::size_t {
return cap_ - size_;
}
auto Buffer::Cap() const noexcept -> std::size_t {
return cap_;
}
auto Buffer::begin() noexcept -> uint8_t* {
return data_;
}
auto Buffer::begin() const noexcept -> const uint8_t* {
return data_;
}
auto Buffer::end() noexcept -> uint8_t* {
return data_ + size_;
}
auto Buffer::end() const noexcept -> const uint8_t* {
return data_ + size_;
}
void Buffer::Reserve(std::size_t size) {
if (cap_ == 0) {
cap_ = size;
data_ = reinterpret_cast<uint8_t*>(std::malloc(cap_));
if (data_ == nullptr) {
throw std::bad_alloc{};
}
return;
}
if (size <= cap_ - size_) {
return;
}
auto cap = cap_ * 2;
if (size > cap - size_) {
cap = size_ + size;
}
auto newData = reinterpret_cast<uint8_t*>(std::realloc(data_, cap));
if (newData == nullptr) {
throw std::bad_alloc{};
}
data_ = newData;
cap_ = cap;
}
void Buffer::Reset() {
size_ = 0;
}
void Buffer::IncSize(std::size_t val) {
if (size_ + val > cap_) {
throw IncrementOutOfBounds{};
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
}
auto Buffer::WriteWithinCap(bytes::View src) noexcept -> std::size_t {
auto size = std::min(cap_ - size_, src.Size());
std::memcpy(data_ + size_, src.Data(), size);
size_ += size;
return size;
}
auto Buffer::Write(bytes::View src) -> std::size_t {
Reserve(src.Size());
return WriteWithinCap(src);
}
auto Buffer::View() const noexcept -> bytes::View {
return bytes::View{data_, size_};
}
auto Buffer::View(std::size_t start) const noexcept -> bytes::View {
return bytes::View{data_, size_}.Slice(start);
}
auto Buffer::operator[](std::size_t index) -> uint8_t& {
if (index >= cap_) {
}
return data_[index];
}
auto Buffer::operator[](std::size_t index) const -> const uint8_t& {
if (index >= cap_) {
}
return data_[index];
}
} // namespace vereign::bytes