commit 5373682cca46905b4bdd3615398950bebf5aa938 parent 55efe851bebd645ad4233b7b6ace78878eaaaaf0 Author: Alexandre BIQUE <bique.alexandre@gmail.com> Date: Mon, 12 Jul 2021 18:05:30 +0200 Prepare a byte buffer class Diffstat:
A | examples/plugins/buffer.hh | | | 36 | ++++++++++++++++++++++++++++++++++++ |
1 file changed, 36 insertions(+), 0 deletions(-)
diff --git a/examples/plugins/buffer.hh b/examples/plugins/buffer.hh @@ -0,0 +1,35 @@ +#pragma once + +#include <array> +#include <cstddef> +#include <cstdint> +#include <cstring> + +namespace clap { + template <size_t CAPACITY> + class Buffer { + + const uint8_t *readData() const noexcept { return &data_[roff_]; } + size_t readAvail() const noexcept { return woff_ - roff_; } + void read(size_t bytes) noexcept { roff_ += bytes; } + + uint8_t *writeData() const noexcept { return &data_[woff_]; } + size_t writeAvail() const noexcept { return CAPACITY - woff_; } + void wrote(size_t bytes) noexcept { woff_ += bytes; } + + void rewind() { + if (woff_ == 0) + return; + + // this is inefficient but simple + // TODO: use scatter/gather IO + std::memmove(&data_[0], &data_[roff_], woff_ - roff_); + woff_ -= roff_; + roff_ = 0; + } + + std::array<uint8_t, CAPACITY> data_; + size_t roff_ = 0; + size_t woff_ = 0; + }; +} // namespace clap +\ No newline at end of file