From fd4fdffd46c3234fba6f805a960d08095a28a9fc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 27 Dec 2022 19:55:07 +0200 Subject: [PATCH] BAN::Formatter now takes putc like function as a parameter This allows us to use capturing lambdas as a putc like function --- BAN/include/BAN/Errors.h | 8 +-- BAN/include/BAN/Formatter.h | 114 ++++++++++++++++----------------- BAN/include/BAN/String.h | 5 +- BAN/include/BAN/StringView.h | 5 +- BAN/include/BAN/Time.h | 5 +- kernel/include/kernel/Serial.h | 12 ++-- kernel/include/kernel/kprint.h | 4 +- kernel/kernel/Shell.cpp | 4 +- 8 files changed, 80 insertions(+), 77 deletions(-) diff --git a/BAN/include/BAN/Errors.h b/BAN/include/BAN/Errors.h index bc89e3bc..c5a9bb8a 100644 --- a/BAN/include/BAN/Errors.h +++ b/BAN/include/BAN/Errors.h @@ -71,12 +71,12 @@ private: namespace BAN::Formatter { - template - void print_argument_impl(const Error& error, const ValueFormat&) + template + void print_argument_impl(F putc, const Error& error, const ValueFormat&) { if (error.GetErrorCode() == 0xFF) - print(error.GetMessage()); + print(putc, error.GetMessage()); else - print("{} ({})", error.GetMessage(), error.GetErrorCode()); + print(putc, "{} ({})", error.GetMessage(), error.GetErrorCode()); } } \ No newline at end of file diff --git a/BAN/include/BAN/Formatter.h b/BAN/include/BAN/Formatter.h index 4d17891a..bab8314f 100644 --- a/BAN/include/BAN/Formatter.h +++ b/BAN/include/BAN/Formatter.h @@ -8,20 +8,20 @@ namespace BAN::Formatter struct ValueFormat; - template - static void print(const char* format); + template + static void print(F putc, const char* format); - template - static void print(const char* format, const Arg& arg, const Args&... args); + template + static void print(F putc, const char* format, const Arg& arg, const Args&... args); - template - static void println(const char* format = "", const Args&... args); + template + static void println(F putc, const char* format = "", const Args&... args); - template - static size_t print_argument(const char* format, const T& arg); + template + static size_t print_argument(F putc, const char* format, const T& arg); - template - static void print_argument_impl(T value, const ValueFormat& format); + template + static void print_argument_impl(F putc, T value, const ValueFormat& format); /* @@ -38,43 +38,43 @@ namespace BAN::Formatter bool upper = false; }; - template - void print(const char* format) + template + void print(F putc, const char* format) { while (*format) { - PUTC_LIKE(*format); - format++; + putc(*format); + format++; } } - template - void print(const char* format, const Arg& arg, const Args&... args) + template + void print(F putc, const char* format, const Arg& arg, const Args&... args) { while (*format && *format != '{') { - PUTC_LIKE(*format); + putc(*format); format++; } if (*format == '{') { - size_t arg_len = print_argument(format, arg); + size_t arg_len = print_argument(putc, format, arg); if (arg_len == size_t(-1)) - return print(format); - print(format + arg_len, args...); + return print(putc, format); + print(putc, format + arg_len, args...); } } - template - void println(const char* format, const Args&... args) + template + void println(F putc, const char* format, const Args&... args) { - print(format, args...); - PUTC_LIKE('\n'); + print(putc, format, args...); + putc('\n'); } - template - size_t print_argument(const char* format, const Arg& argument) + template + size_t print_argument(F putc, const char* format, const Arg& argument) { ValueFormat value_format; @@ -131,7 +131,7 @@ namespace BAN::Formatter if (format[i] != '}') return size_t(-1); - print_argument_impl(argument, value_format); + print_argument_impl(putc, argument, value_format); return i + 1; } @@ -149,13 +149,13 @@ namespace BAN::Formatter return '?'; } - template - void print_integer(T value, const ValueFormat& format) + template + void print_integer(F putc, T value, const ValueFormat& format) { if (value == 0) { for (int i = 0; i < format.fill || i < 1; i++) - PUTC_LIKE('0'); + putc('0'); return; } @@ -186,21 +186,21 @@ namespace BAN::Formatter if (sign) *(--ptr) = '-'; - print(ptr); + print(putc, ptr); } - template - void print_floating(T value, const ValueFormat& format) + template + void print_floating(F putc, 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(int_part, format); + print_integer(putc, int_part, format); if (format.percision > 0) - PUTC_LIKE('.'); + putc('.'); for (int i = 0; i < format.percision; i++) { @@ -208,17 +208,17 @@ namespace BAN::Formatter 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)); + putc(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper)); } } - template - void print_pointer(void* ptr, const ValueFormat& format) + template + void print_pointer(F putc, void* ptr, const ValueFormat& format) { uintptr_t value = (uintptr_t)ptr; - print("0x"); + print(putc, "0x"); for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4) - PUTC_LIKE(value_to_base_char((value >> i) & 0xF, 16, format.upper)); + putc(value_to_base_char((value >> i) & 0xF, 16, format.upper)); } /* @@ -227,27 +227,27 @@ namespace BAN::Formatter */ - template void print_argument_impl(short value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(int value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(long value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(long long value, const ValueFormat& format) { print_integer(value, format); } + template void print_argument_impl(F putc, short value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, int value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, long value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, long long value, const ValueFormat& format) { print_integer(putc, value, format); } - template void print_argument_impl(unsigned short value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(unsigned int value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(unsigned long value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(unsigned long long value, const ValueFormat& format) { print_integer(value, format); } + template void print_argument_impl(F putc, unsigned short value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, unsigned int value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, unsigned long value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, unsigned long long value, const ValueFormat& format) { print_integer(putc, value, format); } - template void print_argument_impl(float value, const ValueFormat& format) { print_floating(value, format); } - template void print_argument_impl(double value, const ValueFormat& format) { print_floating(value, format); } - template void print_argument_impl(long double value, const ValueFormat& format) { print_floating(value, format); } + template void print_argument_impl(F putc, float value, const ValueFormat& format) { print_floating(putc, value, format); } + template void print_argument_impl(F putc, double value, const ValueFormat& format) { print_floating(putc, value, format); } + template void print_argument_impl(F putc, long double value, const ValueFormat& format) { print_floating(putc, value, format); } - template void print_argument_impl(char value, const ValueFormat&) { PUTC_LIKE(value); } - template void print_argument_impl(signed char value, const ValueFormat& format) { print_integer(value, format); } - template void print_argument_impl(unsigned char value, const ValueFormat& format) { print_integer(value, format); } + template void print_argument_impl(F putc, char value, const ValueFormat&) { putc(value); } + template void print_argument_impl(F putc, signed char value, const ValueFormat& format) { print_integer(putc, value, format); } + template void print_argument_impl(F putc, unsigned char value, const ValueFormat& format) { print_integer(putc, value, format); } - template void print_argument_impl(bool value, const ValueFormat& format) { print(value ? "true" : "false"); } + template void print_argument_impl(F putc, bool value, const ValueFormat& format) { print(putc, value ? "true" : "false"); } - template void print_argument_impl(T* value, const ValueFormat& format) { print_pointer((void*)value, format); } - template void print_argument_impl(const char* value, const ValueFormat&) { print(value);} + template void print_argument_impl(F putc, T* value, const ValueFormat& format) { print_pointer(putc, (void*)value, format); } + template void print_argument_impl(F putc, const char* value, const ValueFormat&) { print(putc, value);} } diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 371b10f4..797da03a 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -68,10 +68,11 @@ namespace BAN namespace BAN::Formatter { - template void print_argument_impl(const String& string, const ValueFormat&) + template + void print_argument_impl(F putc, const String& string, const ValueFormat&) { for (String::size_type i = 0; i < string.Size(); i++) - PUTC_LIKE(string[i]); + putc(string[i]); } } diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index f9b2ff82..8904de5f 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -44,10 +44,11 @@ namespace BAN namespace BAN::Formatter { - template void print_argument_impl(const StringView& sv, const ValueFormat&) + template + void print_argument_impl(F putc, const StringView& sv, const ValueFormat&) { for (StringView::size_type i = 0; i < sv.Size(); i++) - PUTC_LIKE(sv[i]); + putc(sv[i]); } } diff --git a/BAN/include/BAN/Time.h b/BAN/include/BAN/Time.h index e929d69b..449870f2 100644 --- a/BAN/include/BAN/Time.h +++ b/BAN/include/BAN/Time.h @@ -23,11 +23,12 @@ namespace BAN namespace BAN::Formatter { - template void print_argument_impl(const Time& time, const ValueFormat&) + template + void print_argument_impl(F putc, const Time& time, const ValueFormat&) { constexpr const char* week_days[] { "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; constexpr const char* months[] { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - print("{} {} {} {2}:{2}:{2} GMT+0 {4}", week_days[time.week_day], months[time.month], time.day, time.hour, time.minute, time.second, time.year); + print(putc, "{} {} {} {2}:{2}:{2} GMT+0 {4}", week_days[time.week_day], months[time.month], time.day, time.hour, time.minute, time.second, time.year); } } \ No newline at end of file diff --git a/kernel/include/kernel/Serial.h b/kernel/include/kernel/Serial.h index ef7239f7..72bdff2b 100644 --- a/kernel/include/kernel/Serial.h +++ b/kernel/include/kernel/Serial.h @@ -5,22 +5,22 @@ #define dprintln(...) \ do { \ - BAN::Formatter::print("[{5.3}] {}({}): ", (float)PIT::ms_since_boot() / 1000.0f, __FILE__, __LINE__); \ - BAN::Formatter::println(__VA_ARGS__); \ + BAN::Formatter::print(Serial::serial_putc, "[{5.3}] {}({}): ", (float)PIT::ms_since_boot() / 1000.0f, __FILE__, __LINE__); \ + BAN::Formatter::println(Serial::serial_putc, __VA_ARGS__); \ } while(false) #define dwarnln(...) \ do { \ - BAN::Formatter::print("\e[33m"); \ + BAN::Formatter::print(Serial::serial_putc, "\e[33m"); \ dprintln(__VA_ARGS__); \ - BAN::Formatter::print("\e[m"); \ + BAN::Formatter::print(Serial::serial_putc, "\e[m"); \ } while(false) #define derrorln(...) \ do { \ - BAN::Formatter::print("\e[31m"); \ + BAN::Formatter::print(Serial::serial_putc, "\e[31m"); \ dprintln(__VA_ARGS__); \ - BAN::Formatter::print("\e[m"); \ + BAN::Formatter::print(Serial::serial_putc, "\e[m"); \ } while(false) namespace Serial diff --git a/kernel/include/kernel/kprint.h b/kernel/include/kernel/kprint.h index a44c020e..c6727036 100644 --- a/kernel/include/kernel/kprint.h +++ b/kernel/include/kernel/kprint.h @@ -3,5 +3,5 @@ #include #include -#define kprint(...) BAN::Formatter::print(__VA_ARGS__) -#define kprintln(...) BAN::Formatter::println(__VA_ARGS__) +#define kprint(...) BAN::Formatter::print(TTY::PutCharCurrent, __VA_ARGS__) +#define kprintln(...) BAN::Formatter::println(TTY::PutCharCurrent, __VA_ARGS__) diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 2f5ec3e8..c3c1c266 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -72,7 +72,7 @@ namespace Kernel for (size_t i = 2; i < arguments.Size(); i++) kprint(" {}", arguments[i]); } - kprintln(); + kprintln(""); return; } @@ -125,7 +125,7 @@ namespace Kernel if (edx & ((uint32_t)1 << i)) kprint("{}{}", first ? (first = false, "") : ", ", CPUID::FeatStringEDX((uint32_t)1 << i)); if (!first) - kprintln(); + kprintln(""); return; }