WIP: Migrate packing to pass context object.
This commit is contained in:
parent
a027d6b946
commit
cb2b5b47b0
@ -66,17 +66,37 @@ public:
|
|||||||
, curr_(std::begin(buff_))
|
, curr_(std::begin(buff_))
|
||||||
, end_(std::end(buff_)) {}
|
, end_(std::end(buff_)) {}
|
||||||
|
|
||||||
|
struct context {
|
||||||
|
decltype(std::begin(BufferType{})) out;
|
||||||
|
decltype(std::begin(BufferType{})) end;
|
||||||
|
|
||||||
|
constexpr auto remaining() noexcept {
|
||||||
|
return std::ranges::distance(out, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <packable_type T>
|
||||||
|
constexpr tl::expected<tl::monostate, error> pack(T&& v) noexcept {
|
||||||
|
using diff_type =
|
||||||
|
std::iterator_traits<decltype(curr_)>::difference_type;
|
||||||
|
auto rem = remaining();
|
||||||
|
decltype(rem) needed = builtin_packer<T>::total_size(v);
|
||||||
|
if (needed > rem) {
|
||||||
|
return tl::make_unexpected(error::out_of_space);
|
||||||
|
} else {
|
||||||
|
builtin_packer<T>::pack(v, *this);
|
||||||
|
return tl::monostate{};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <packable_type T>
|
template <packable_type T>
|
||||||
constexpr tl::expected<tl::monostate, error> pack(T&& v) noexcept {
|
constexpr tl::expected<tl::monostate, error> pack(T&& v) noexcept {
|
||||||
using diff_type =
|
context ctx{curr_, end_};
|
||||||
std::iterator_traits<decltype(curr_)>::difference_type;
|
// If packing is successful, it updates iterators.
|
||||||
diff_type const space_needed = builtin_packer<T>::total_size(v);
|
return ctx.pack(std::forward<T>(v)).map([&]{
|
||||||
if (space_needed > std::distance(curr_, end_)) {
|
curr_ = ctx.out;
|
||||||
return tl::make_unexpected(error::out_of_space);
|
end_ = ctx.end;
|
||||||
} else {
|
});
|
||||||
curr_ = builtin_packer<T>::pack(v, curr_);
|
|
||||||
return tl::monostate{};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr auto tell() const noexcept {
|
constexpr auto tell() const noexcept {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user