libfilezilla
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_BUFFER_HEADER
2#define LIBFILEZILLA_BUFFER_HEADER
3
4#include "libfilezilla.hpp"
5
6#include <vector>
7#include <type_traits>
8#include <cstdint>
9
13
14namespace fz {
15
27class FZ_PUBLIC_SYMBOL buffer final
28{
29public:
30 typedef unsigned char value_type;
31
32 buffer() noexcept = default;
33
35 explicit buffer(size_t capacity);
36
37 buffer(buffer const& buf);
38 buffer(buffer && buf) noexcept;
39
40 ~buffer() { delete[] data_; }
41
42 buffer& operator=(buffer const& buf);
43 buffer& operator=(buffer && buf) noexcept;
44
46 unsigned char const* get() const { return pos_; }
47 unsigned char* get() { return pos_; }
48
50 unsigned char* data() { return pos_; }
51 unsigned char const* data() const { return pos_; }
52
71 unsigned char* get(size_t write_size);
72
74 void add(size_t added);
75
82 template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
83 void add(T added) {
84 if (added > 0) {
85 add(static_cast<size_t>(added));
86 }
87 }
88
93 void consume(size_t consumed);
94
95 size_t size() const { return size_; }
96
100 void clear();
101
102 void clear_and_free();
103
108 void append(unsigned char const* data, size_t len);
109 void append(std::string_view const& str);
110 void append(std::basic_string_view<uint8_t> const& data);
111 void append(std::vector<uint8_t> const& data);
112 void append(fz::buffer const& b);
113 void append(unsigned char v);
114 void append(size_t len, unsigned char c);
115
116 buffer& operator+=(unsigned char v) {
117 append(v);
118 return *this;
119 }
120 buffer& operator+=(std::string_view const& str) {
121 append(str);
122 return *this;
123 }
124 buffer& operator+=(std::vector<uint8_t> const& data) {
125 append(data);
126 return *this;
127 }
128 buffer& operator+=(fz::buffer const& b) {
129 append(b);
130 return *this;
131 }
132
133 bool empty() const { return size_ == 0; }
134 explicit operator bool() const {
135 return size_ != 0;
136 }
137
138 size_t capacity() const { return capacity_; }
139 void reserve(size_t capacity);
140
141 void resize(size_t size);
142
144 unsigned char operator[](size_t i) const { return pos_[i]; }
145 unsigned char & operator[](size_t i) { return pos_[i]; }
146
147 bool operator==(buffer const& rhs) const;
148
149 bool operator!=(buffer const& rhs) const {
150 return !(*this == rhs);
151 }
152
153 std::string_view to_view() const;
154
155 void wipe();
156 void wipe_unused();
157
158private:
159
160 // Invariants:
161 // size_ <= capacity_
162 // data_ <= pos_
163 // pos_ <= data_ + capacity_
164 // pos_ + size_ <= data_ + capacity_
165 unsigned char* data_{};
166 unsigned char* pos_{};
167 size_t size_{};
168 size_t capacity_{};
169};
170
171inline void wipe(buffer & b) {
172 b.wipe();
173}
174
175inline void wipe_unused(buffer & b) {
176 b.wipe_unused();
177}
178
179}
180
181#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:28
void add(T added)
Overload of add for signed types, only adds if value is positive.
Definition buffer.hpp:83
buffer(size_t capacity)
Initially reserves the passed capacity.
unsigned char * data()
Same as get().
Definition buffer.hpp:50
void consume(size_t consumed)
Removes consumed bytes from the beginning of the buffer.
unsigned char * get(size_t write_size)
Returns a writable buffer guaranteed to be large enough for write_size bytes, call add when done.
unsigned char const * get() const
Undefined if buffer is empty.
Definition buffer.hpp:46
void clear()
void append(unsigned char const *data, size_t len)
Appends the passed data to the buffer.
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition buffer.hpp:144
void add(size_t added)
Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t...
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17