Add formatting & factories for status
This commit is contained in:
1
.bazelrc
1
.bazelrc
@ -20,6 +20,7 @@ build --cxxopt=-Wno-extra-semi-stmt
|
||||
build --cxxopt=-Wno-extra-semi
|
||||
build --cxxopt=-Wno-global-constructors
|
||||
build --cxxopt=-Wno-unsafe-buffer-usage
|
||||
build --cxxopt=-Wno-covered-switch-default
|
||||
|
||||
build:windows_san --config=windows
|
||||
build:windows_san --copt=-fno-sanitize-ignorelist
|
||||
|
@ -55,6 +55,8 @@ public:
|
||||
{
|
||||
m_writer->write(as_bytes(s.as_span()));
|
||||
}
|
||||
|
||||
constexpr Writer* writer() const { return m_writer; }
|
||||
};
|
||||
|
||||
template<formattable... Args>
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "asl/allocator.hpp"
|
||||
#include "asl/string.hpp"
|
||||
#include "asl/atomic.hpp"
|
||||
#include "asl/format.hpp"
|
||||
|
||||
// @Todo Use custom allocator
|
||||
using Allocator = asl::DefaultAllocator;
|
||||
@ -10,16 +11,56 @@ static Allocator g_allocator{};
|
||||
namespace
|
||||
{
|
||||
|
||||
// @Todo Make a proper string builder, replace the tests's StringSink too
|
||||
class StringSink : public asl::Writer
|
||||
{
|
||||
isize_t m_current_len{};
|
||||
char* m_data{};
|
||||
|
||||
public:
|
||||
~StringSink() override
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
void write(asl::span<const asl::byte> str) override
|
||||
{
|
||||
m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
|
||||
m_data,
|
||||
asl::layout::array<char>(m_current_len),
|
||||
asl::layout::array<char>(m_current_len + str.size())));
|
||||
|
||||
asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT
|
||||
|
||||
m_current_len += str.size();
|
||||
}
|
||||
|
||||
constexpr asl::string_view str() const { return {m_data, m_current_len}; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_data != nullptr)
|
||||
{
|
||||
m_current_len = 0;
|
||||
asl::GlobalHeap::dealloc(m_data, asl::layout::array<char>(m_current_len));
|
||||
m_data = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct StatusInternal
|
||||
{
|
||||
asl::string<Allocator> msg;
|
||||
asl::status_code code;
|
||||
asl::atomic<int32_t> ref_count;
|
||||
|
||||
// @Todo Once we have string builder, move the string instead
|
||||
|
||||
constexpr StatusInternal(asl::string_view msg_, asl::status_code code_)
|
||||
: msg{msg_, g_allocator}
|
||||
, code{code_}
|
||||
{
|
||||
ASL_ASSERT(code != asl::status_code::ok);
|
||||
atomic_store(&ref_count, 1);
|
||||
}
|
||||
};
|
||||
@ -30,6 +71,13 @@ asl::status::status(status_code code, string_view msg)
|
||||
: m_payload{alloc_new<StatusInternal>(g_allocator, msg, code)}
|
||||
{}
|
||||
|
||||
asl::status::status(status_code code, string_view fmt, span<format_internals::type_erased_arg> args)
|
||||
{
|
||||
StringSink sink;
|
||||
format_internals::format(&sink, fmt, args);
|
||||
m_payload = alloc_new<StatusInternal>(g_allocator, sink.str(), code);
|
||||
}
|
||||
|
||||
asl::status_code asl::status::code_internal() const
|
||||
{
|
||||
ASL_ASSERT(!is_inline());
|
||||
@ -60,3 +108,27 @@ void asl::status::unref()
|
||||
}
|
||||
}
|
||||
|
||||
void asl::AslFormat(asl::Formatter& f, const asl::status& s)
|
||||
{
|
||||
string_view status_str{};
|
||||
|
||||
switch (s.code())
|
||||
{
|
||||
case status_code::ok: status_str = "ok"_sv; break;
|
||||
case status_code::unknown: status_str = "unknown"_sv; break;
|
||||
case status_code::internal: status_str = "internal"_sv; break;
|
||||
case status_code::runtime: status_str = "runtime"_sv; break;
|
||||
case status_code::invalid_argument: status_str = "invalid_argument"_sv; break;
|
||||
default: status_str = "<unknown>"_sv; break;
|
||||
}
|
||||
|
||||
if (s.is_inline())
|
||||
{
|
||||
format(f.writer(), "[{}]", status_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
format(f.writer(), "[{}: {}]", status_str, s.message_internal());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,11 +2,12 @@
|
||||
|
||||
#include "asl/integers.hpp"
|
||||
#include "asl/string_view.hpp"
|
||||
#include "asl/format.hpp"
|
||||
|
||||
namespace asl
|
||||
{
|
||||
|
||||
// @Todo Make status with formatting
|
||||
class Formatter;
|
||||
|
||||
enum class status_code : uint8_t
|
||||
{
|
||||
@ -55,8 +56,6 @@ class status
|
||||
void unref();
|
||||
|
||||
public:
|
||||
constexpr status() = default;
|
||||
|
||||
constexpr ~status()
|
||||
{
|
||||
if (!is_inline()) { unref(); }
|
||||
@ -67,6 +66,7 @@ public:
|
||||
{}
|
||||
|
||||
status(status_code code, string_view msg);
|
||||
status(status_code code, string_view fmt, span<format_internals::type_erased_arg> args);
|
||||
|
||||
constexpr status(const status& other)
|
||||
: m_payload{other.m_payload}
|
||||
@ -100,7 +100,7 @@ public:
|
||||
|
||||
constexpr bool ok() const
|
||||
{
|
||||
return m_payload == nullptr || code() == status_code::ok;
|
||||
return m_payload == nullptr;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(*-explicit-conversions)
|
||||
@ -119,6 +119,27 @@ public:
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
friend void AslFormat(Formatter& f, const status&);
|
||||
};
|
||||
|
||||
static constexpr status ok() { return status{status_code::ok}; }
|
||||
|
||||
#define ASL_DEFINE_ERROR_(type) \
|
||||
static constexpr status type##_error() { return status{status_code::type}; } \
|
||||
static inline status type##_error(string_view sv) { return status{status_code::type, sv}; } \
|
||||
template<formattable... Args> \
|
||||
[[maybe_unused]] static status type##_error(string_view fmt, const Args&... args) \
|
||||
{ \
|
||||
format_internals::type_erased_arg type_erased_args[] = { \
|
||||
format_internals::type_erased_arg(args)... \
|
||||
}; \
|
||||
return status{status_code::type, fmt, type_erased_args}; \
|
||||
}
|
||||
|
||||
ASL_DEFINE_ERROR_(unknown)
|
||||
ASL_DEFINE_ERROR_(internal)
|
||||
ASL_DEFINE_ERROR_(runtime)
|
||||
ASL_DEFINE_ERROR_(invalid_argument)
|
||||
|
||||
} // namespace asl
|
||||
|
@ -1,47 +1,10 @@
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/testing/testing.hpp"
|
||||
#include "asl/allocator.hpp"
|
||||
#include "asl/float.hpp"
|
||||
#include "asl/tests/test_types.hpp"
|
||||
|
||||
static_assert(asl::formattable<decltype("Hello")>);
|
||||
|
||||
class StringSink : public asl::Writer
|
||||
{
|
||||
// @Todo Use string, once we have it, or a buffer
|
||||
isize_t m_current_len{};
|
||||
char* m_data{};
|
||||
|
||||
public:
|
||||
~StringSink() override
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
void write(asl::span<const asl::byte> str) override
|
||||
{
|
||||
m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
|
||||
m_data,
|
||||
asl::layout::array<char>(m_current_len),
|
||||
asl::layout::array<char>(m_current_len + str.size())));
|
||||
|
||||
asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT
|
||||
|
||||
m_current_len += str.size();
|
||||
}
|
||||
|
||||
constexpr asl::string_view str() const { return {m_data, m_current_len}; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_data != nullptr)
|
||||
{
|
||||
m_current_len = 0;
|
||||
asl::GlobalHeap::dealloc(m_data, asl::layout::array<char>(m_current_len));
|
||||
m_data = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ASL_TEST(format_args)
|
||||
{
|
||||
StringSink sink;
|
||||
@ -154,3 +117,26 @@ ASL_TEST(format_boolean)
|
||||
asl::format(&sink, "{} {}", true, false);
|
||||
ASL_TEST_EXPECT(sink.str() == "true false"_sv);
|
||||
}
|
||||
|
||||
struct CustomFormat
|
||||
{
|
||||
int x;
|
||||
friend void AslFormat(asl::Formatter&, const CustomFormat&);
|
||||
};
|
||||
|
||||
void AslFormat(asl::Formatter& f, const CustomFormat& c)
|
||||
{
|
||||
f.write("("_sv);
|
||||
AslFormat(f, c.x);
|
||||
f.write(")"_sv);
|
||||
}
|
||||
|
||||
static_assert(asl::formattable<CustomFormat>);
|
||||
|
||||
ASL_TEST(format_custom)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
asl::format(&sink, "{}", CustomFormat{37});
|
||||
ASL_TEST_EXPECT(sink.str() == "(37)"_sv);
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
#include "asl/status.hpp"
|
||||
#include "asl/testing/testing.hpp"
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/tests/test_types.hpp"
|
||||
|
||||
ASL_TEST(simple_ok)
|
||||
{
|
||||
asl::status s;
|
||||
asl::status s = asl::ok();
|
||||
ASL_TEST_ASSERT(s);
|
||||
ASL_TEST_ASSERT(s.ok());
|
||||
ASL_TEST_ASSERT(s.code() == asl::status_code::ok);
|
||||
@ -11,7 +13,7 @@ ASL_TEST(simple_ok)
|
||||
|
||||
ASL_TEST(simple_code)
|
||||
{
|
||||
asl::status s{asl::status_code::runtime};
|
||||
asl::status s = asl::runtime_error();
|
||||
ASL_TEST_ASSERT(!s);
|
||||
ASL_TEST_ASSERT(!s.ok());
|
||||
ASL_TEST_ASSERT(s.code() == asl::status_code::runtime);
|
||||
@ -20,7 +22,7 @@ ASL_TEST(simple_code)
|
||||
|
||||
ASL_TEST(with_message)
|
||||
{
|
||||
asl::status s{asl::status_code::internal, "We done goofed"};
|
||||
asl::status s = asl::internal_error("We done goofed");
|
||||
ASL_TEST_ASSERT(!s);
|
||||
ASL_TEST_ASSERT(!s.ok());
|
||||
ASL_TEST_ASSERT(s.code() == asl::status_code::internal);
|
||||
@ -29,8 +31,8 @@ ASL_TEST(with_message)
|
||||
|
||||
ASL_TEST(copy_inline)
|
||||
{
|
||||
asl::status s{asl::status_code::ok};
|
||||
asl::status s2{asl::status_code::internal};
|
||||
asl::status s = asl::ok();
|
||||
asl::status s2 = asl::internal_error();
|
||||
|
||||
asl::status s3 = s;
|
||||
ASL_TEST_ASSERT(s3.code() == asl::status_code::ok);
|
||||
@ -41,10 +43,10 @@ ASL_TEST(copy_inline)
|
||||
|
||||
ASL_TEST(copy_message)
|
||||
{
|
||||
asl::status s2;
|
||||
asl::status s2 = asl::ok();
|
||||
|
||||
{
|
||||
asl::status s{asl::status_code::internal, "Oh no!"};
|
||||
asl::status s = asl::internal_error("Oh no!");
|
||||
ASL_TEST_ASSERT(!s);
|
||||
ASL_TEST_ASSERT(!s.ok());
|
||||
ASL_TEST_ASSERT(s.code() == asl::status_code::internal);
|
||||
@ -68,3 +70,26 @@ ASL_TEST(copy_message)
|
||||
ASL_TEST_ASSERT(s2.code() == asl::status_code::internal);
|
||||
ASL_TEST_ASSERT(s2.message() == "Oh no!"_sv);
|
||||
}
|
||||
|
||||
static_assert(asl::formattable<asl::status>);
|
||||
|
||||
ASL_TEST(format)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
asl::format(&sink, "-{}-", asl::ok());
|
||||
ASL_TEST_EXPECT(sink.str() == "-[ok]-"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "-{}-", asl::internal_error("hello"));
|
||||
ASL_TEST_EXPECT(sink.str() == "-[internal: hello]-"_sv);
|
||||
}
|
||||
|
||||
ASL_TEST(make_with_format)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "-{}-", asl::internal_error("hello, {}, {}", 45, "world"));
|
||||
ASL_TEST_EXPECT(sink.str() == "-[internal: hello, 45, world]-"_sv);
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "asl/utility.hpp"
|
||||
#include "asl/io.hpp"
|
||||
#include "asl/allocator.hpp"
|
||||
#include "asl/string_view.hpp"
|
||||
|
||||
struct TrivialType
|
||||
{
|
||||
@ -88,3 +91,39 @@ struct DestructorObserver
|
||||
}
|
||||
};
|
||||
|
||||
class StringSink : public asl::Writer
|
||||
{
|
||||
// @Todo Use string, once we have it, or a buffer
|
||||
isize_t m_current_len{};
|
||||
char* m_data{};
|
||||
|
||||
public:
|
||||
~StringSink() override
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
void write(asl::span<const asl::byte> str) override
|
||||
{
|
||||
m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
|
||||
m_data,
|
||||
asl::layout::array<char>(m_current_len),
|
||||
asl::layout::array<char>(m_current_len + str.size())));
|
||||
|
||||
asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT
|
||||
|
||||
m_current_len += str.size();
|
||||
}
|
||||
|
||||
constexpr asl::string_view str() const { return {m_data, m_current_len}; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_data != nullptr)
|
||||
{
|
||||
m_current_len = 0;
|
||||
asl::GlobalHeap::dealloc(m_data, asl::layout::array<char>(m_current_len));
|
||||
m_data = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user