LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
ArenaWriter.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Verizon Media 2020
6
7#include "swoc/ArenaWriter.h"
8
9namespace swoc { inline namespace SWOC_VERSION_NS {
10
13 if (_attempted >= _capacity) {
14 this->realloc(_attempted + 1);
15 }
16 this->super_type::write(c);
17 return *this;
18}
19
21ArenaWriter::write(void const *data, size_t n) {
22 if (n + _attempted > _capacity) {
23 this->realloc(n + _attempted);
24 }
25 this->super_type::write(data, n);
26 return *this;
27}
28
29bool
31 if (_attempted + n > _capacity) {
32 this->realloc(_attempted + n);
33 return false;
34 }
35 return this->super_type::commit(n);
36}
37
38void
40 auto text = this->view(); // Current data.
41 auto span = _arena.require(n).remnant().rebind<char>();
42 const_cast<char *&>(_buffer) = span.data();
43 _capacity = span.size();
44 memcpy(_buffer, text.data(), text.size());
45}
46
47}} // namespace swoc::SWOC_VERSION_NS
MemArena & _arena
Arena for the buffer.
Definition ArenaWriter.h:52
bool commit(size_t n) override
ArenaWriter & write(void const *data, size_t n) override
void realloc(size_t n)
swoc::TextView view() const
size_t _capacity
Size of output buffer.
const char * data() const override
Return the output buffer.
FixedBufferWriter & write(char c) override
Write a single character c to the buffer.
size_t _attempted
Number of characters written, including those discarded due error condition.
bool commit(size_t n) override
Advance the used part of the output buffer.
char *const _buffer
Output buffer.
For template deduction guides.
Definition ArenaWriter.cc:9
void * memcpy(void *dst, const std::string_view &src)