srs-control 0.2.2
Loading...
Searching...
No Matches
LargeBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstddef>
5#include <fmt/base.h>
6#include <span>
7#include <string_view>
8
9namespace srs
10{
23 {
24 public:
29 LargeBuffer() = default;
30
38 explicit LargeBuffer(std::size_t buffer_size) { data_.resize(buffer_size); }
39
43 explicit LargeBuffer(const LargeBuffer& other) = delete;
44
52 explicit LargeBuffer(LargeBuffer&& other) noexcept = default;
53
57 auto operator=(const LargeBuffer& other) -> LargeBuffer& = delete;
58
66 auto operator=(LargeBuffer&& other) noexcept -> LargeBuffer& = default;
67
71 ~LargeBuffer() = default;
72
79 auto operator==(std::span<char> msg) const -> bool { return std::string_view{ msg } == data(); }
80
87 auto operator==(std::string_view msg) const -> bool { return msg == data(); }
88
94 [[nodiscard]] auto data() const -> std::string_view { return std::string_view{ data_.data(), size_ }; }
95 [[nodiscard]] auto empty() const -> bool { return data_.empty(); }
96
103 void resize(std::size_t size)
104 {
105 size_ = size;
106 data_.resize(size_ >= data_.size() ? size_ : data_.size());
107 }
108 // void set_buffer_size(std::size_t capacity) { data_.resize(capacity); }
109
115 [[nodiscard]] auto get_buffer_size() const -> std::size_t { return data_.size(); }
116
122 [[nodiscard]] auto get_size() const -> std::size_t { return size_; }
123
129 // INFO: Do not return the reference to the underlying vector, as it's not thread safe.
130 auto get_all_data() -> std::span<char> { return std::span{ data_ }; }
131
132 auto is_empty() -> bool { return data_.size() == 0 and data_.capacity() == 0; }
133
134 private:
135 std::size_t size_ = 0;
137 };
138
139} // namespace srs
~LargeBuffer()=default
Default destructor.
auto is_empty() -> bool
LargeBuffer()=default
Default constructor for an empty buffer. This does not allocate any memory.
auto data() const -> std::string_view
Return a string_view with the underlying data and current size.
std::size_t size_
auto operator==(std::span< char > msg) const -> bool
Comparison with a span.
auto get_size() const -> std::size_t
Getter for the size.
LargeBuffer(LargeBuffer &&other) noexcept=default
Move constructor via swapping.
auto operator=(const LargeBuffer &other) -> LargeBuffer &=delete
Default copy assignment. Very slow and should be avoided.
auto empty() const -> bool
LargeBuffer(std::size_t buffer_size)
Constructor with a buffer size.
void resize(std::size_t size)
Change the size of this buffer.
auto operator==(std::string_view msg) const -> bool
Comparison with a string_view.
LargeBuffer(const LargeBuffer &other)=delete
Default copy constructor. Very slow and should be avoided.
auto operator=(LargeBuffer &&other) noexcept -> LargeBuffer &=default
Move assignment via swapping.
auto get_buffer_size() const -> std::size_t
Getter for the buffer size.
auto get_all_data() -> std::span< char >
Return the total underlying data.
std::vector< BufferElementType > BinaryData