Add an experimental writer API

This commit is contained in:
Victor Zverovich
2024-08-17 09:54:09 -07:00
parent 020af729dd
commit 94b8bc8eae
4 changed files with 87 additions and 14 deletions
+23 -14
View File
@@ -1069,6 +1069,24 @@ template <typename T> class iterator_buffer<T*, T> : public buffer<T> {
auto out() -> T* { return &*this->end(); }
};
template <typename Container>
class container_buffer : public buffer<typename Container::value_type> {
private:
using value_type = typename Container::value_type;
static FMT_CONSTEXPR void grow(buffer<value_type>& buf, size_t capacity) {
auto& self = static_cast<container_buffer&>(buf);
self.container.resize(capacity);
self.set(&self.container[0], capacity);
}
public:
Container& container;
explicit container_buffer(Container& c)
: buffer<value_type>(grow, c.size()), container(c) {}
};
// A buffer that writes to a container with the contiguous storage.
template <typename OutputIt>
class iterator_buffer<
@@ -1076,25 +1094,16 @@ class iterator_buffer<
enable_if_t<detail::is_back_insert_iterator<OutputIt>::value &&
is_contiguous<typename OutputIt::container_type>::value,
typename OutputIt::container_type::value_type>>
: public buffer<typename OutputIt::container_type::value_type> {
: public container_buffer<typename OutputIt::container_type> {
private:
using container_type = typename OutputIt::container_type;
using value_type = typename container_type::value_type;
container_type& container_;
static FMT_CONSTEXPR void grow(buffer<value_type>& buf, size_t capacity) {
auto& self = static_cast<iterator_buffer&>(buf);
self.container_.resize(capacity);
self.set(&self.container_[0], capacity);
}
using base = container_buffer<typename OutputIt::container_type>;
public:
explicit iterator_buffer(container_type& c)
: buffer<value_type>(grow, c.size()), container_(c) {}
explicit iterator_buffer(typename OutputIt::container_type& c) : base(c) {}
explicit iterator_buffer(OutputIt out, size_t = 0)
: iterator_buffer(get_container(out)) {}
: base(get_container(out)) {}
auto out() -> OutputIt { return OutputIt(container_); }
auto out() -> OutputIt { return OutputIt(this->container); }
};
// A buffer that counts the number of code units written discarding the output.