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 
12 namespace fz {
13 
25 class FZ_PUBLIC_SYMBOL buffer final
26 {
27 public:
28  buffer() noexcept = 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  }
109 
110  std::string_view to_view() const;
111 private:
112 
113  // Invariants:
114  // size_ <= capacity_
115  // data_ <= pos_
116  // pos_ <= data_ + capacity_
117  // pos_ + size_ <= data_ + capacity_
118  unsigned char* data_{};
119  unsigned char* pos_{};
120  size_t size_{};
121  size_t capacity_{};
122 };
123 
124 }
125 
126 #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:25
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:101