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 "buffer.hpp"
5
9
10namespace fz {
11
19class FZ_PUBLIC_SYMBOL nonowning_buffer final : public buffer_access_interface
20{
21public:
22 constexpr nonowning_buffer() noexcept = default;
23
24 explicit nonowning_buffer(uint8_t *buffer, size_t capacity) noexcept
25 : buffer_(buffer)
26 , capacity_(capacity)
27 {
28 }
29
30 explicit nonowning_buffer(uint8_t *buffer, size_t capacity, size_t size) noexcept
31 : buffer_(buffer)
32 , capacity_(capacity)
33 , size_(size)
34 {
35 if (size > capacity) {
36 abort();
37 }
38 }
39
40 // Copy is shallow!
41 nonowning_buffer(nonowning_buffer const&) = default;
42 nonowning_buffer& operator=(nonowning_buffer const&) = default;
43
44 nonowning_buffer(nonowning_buffer &&) noexcept = default;
45 nonowning_buffer& operator=(nonowning_buffer &&) noexcept = default;
46
47 ~nonowning_buffer() noexcept override;
48
49 size_t capacity() const override { return capacity_; }
50 size_t size() const override { return size_; }
51 bool empty() const { return size_ == 0; }
52 explicit operator bool() const { return !empty(); }
53
60 void resize(size_t size) override;
61 void clear() override { resize(0); }
62
64 uint8_t operator[](size_t offset) { return *(buffer_ + start_ + offset); }
65
70 uint8_t const* get() const override { return buffer_ ? buffer_ + start_ : nullptr; }
71 uint8_t * get() override { return buffer_ ? buffer_ + start_ : nullptr; }
72 uint8_t* get(size_t bytes) override;
73 void add(size_t bytes) override;
75
80 void consume(size_t bytes) override;
81
82 void reset();
83
84 void append(uint8_t const* data, size_t len) override;
85 void append(uint8_t c) { append(&c, 1); }
86 void append(std::string_view const& str);
87
88 std::string_view to_view() const;
89
90private:
91 uint8_t* buffer_{};
92 size_t capacity_{};
93 size_t size_{};
94 size_t start_{};
95};
96}
97
98#endif
Declares fz::buffer.
Definition buffer.hpp:360
void resize(size_t size) override
Resizes the buffer.
uint8_t operator[](size_t offset)
Gets element at offset. No safety check.
Definition nonowning_buffer.hpp:64
void consume(size_t bytes) override
Removes consumed bytes from the beginning of the buffer.
The namespace used by libfilezilla.
Definition apply.hpp:17