aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/buffer.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/yson_pull/buffer.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yson_pull/buffer.h')
-rw-r--r--library/cpp/yson_pull/buffer.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/yson_pull/buffer.h b/library/cpp/yson_pull/buffer.h
new file mode 100644
index 0000000000..04c9220ef3
--- /dev/null
+++ b/library/cpp/yson_pull/buffer.h
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <util/system/types.h>
+#include <util/system/yassert.h>
+
+#include <cstddef>
+
+namespace NYsonPull {
+ //! \brief A non-owning buffer model.
+ //!
+ //! Represents a \p pos pointer moving between \p begin and \p end.
+ template <typename T>
+ class buffer {
+ T* begin_ = nullptr;
+ T* pos_ = nullptr;
+ T* end_ = nullptr;
+
+ public:
+ T* begin() const noexcept {
+ return begin_;
+ }
+ T* pos() const noexcept {
+ return pos_;
+ }
+ T* end() const noexcept {
+ return end_;
+ }
+
+ //! \brief Amount of data after current position.
+ size_t available() const noexcept {
+ return end_ - pos_;
+ }
+
+ //! \brief Amount of data before current position.
+ size_t used() const noexcept {
+ return pos_ - begin_;
+ }
+
+ //! \brief Move current position \p nbytes forward.
+ void advance(size_t nbytes) noexcept {
+ Y_ASSERT(pos_ + nbytes <= end_);
+ pos_ += nbytes;
+ }
+
+ //! \brief Reset buffer pointers.
+ void reset(T* new_begin, T* new_end, T* new_pos) {
+ begin_ = new_begin;
+ pos_ = new_pos;
+ end_ = new_end;
+ }
+
+ //! \brief Reset buffer to beginning
+ void reset(T* new_begin, T* new_end) {
+ reset(new_begin, new_end, new_begin);
+ }
+ };
+
+ class output_buffer: public buffer<ui8> {
+ public:
+ //! \brief An output buffer is empty when there is no data written to it.
+ bool is_empty() const noexcept {
+ return pos() == begin();
+ }
+
+ //! \brief An output buffer is full when there is no space to write more data to it.
+ bool is_full() const noexcept {
+ return pos() == end();
+ }
+ };
+
+ class input_buffer: public buffer<const ui8> {
+ public:
+ //! An input stream is empty when there is no data to read in it.
+ bool is_empty() const noexcept {
+ return pos() == end();
+ }
+ };
+
+}