Some methods of managed_bytes contain the logic needed to read/write the
contents of managed_bytes, even though this logic is already present in
managed_bytes_{,mutable}_view.
Reimplementing those methods by using the views as intermediates allows us to
remove some code and makes the responsibilities cleaner -- after the change,
managed_bytes contains the logic of allocating and freeing the storage,
while views provide read/write access to the storage.
This change will simplify the next patch which changes the internals of
managed_bytes.
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
/*
|
|
* Copyright (C) 2017-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string_view>
|
|
#include <seastar/core/sstring.hh>
|
|
|
|
#include "seastarx.hh"
|
|
|
|
template<typename CharT>
|
|
class basic_mutable_view {
|
|
CharT* _begin = nullptr;
|
|
CharT* _end = nullptr;
|
|
public:
|
|
using value_type = CharT;
|
|
using pointer = CharT*;
|
|
using iterator = CharT*;
|
|
using const_iterator = CharT*;
|
|
|
|
basic_mutable_view() = default;
|
|
|
|
template<typename U, U N>
|
|
basic_mutable_view(basic_sstring<CharT, U, N>& str)
|
|
: _begin(str.begin())
|
|
, _end(str.end())
|
|
{ }
|
|
|
|
basic_mutable_view(CharT* ptr, size_t length)
|
|
: _begin(ptr)
|
|
, _end(ptr + length)
|
|
{ }
|
|
|
|
operator std::basic_string_view<CharT>() const noexcept {
|
|
return std::basic_string_view<CharT>(begin(), size());
|
|
}
|
|
|
|
CharT& operator[](size_t idx) const { return _begin[idx]; }
|
|
|
|
iterator begin() const { return _begin; }
|
|
iterator end() const { return _end; }
|
|
|
|
CharT* data() const { return _begin; }
|
|
size_t size() const { return _end - _begin; }
|
|
bool empty() const { return _begin == _end; }
|
|
CharT& front() const { return *_begin; }
|
|
|
|
void remove_prefix(size_t n) {
|
|
_begin += n;
|
|
}
|
|
void remove_suffix(size_t n) {
|
|
_end -= n;
|
|
}
|
|
|
|
basic_mutable_view substr(size_t pos, size_t count) {
|
|
size_t n = std::min(count, (_end - _begin) - pos);
|
|
return basic_mutable_view{_begin + pos, n};
|
|
}
|
|
};
|