char* _end;
char* _current;
bool _own_buf;
-
+ bool _alloc_failure = false;
private:
void free_buffer() noexcept;
- void realloc_buffer();
+ void realloc_buffer_and_append(char c) noexcept;
public:
// inserter_iterator is designed like std::back_insert_iterator:
public:
explicit inserter_iterator(log_buf& buf) noexcept : _buf(&buf) { }
- inserter_iterator& operator=(char c) {
+ inserter_iterator& operator=(char c) noexcept {
if (__builtin_expect(_buf->_current == _buf->_end, false)) {
- _buf->realloc_buffer();
+ _buf->realloc_buffer_and_append(c);
+ return *this;
}
*_buf->_current++ = c;
return *this;
}
- inserter_iterator& operator*() {
+ inserter_iterator& operator*() noexcept {
return *this;
}
inserter_iterator& operator++() noexcept {
/// allocated buffer. log_buf doesn't take ownership of the buffer.
log_buf(char* external_buf, size_t size) noexcept;
~log_buf();
+ /// Clear the buffer, setting its position back to the start, but does not
+ /// free any buffers (after this called, size is zero, capacity is unchanged).
+ /// Any existing iterators are invalidated.
+ void clear() { _current = _begin; }
/// Create an output iterator which allows writing into the buffer.
inserter_iterator back_insert_begin() noexcept { return inserter_iterator(*this); }
/// The amount of data written so far.
- const size_t size() const noexcept { return _current - _begin; }
+ size_t size() const noexcept { return _current - _begin; }
/// The size of the buffer.
- const size_t capacity() const noexcept { return _end - _begin; }
+ size_t capacity() const noexcept { return _end - _begin; }
/// Read only pointer to the buffer.
/// Note that the buffer is not guaranteed to be null terminated. The writer
/// has to ensure that, should it wish to.