libfilezilla
Loading...
Searching...
No Matches
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
10namespace fz {
11
22class FZ_PUBLIC_SYMBOL nonowning_buffer final
23{
24public:
25 constexpr nonowning_buffer() noexcept = default;
26
27 explicit nonowning_buffer(uint8_t *buffer, size_t capacity) noexcept
28 : buffer_(buffer)
29 , capacity_(capacity)
30 {
31 }
32
33 explicit nonowning_buffer(uint8_t *buffer, size_t capacity, size_t size) noexcept
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 &&) noexcept = default;
48 nonowning_buffer& operator=(nonowning_buffer &&) noexcept = 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 void clear() { resize(0); }
65
67 uint8_t operator[](size_t offset) { return *(buffer_ + start_ + offset); }
68
70 uint8_t const* get() const { return buffer_ + start_; }
71 uint8_t * get() { return buffer_ + start_; }
72
81 uint8_t* get(size_t bytes);
82
88 void add(size_t bytes);
89
94 void consume(size_t bytes);
95
96 void reset();
97
98 void append(uint8_t const* data, size_t len);
99 void append(uint8_t c) { append(&c, 1); }
100
101 std::string_view to_view() const;
102
103private:
104 uint8_t* buffer_{};
105 size_t capacity_{};
106 size_t size_{};
107 size_t start_{};
108};
109}
110
111#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:27
Similar to fz::buffer, but does not own memory.
Definition: nonowning_buffer.hpp:23
void consume(size_t bytes)
Removes consumed bytes from the beginning of the buffer.
void resize(size_t size)
Resizes the buffer.
uint8_t operator[](size_t offset)
Gets element at offset. No safety check.
Definition: nonowning_buffer.hpp:67
uint8_t const * get() const
Gets buffer.
Definition: nonowning_buffer.hpp:70
uint8_t * get(size_t bytes)
Returns a writable buffer guaranteed to be large enough for write_size bytes, call add when done.
void add(size_t bytes)
Grows size by passed amount.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17