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 
10 namespace fz {
11 
23 class FZ_PUBLIC_SYMBOL buffer final
24 {
25 public:
26  buffer() = default;
27 
29  explicit buffer(size_t capacity);
30 
31  buffer(buffer const& buf);
32  buffer(buffer && buf) noexcept;
33 
34  ~buffer() { delete[] data_; }
35 
36  buffer& operator=(buffer const& buf);
37  buffer& operator=(buffer && buf) noexcept;
38 
40  unsigned char const* get() const { return pos_; }
41  unsigned char* get() { return pos_; }
42 
61  unsigned char* get(size_t write_size);
62 
64  void add(size_t added);
65 
70  void consume(size_t consumed);
71 
72  size_t size() const { return size_; }
73 
77  void clear();
78 
83  void append(unsigned char const* data, size_t len);
84  void append(std::string_view const& str);
85 
86  bool empty() const { return size_ == 0; }
87  explicit operator bool() const {
88  return size_ != 0;
89  }
90 
91  void reserve(size_t capacity);
92 
93  void resize(size_t size);
94 
96  unsigned char operator[](size_t i) const { return pos_[i]; }
97  unsigned char & operator[](size_t i) { return pos_[i]; }
98 
99  bool operator==(buffer const& rhs) const;
100 
101  bool operator!=(buffer const& rhs) const {
102  return !(*this == rhs);
103  }
104 private:
105 
106  // Invariants:
107  // size_ <= capacity_
108  // data_ <= pos_
109  // pos_ <= data_ + capacity_
110  // pos_ + size_ <= data_ + capacity_
111  unsigned char* data_{};
112  unsigned char* pos_{};
113  size_t size_{};
114  size_t capacity_{};
115 };
116 
117 }
118 
119 #endif
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
The namespace used by libfilezilla.
Definition: apply.hpp:17
Sets some global macros and further includes string.hpp.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:23
unsigned char const * get() const
Undefined if buffer is empty.
Definition: buffer.hpp:40
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:96