ladybird/Kernel/KBufferBuilder.h
MacDue 9a120d7243 AK: Add support for "debug only" formatters
These are formatters that can only be used with debug print
functions, such as dbgln(). Currently this is limited to
Formatter<ErrorOr<T>>. With this you can still debug log ErrorOr
values (good for debugging), but trying to use them in any
String::formatted() call will fail (which prevents .to_string()
errors with the new failable strings being ignored).

You make a formatter debug only by adding a constexpr method like:
static constexpr bool is_debug_only() { return true; }
2023-01-13 21:09:26 +00:00

75 lines
1.8 KiB
C++

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