From 0b20c7ef6506de0c4f9205fe87cf2cc82cfb78e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Chojnowski?= Date: Mon, 23 Nov 2020 15:49:37 +0100 Subject: [PATCH] serializer: implement FragmentedView for buffer_view buffer_view is one of the types we want to directly deserialize from. --- serializer.hh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/serializer.hh b/serializer.hh index 4eeec573fa..3c577a52bb 100644 --- a/serializer.hh +++ b/serializer.hh @@ -140,6 +140,36 @@ public: return !_total_size; } + // FragmentedView implementation + void remove_prefix(size_t n) { + while (n >= _first.size() && n > 0) { + n -= _first.size(); + remove_current(); + } + _total_size -= n; + _first.remove_prefix(n); + } + void remove_current() { + _total_size -= _first.size(); + if (_total_size) { + auto next_data = reinterpret_cast((*_next).begin()); + size_t next_size = std::min(_total_size, (*_next).size()); + _first = bytes_view(next_data, next_size); + ++_next; + } else { + _first = bytes_view(); + } + } + buffer_view prefix(size_t n) const { + auto tmp = *this; + tmp._total_size = std::min(tmp._total_size, n); + tmp._first = tmp._first.substr(0, n); + return tmp; + } + bytes_view current_fragment() { + return _first; + } + bytes linearize() const { bytes b(bytes::initialized_later(), size_bytes()); using boost::range::for_each; @@ -164,6 +194,7 @@ public: return fn(bv); } }; +static_assert(FragmentedView>); using size_type = uint32_t;