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);
33 
34  ~buffer() { delete[] data_; }
35 
36  buffer& operator=(buffer const& buf);
37  buffer& operator=(buffer && buf);
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 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 
94  unsigned char operator[](size_t i) const { return pos_[i]; }
95  unsigned char & operator[](size_t i) { return pos_[i]; }
96 
97  bool operator==(buffer const& rhs) const;
98 
99  bool operator!=(buffer const& rhs) const {
100  return !(*this == rhs);
101  }
102 private:
103 
104  // Invariants:
105  // size_ <= capacity_
106  // data_ <= pos_
107  // pos_ <= data_ + capacity_
108  // pos_ + size_ <= data_ + capacity_
109  unsigned char* data_{};
110  unsigned char* pos_{};
111  size_t size_{};
112  size_t capacity_{};
113 };
114 
115 }
116 
117 #endif
The namespace used by libfilezilla.
Definition: apply.hpp:16
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 operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:94