Skip to content

Commit

Permalink
bytes_ostream: add output_iterator
Browse files Browse the repository at this point in the history
To allow it being used for serialization code, which works in terms of
output iterators.
  • Loading branch information
denesb committed Dec 2, 2019
1 parent c5a9521 commit 07007ed
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bytes_ostream.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ public:
return _current != other._current;
}
};
class output_iterator {
public:
using iterator_category = std::output_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = bytes_ostream::value_type;
using pointer = bytes_ostream::value_type*;
using reference = bytes_ostream::value_type&;

friend class bytes_ostream;

private:
bytes_ostream* _ostream = nullptr;

private:
explicit output_iterator(bytes_ostream& os) : _ostream(&os) { }

public:
reference operator*() const { return *_ostream->write_place_holder(1); }
output_iterator& operator++() { return *this; }
output_iterator operator++(int) { return *this; }
};
private:
inline size_type current_space_left() const {
if (!_current) {
Expand Down Expand Up @@ -326,6 +347,8 @@ public:
fragment_iterator begin() const { return { _begin.get() }; }
fragment_iterator end() const { return { nullptr }; }

output_iterator write_begin() { return output_iterator(*this); }

boost::iterator_range<fragment_iterator> fragments() const {
return { begin(), end() };
}
Expand Down

0 comments on commit 07007ed

Please sign in to comment.