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 
55  void clear();
56 
61  void append(unsigned char const* data, size_t len);
62  void append(std::string const& str);
63 
64  bool empty() const { return size_ == 0; }
65  explicit operator bool() const {
66  return size_ != 0;
67  }
68 
69  void reserve(size_t capacity);
70 
72  unsigned char operator[](size_t i) const { return pos_[i]; }
73  unsigned char & operator[](size_t i) { return pos_[i]; }
74 private:
75 
76  // Invariants:
77  // size_ <= capacity_
78  // data_ <= pos_
79  // pos_ <= data_ + capacity_
80  // pos_ + size_ <= data_ + capacity_
81  unsigned char* data_{};
82  unsigned char* pos_{};
83  size_t size_{};
84  size_t capacity_{};
85 };
86 
87 }
88 
89 #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:19
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:72