BAN: Move Formatter to new project called BAN
I also implemented basic ErrorOr and Queue
This commit is contained in:
78
BAN/include/BAN/Errors.h
Normal file
78
BAN/include/BAN/Errors.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Formatter.h>
|
||||
|
||||
#if defined(__is_bank)
|
||||
#include <kernel/panic.h>
|
||||
#define MUST(error) { decltype(error) e = error; if (e.HasError()) { Kernel::panic("{}", e.GetError()); } }
|
||||
#else
|
||||
#error "NOT IMPLEMENTED"
|
||||
#endif
|
||||
|
||||
#define TRY(error) { decltype(error) e = error; if (e.HasError()) return e; }
|
||||
|
||||
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
static Error FromString(const char* message)
|
||||
{
|
||||
Error result;
|
||||
strncpy(result.m_message, message, sizeof(m_message));
|
||||
result.m_message[sizeof(result.m_message) - 1] = '\0';
|
||||
result.m_error_code = 0xFF;
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t GetErrorCode() const { return m_error_code; }
|
||||
const char* GetMessage() const { return m_message; }
|
||||
|
||||
private:
|
||||
char m_message[128];
|
||||
uint8_t m_error_code;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ErrorOr
|
||||
{
|
||||
public:
|
||||
ErrorOr(const T& value) : m_has_error(false) { m_data = (void*)new T(value); }
|
||||
ErrorOr(const Error& error) : m_has_error(true) { m_data = (void*)new Error(error); }
|
||||
~ErrorOr() { IsError() ? (delete reinterpret_cast<Error*>(m_data)) : (delete reinterpret_cast<T*>(m_data)); }
|
||||
|
||||
bool IsError() const { return m_has_error; }
|
||||
const Error& GetError() const { return *reinterpret_cast<Error*>(m_data); }
|
||||
T& Value() { return *reinterpret_cast<T*>(m_data); }
|
||||
|
||||
private:
|
||||
bool m_has_error;
|
||||
void* m_data;
|
||||
};
|
||||
|
||||
template<>
|
||||
class ErrorOr<void>
|
||||
{
|
||||
public:
|
||||
ErrorOr() : m_error(nullptr) { }
|
||||
ErrorOr(const Error& error) { m_error = new Error(error); }
|
||||
~ErrorOr() { delete m_error; }
|
||||
|
||||
bool IsError() const { return m_error; }
|
||||
const Error& GetError() const { return *m_error; }
|
||||
|
||||
private:
|
||||
Error* m_error;
|
||||
};
|
||||
|
||||
|
||||
namespace BAN::Formatter
|
||||
{
|
||||
template<void(*PUTC_LIKE)(char)>
|
||||
void print_value(const Error& error, const ValueFormat& format)
|
||||
{
|
||||
if (error.GetErrorCode() == 0xFF)
|
||||
print<PUTC_LIKE>(error.GetMessage());
|
||||
else
|
||||
print<PUTC_LIKE>("{} ({})", error.GetMessage(), error.GetErrorCode());
|
||||
}
|
||||
}
|
||||
253
BAN/include/BAN/Formatter.h
Normal file
253
BAN/include/BAN/Formatter.h
Normal file
@@ -0,0 +1,253 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
namespace BAN::Formatter
|
||||
{
|
||||
|
||||
struct ValueFormat;
|
||||
|
||||
template<void(*PUTC_LIKE)(char)>
|
||||
static void print(const char* format);
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
|
||||
static void print(const char* format, Arg arg, Args... args);
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename... Args>
|
||||
static void println(const char* format = "", Args... args);
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename T>
|
||||
static size_t print_argument(const char* format, T arg);
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename T>
|
||||
static void print_value(T value, const ValueFormat& format);
|
||||
|
||||
|
||||
/*
|
||||
|
||||
IMPLEMENTATION
|
||||
|
||||
*/
|
||||
|
||||
struct ValueFormat
|
||||
{
|
||||
int base = 10;
|
||||
int percision = 3;
|
||||
int fill = 0;
|
||||
bool upper = false;
|
||||
};
|
||||
|
||||
template<void(*PUTC_LIKE)(char)>
|
||||
void print(const char* format)
|
||||
{
|
||||
while (*format)
|
||||
{
|
||||
PUTC_LIKE(*format);
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
|
||||
void print(const char* format, Arg arg, Args... args)
|
||||
{
|
||||
while (*format && *format != '{')
|
||||
{
|
||||
PUTC_LIKE(*format);
|
||||
format++;
|
||||
}
|
||||
|
||||
if (*format == '{')
|
||||
{
|
||||
size_t arg_len = print_argument<PUTC_LIKE>(format, arg);
|
||||
if (arg_len == size_t(-1))
|
||||
return print<PUTC_LIKE>(format);
|
||||
print<PUTC_LIKE>(format + arg_len, args...);
|
||||
}
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename... Args>
|
||||
void println(const char* format, Args... args)
|
||||
{
|
||||
print<PUTC_LIKE>(format, args...);
|
||||
PUTC_LIKE('\n');
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename Arg>
|
||||
size_t print_argument(const char* format, Arg argument)
|
||||
{
|
||||
ValueFormat value_format;
|
||||
|
||||
if (format[0] != '{')
|
||||
return size_t(-1);
|
||||
|
||||
size_t i = 1;
|
||||
do
|
||||
{
|
||||
if (!format[i] || format[i] == '}')
|
||||
break;
|
||||
|
||||
if ('0' <= format[i] && format[i] <= '9')
|
||||
{
|
||||
int fill = 0;
|
||||
while ('0' <= format[i] && format[i] <= '9')
|
||||
{
|
||||
fill = (fill * 10) + (format[i] - '0');
|
||||
i++;
|
||||
}
|
||||
value_format.fill = fill;
|
||||
}
|
||||
|
||||
switch (format[i])
|
||||
{
|
||||
case 'b': value_format.base = 2; value_format.upper = false; i++; break;
|
||||
case 'B': value_format.base = 2; value_format.upper = true; i++; break;
|
||||
case 'o': value_format.base = 8; value_format.upper = false; i++; break;
|
||||
case 'O': value_format.base = 8; value_format.upper = true; i++; break;
|
||||
case 'd': value_format.base = 10; value_format.upper = false; i++; break;
|
||||
case 'D': value_format.base = 10; value_format.upper = true; i++; break;
|
||||
case 'h': value_format.base = 16; value_format.upper = false; i++; break;
|
||||
case 'H': value_format.base = 16; value_format.upper = true; i++; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (!format[i] || format[i] == '}')
|
||||
break;
|
||||
|
||||
if (format[i] == '.')
|
||||
{
|
||||
i++;
|
||||
int percision = 0;
|
||||
while ('0' <= format[i] && format[i] <= '9')
|
||||
{
|
||||
percision = (percision * 10) + (format[i] - '0');
|
||||
i++;
|
||||
}
|
||||
value_format.percision = percision;
|
||||
}
|
||||
|
||||
} while(false);
|
||||
|
||||
if (format[i] != '}')
|
||||
return size_t(-1);
|
||||
|
||||
print_value<PUTC_LIKE>(argument, value_format);
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
static char value_to_base_char(uint8_t value, int base, bool upper)
|
||||
{
|
||||
if (base <= 10)
|
||||
return value + '0';
|
||||
if (base <= 36)
|
||||
{
|
||||
if (value < 10)
|
||||
return value + '0';
|
||||
return value + (upper ? 'A' : 'a') - 10;
|
||||
}
|
||||
return '?';
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename T>
|
||||
void print_integer(T value, const ValueFormat& format)
|
||||
{
|
||||
if (value == 0)
|
||||
{
|
||||
for (int i = 0; i < format.fill || i < 1; i++)
|
||||
PUTC_LIKE('0');
|
||||
return;
|
||||
}
|
||||
|
||||
bool sign = false;
|
||||
|
||||
// Fits signed 64-bit binary number and null
|
||||
char buffer[66];
|
||||
char* ptr = buffer + sizeof(buffer);
|
||||
*(--ptr) = '\0';
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
sign = true;
|
||||
T digit = (format.base - (value % format.base)) % format.base;
|
||||
*(--ptr) = value_to_base_char(digit, format.base, format.upper);
|
||||
value = -(value / format.base);
|
||||
}
|
||||
|
||||
while (value)
|
||||
{
|
||||
*(--ptr) = value_to_base_char(value % format.base, format.base, format.upper);
|
||||
value /= format.base;
|
||||
}
|
||||
|
||||
while (ptr >= buffer + sizeof(buffer) - format.fill)
|
||||
*(--ptr) = '0';
|
||||
|
||||
if (sign)
|
||||
*(--ptr) = '-';
|
||||
|
||||
print<PUTC_LIKE>(ptr);
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename T>
|
||||
void print_floating(T value, const ValueFormat& format)
|
||||
{
|
||||
int64_t int_part = (int64_t)value;
|
||||
T frac_part = value - (T)int_part;
|
||||
if (frac_part < 0)
|
||||
frac_part = -frac_part;
|
||||
|
||||
print_integer<PUTC_LIKE>(int_part, format);
|
||||
|
||||
if (format.percision > 0)
|
||||
PUTC_LIKE('.');
|
||||
|
||||
for (int i = 0; i < format.percision; i++)
|
||||
{
|
||||
frac_part *= format.base;
|
||||
if (i == format.percision - 1)
|
||||
frac_part += 0.5;
|
||||
|
||||
PUTC_LIKE(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper));
|
||||
}
|
||||
}
|
||||
|
||||
template<void(*PUTC_LIKE)(char)>
|
||||
void print_pointer(void* ptr, const ValueFormat& format)
|
||||
{
|
||||
uintptr_t value = (uintptr_t)ptr;
|
||||
print<PUTC_LIKE>("0x");
|
||||
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
|
||||
PUTC_LIKE(value_to_base_char((value >> i) & 0xF, 16, format.upper));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
TEMPLATE SPECIALIZATIONS
|
||||
|
||||
*/
|
||||
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(unsigned short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(unsigned int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(unsigned long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(unsigned long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(float value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(long double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
||||
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(char value, const ValueFormat&) { PUTC_LIKE(value); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(signed char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(unsigned char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
||||
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(bool value, const ValueFormat& format) { print<PUTC_LIKE>(value ? "true" : "false"); }
|
||||
|
||||
template<void(*PUTC_LIKE)(char), typename T> void print_value(T* value, const ValueFormat& format) { print_pointer<PUTC_LIKE>((void*)value, format); }
|
||||
template<void(*PUTC_LIKE)(char)> void print_value(const char* value, const ValueFormat&) { print<PUTC_LIKE>(value);}
|
||||
|
||||
}
|
||||
125
BAN/include/BAN/Queue.h
Normal file
125
BAN/include/BAN/Queue.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Errors.h>
|
||||
#include <kernel/kmalloc.h>
|
||||
|
||||
#if defined(__is_bank)
|
||||
#include <kernel/kmalloc.h>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
namespace BAN
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Queue
|
||||
{
|
||||
private:
|
||||
#if defined(__is_bank)
|
||||
using allocator = kmalloc;
|
||||
using deallocator = kfree;
|
||||
#else
|
||||
using allocator = malloc;
|
||||
using deallocator = free;
|
||||
#endif
|
||||
|
||||
|
||||
public:
|
||||
using size_type = uint32_t;
|
||||
using value_type = T;
|
||||
|
||||
public:
|
||||
Queue() = default;
|
||||
~Queue();
|
||||
|
||||
ErrorOr<void> Push(const T& value);
|
||||
void Pop();
|
||||
|
||||
bool Empty() const;
|
||||
size_type Size() const;
|
||||
|
||||
const T& Front() const;
|
||||
T& Front();
|
||||
|
||||
private:
|
||||
ErrorOr<void> VerifyCapacity(size_type size);
|
||||
|
||||
private:
|
||||
T* m_data = nullptr;
|
||||
size_type m_capacity = 0;
|
||||
size_type m_size = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Queue<T>::~Queue()
|
||||
{
|
||||
deallocator(m_data);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ErrorOr<void> Queue<T>::Push(const T& value)
|
||||
{
|
||||
VerifyCapacity(m_size + 1);
|
||||
m_data[m_size++] = value;
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Queue<T>::Pop()
|
||||
{
|
||||
assert(m_size > 0);
|
||||
m_data->~T();
|
||||
memmove(m_data, m_data + 1, sizeof(T) * (--m_size));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Queue<T>::Empty() const
|
||||
{
|
||||
return m_size == 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename Queue<T>::size_type Queue<T>::Size() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const T& Queue<T>::Front() const
|
||||
{
|
||||
assert(m_size > 0);
|
||||
return *m_data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& Queue<T>::Front()
|
||||
{
|
||||
assert(m_size > 0);
|
||||
return *m_data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ErrorOr<void> Queue<T>::VerifyCapacity(size_type size)
|
||||
{
|
||||
if (m_capacity > size)
|
||||
return {};
|
||||
|
||||
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1) * sizeof(T);
|
||||
void* new_data = allocator(new_cap);
|
||||
if (new_data == nullptr)
|
||||
return Error { .message = "Queue: out of memory", .error_code = ErrorCode::OutOfMemory };
|
||||
|
||||
memcpy(new_data, m_data, m_size * sizeof(T));
|
||||
deallocator(m_data);
|
||||
|
||||
m_data = (T*)new_data;
|
||||
m_capacity = new_cap;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user