libfilezilla
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
19 class FZ_PUBLIC_SYMBOL buffer final
20 {
21 public:
22  buffer() = default;
23 
25  explicit buffer(size_t capacity);
26 
27  buffer(buffer const& buf);
28  buffer(buffer && buf);
29 
30  ~buffer() { delete data_; }
31 
32  buffer& operator=(buffer const& buf);
33  buffer& operator=(buffer && buf);
34 
35  // Undefined if buffer is empty
36  unsigned char* get() { return pos_; }
37 
42  unsigned char* get(size_t write_size);
43 
44  // Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t write_size)
45  void add(size_t added);
46 
51  void consume(size_t consumed);
52 
53  size_t size() const { return size_; }
54 
58  void clear();
59 
64  void append(unsigned char const* data, size_t len);
65  void append(std::string const& str);
66 
67  bool empty() const { return size_ == 0; }
68  explicit operator bool() const {
69  return size_ != 0;
70  }
71 
72  void reserve(size_t capacity);
73 
75  unsigned char operator[](size_t i) const { return pos_[i]; }
76  unsigned char & operator[](size_t i) { return pos_[i]; }
77 private:
78 
79  // Invariants:
80  // size_ <= capacity_
81  // data_ <= pos_
82  // pos_ <= data_ + capacity_
83  // pos_ + size_ <= data_ + capacity_
84  unsigned char* data_{};
85  unsigned char* pos_{};
86  size_t size_{};
87  size_t capacity_{};
88 };
89 
90 }
91 
92 #endif
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:19
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:75