libfilezilla
nonowning_buffer.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_NONOWNING_BUFFER_HEADER
2 #define LIBFILEZILLA_NONOWNING_BUFFER_HEADER
3 
4 #include "libfilezilla.hpp"
5 
10 namespace fz {
11 
22 class FZ_PUBLIC_SYMBOL nonowning_buffer final
23 {
24 public:
25  nonowning_buffer() = default;
26 
27  explicit nonowning_buffer(uint8_t *buffer, size_t capacity)
28  : buffer_(buffer)
29  , capacity_(capacity)
30  {
31  }
32 
33  explicit nonowning_buffer(uint8_t *buffer, size_t capacity, size_t size)
34  : buffer_(buffer)
35  , capacity_(capacity)
36  , size_(size)
37  {
38  if (size > capacity) {
39  abort();
40  }
41  }
42 
43  // Copy is shallow!
44  nonowning_buffer(nonowning_buffer const&) = default;
45  nonowning_buffer& operator=(nonowning_buffer const&) = default;
46 
47  nonowning_buffer(nonowning_buffer &&) = default;
48  nonowning_buffer& operator=(nonowning_buffer &&) = default;
49 
50  ~nonowning_buffer() noexcept = default;
51 
52  size_t capacity() const { return capacity_; }
53  size_t size() const { return size_; }
54  bool empty() const { return size_ == 0; }
55  explicit operator bool() const { return !empty(); }
56 
63  void resize(size_t size);
64 
66  uint8_t operator[](size_t offset) { return *(buffer_ + start_ + offset); }
67 
69  uint8_t const* get() const { return buffer_ + start_; }
70  uint8_t * get() { return buffer_ + start_; }
71 
80  uint8_t* get(size_t bytes);
81 
87  void add(size_t bytes);
88 
93  void consume(size_t bytes);
94 
95  void reset();
96 
97  void append(uint8_t const* data, size_t len);
98  void append(uint8_t c) { append(&c, 1); }
99 
100 private:
101  uint8_t* buffer_{};
102  size_t capacity_{};
103  size_t size_{};
104  size_t start_{};
105 };
106 }
107 
108 #endif
Similar to fz::buffer, but does not own memory.
Definition: nonowning_buffer.hpp:22
uint8_t operator[](size_t offset)
Gets element at offset. No safety check.
Definition: nonowning_buffer.hpp:66
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:26