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
106 void append(unsigned char const* data, size_t len);
107 void append(std::string_view const& str);
108 void append(std::basic_string_view<uint8_t> const& data);
109 void append(std::vector<uint8_t> const& data);
110 void append(fz::buffer const& b);
111 void append(unsigned char v);
112 void append(size_t len, unsigned char c);
113
114 buffer& operator+=(unsigned char v) {
115 append(v);
116 return *this;
117 }
118 buffer& operator+=(std::string_view const& str) {
119 append(str);
120 return *this;
121 }
122 buffer& operator+=(std::vector<uint8_t> const& data) {
123 append(data);
124 return *this;
125 }
126 buffer& operator+=(fz::buffer const& b) {
127 append(b);
128 return *this;
129 }
130
131 bool empty() const { return size_ == 0; }
132 explicit operator bool() const {
133 return size_ != 0;
134 }
135
136 size_t capacity() const { return capacity_; }
137 void reserve(size_t capacity);
138
139 void resize(size_t size);
140
142 unsigned char operator[](size_t i) const { return pos_[i]; }
143 unsigned char & operator[](size_t i) { return pos_[i]; }
144
145 bool operator==(buffer const& rhs) const;
146
147 bool operator!=(buffer const& rhs) const {
148 return !(*this == rhs);
149 }
150
151 std::string_view to_view() const;
152
153 void wipe();
154 void wipe_unused();
155
156private:
157
158 // Invariants:
159 // size_ <= capacity_
160 // data_ <= pos_
161 // pos_ <= data_ + capacity_
162 // pos_ + size_ <= data_ + capacity_
163 unsigned char* data_{};
164 unsigned char* pos_{};
165 size_t size_{};
166 size_t capacity_{};
167};
168
169inline void FZ_PUBLIC_SYMBOL wipe(buffer & b) {
170 b.wipe();
171}
172
173inline void FZ_PUBLIC_SYMBOL wipe_unused(buffer & b) {
174 b.wipe_unused();
175}
176
177}
178
179#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:142
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