ladybird/Kernel/KBufferBuilder.h
Andreas Kling f85b94e6d4 Kernel: Remove KBufferBuilder's can_expand restriction
KBufferBuilder is always allowed to expand if it wants to. This
restriction was added a long time ago when it was unsafe to allocate
VM while generating ProcFS contents.
2021-07-20 18:05:05 +02:00

55 lines
1.2 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <Kernel/KBuffer.h>
#include <stdarg.h>
namespace Kernel {
class KBufferBuilder {
public:
using OutputType = KBuffer;
KBufferBuilder();
KBufferBuilder(KBufferBuilder&&) = default;
~KBufferBuilder() = default;
void append(const StringView&);
void append(char);
void append(const char*, int);
void append_escaped_for_json(const StringView&);
void append_bytes(ReadonlyBytes);
template<typename... Parameters>
void appendff(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&... parameters)
{
// FIXME: This really not ideal, but vformat expects StringBuilder.
StringBuilder builder;
vformat(builder, fmtstr.view(), AK::VariadicFormatParams { parameters... });
append_bytes(builder.string_view().bytes());
}
bool flush();
OwnPtr<KBuffer> build();
private:
bool check_expand(size_t);
u8* insertion_ptr()
{
if (!m_buffer)
return nullptr;
return m_buffer->data() + m_size;
}
RefPtr<KBufferImpl> m_buffer;
size_t m_size { 0 };
};
}