libfilezilla
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
12namespace fz {
13
25class FZ_PUBLIC_SYMBOL buffer final
26{
27public:
28 buffer() = default;
29
31 explicit buffer(size_t capacity);
32
33 buffer(buffer const& buf);
34 buffer(buffer && buf) noexcept;
35
36 ~buffer() { delete[] data_; }
37
38 buffer& operator=(buffer const& buf);
39 buffer& operator=(buffer && buf) noexcept;
40
42 unsigned char const* get() const { return pos_; }
43 unsigned char* get() { return pos_; }
44
63 unsigned char* get(size_t write_size);
64
66 void add(size_t added);
67
72 void consume(size_t consumed);
73
74 size_t size() const { return size_; }
75
79 void clear();
80
85 void append(unsigned char const* data, size_t len);
86 void append(std::string_view const& str);
87 void append(std::vector<uint8_t> const& data);
88 void append(unsigned char v);
89
90 bool empty() const { return size_ == 0; }
91 explicit operator bool() const {
92 return size_ != 0;
93 }
94
95 size_t capacity() const { return capacity_; }
96 void reserve(size_t capacity);
97
98 void resize(size_t size);
99
101 unsigned char operator[](size_t i) const { return pos_[i]; }
102 unsigned char & operator[](size_t i) { return pos_[i]; }
103
104 bool operator==(buffer const& rhs) const;
105
106 bool operator!=(buffer const& rhs) const {
107 return !(*this == rhs);
108 }
109private:
110
111 // Invariants:
112 // size_ <= capacity_
113 // data_ <= pos_
114 // pos_ <= data_ + capacity_
115 // pos_ + size_ <= data_ + capacity_
116 unsigned char* data_{};
117 unsigned char* pos_{};
118 size_t size_{};
119 size_t capacity_{};
120};
121
122}
123
124#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:26
buffer(size_t capacity)
Initially reserves the passed capacity.
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:42
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:101
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
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.