BAN: Formatter now uses perfect forwarding on its arguments
This commit is contained in:
parent
3ac99f1bd8
commit
371dfe8ef3
|
@ -115,7 +115,7 @@ namespace BAN
|
|||
namespace BAN::Formatter
|
||||
{
|
||||
template<typename F>
|
||||
void print_argument_impl(F putc, const Error& error, const ValueFormat&)
|
||||
void print_argument(F putc, const Error& error, const ValueFormat&)
|
||||
{
|
||||
if (error.get_error_code() == 0xFF)
|
||||
print(putc, error.get_message());
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <BAN/Move.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
@ -12,16 +14,19 @@ namespace BAN::Formatter
|
|||
static void print(F putc, const char* format);
|
||||
|
||||
template<typename F, typename Arg, typename... Args>
|
||||
static void print(F putc, const char* format, const Arg& arg, const Args&... args);
|
||||
static void print(F putc, const char* format, Arg&& arg, Args&&... args);
|
||||
|
||||
template<typename F, typename... Args>
|
||||
static void println(F putc, const char* format = "", const Args&... args);
|
||||
static void println(F putc, const char* format, Args&&... args);
|
||||
|
||||
template<typename F, typename T>
|
||||
static size_t print_argument(F putc, const char* format, const T& arg);
|
||||
static void print_argument(F putc, T value, const ValueFormat& format);
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename F, typename T>
|
||||
static void print_argument_impl(F putc, T value, const ValueFormat& format);
|
||||
static size_t parse_format_and_print_argument(F putc, const char* format, T&& arg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
@ -49,7 +54,7 @@ namespace BAN::Formatter
|
|||
}
|
||||
|
||||
template<typename F, typename Arg, typename... Args>
|
||||
void print(F putc, const char* format, const Arg& arg, const Args&... args)
|
||||
void print(F putc, const char* format, Arg&& arg, Args&&... args)
|
||||
{
|
||||
while (*format && *format != '{')
|
||||
{
|
||||
|
@ -59,22 +64,25 @@ namespace BAN::Formatter
|
|||
|
||||
if (*format == '{')
|
||||
{
|
||||
size_t arg_len = print_argument(putc, format, arg);
|
||||
size_t arg_len = detail::parse_format_and_print_argument(putc, format, forward<Arg>(arg));
|
||||
if (arg_len == size_t(-1))
|
||||
return print(putc, format);
|
||||
print(putc, format + arg_len, args...);
|
||||
print(putc, format + arg_len, forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F, typename... Args>
|
||||
void println(F putc, const char* format, const Args&... args)
|
||||
void println(F putc, const char* format, Args&&... args)
|
||||
{
|
||||
print(putc, format, args...);
|
||||
putc('\n');
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<typename F, typename Arg>
|
||||
size_t print_argument(F putc, const char* format, const Arg& argument)
|
||||
size_t parse_format_and_print_argument(F putc, const char* format, Arg&& argument)
|
||||
{
|
||||
ValueFormat value_format;
|
||||
|
||||
|
@ -131,7 +139,7 @@ namespace BAN::Formatter
|
|||
if (format[i] != '}')
|
||||
return size_t(-1);
|
||||
|
||||
print_argument_impl(putc, argument, value_format);
|
||||
print_argument(putc, forward<Arg>(argument), value_format);
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
|
@ -221,33 +229,24 @@ namespace BAN::Formatter
|
|||
putc(value_to_base_char((value >> i) & 0xF, 16, format.upper));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
TEMPLATE SPECIALIZATIONS
|
||||
|
||||
*/
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, short value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, int value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F, integral T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
template<typename F, floating_point T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_floating(putc, value, format); }
|
||||
template<typename F, pointer T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, unsigned short value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned int value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned long long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument(F putc, char value, const ValueFormat&) { putc(value); }
|
||||
template<typename F> void print_argument(F putc, bool value, const ValueFormat&) { print(putc, value ? "true" : "false"); }
|
||||
template<typename F> void print_argument(F putc, const char* value, const ValueFormat&) { print(putc, value);}
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, float value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, double value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long double value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, char value, const ValueFormat&) { putc(value); }
|
||||
template<typename F> void print_argument_impl(F putc, signed char value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned char value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, bool value, const ValueFormat& format) { print(putc, value ? "true" : "false"); }
|
||||
|
||||
template<typename F, typename T> void print_argument_impl(F putc, T* value, const ValueFormat& format) { print_pointer(putc, (void*)value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, const char* value, const ValueFormat&) { print(putc, value);}
|
||||
//template<typename F> void print_argument(F putc, signed char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
//template<typename F> void print_argument(F putc, unsigned char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
//template<typename F, typename T> void print_argument(F putc, T* value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
|
||||
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ namespace BAN::Formatter
|
|||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument_impl(F putc, const String& string, const ValueFormat&)
|
||||
void print_argument(F putc, const String& string, const ValueFormat&)
|
||||
{
|
||||
for (String::size_type i = 0; i < string.size(); i++)
|
||||
putc(string[i]);
|
||||
|
|
|
@ -50,7 +50,7 @@ namespace BAN::Formatter
|
|||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument_impl(F putc, const StringView& sv, const ValueFormat&)
|
||||
void print_argument(F putc, const StringView& sv, const ValueFormat&)
|
||||
{
|
||||
for (StringView::size_type i = 0; i < sv.size(); i++)
|
||||
putc(sv[i]);
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace BAN::Formatter
|
|||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument_impl(F putc, const Time& time, const ValueFormat&)
|
||||
void print_argument(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" };
|
||||
|
|
|
@ -418,13 +418,13 @@ namespace BAN::Formatter
|
|||
{
|
||||
|
||||
template<typename F, typename T>
|
||||
void print_argument_impl(F putc, const Vector<T>& vector, const ValueFormat& format)
|
||||
void print_argument(F putc, const Vector<T>& vector, const ValueFormat& format)
|
||||
{
|
||||
putc('[');
|
||||
for (typename Vector<T>::size_type i = 0; i < vector.size(); i++)
|
||||
{
|
||||
if (i != 0) putc(',');
|
||||
print_argument_impl(putc, vector[i], format);
|
||||
print_argument(putc, vector[i], format);
|
||||
}
|
||||
putc(']');
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ struct framebuffer_info_t
|
|||
uint8_t bpp;
|
||||
uint8_t type;
|
||||
uint8_t color_info[6];
|
||||
} __attribute__((packed));
|
||||
};
|
||||
|
||||
struct multiboot_memory_map_t
|
||||
{
|
||||
|
@ -51,7 +51,7 @@ struct multiboot_info_t
|
|||
uint16_t vbe_interface_off;
|
||||
uint16_t vbe_interface_len;
|
||||
framebuffer_info_t framebuffer;
|
||||
} __attribute__((packed));
|
||||
};
|
||||
|
||||
extern "C" multiboot_info_t* g_multiboot_info;
|
||||
extern "C" uint32_t g_multiboot_magic;
|
||||
|
|
|
@ -54,6 +54,27 @@ ParsedCommandLine ParseCommandLine()
|
|||
return result;
|
||||
}
|
||||
|
||||
struct Test
|
||||
{
|
||||
Test() { dprintln("construct (default)"); }
|
||||
Test(const Test&) { dprintln("construct (copy)"); }
|
||||
Test(Test&&) { dprintln("construct (move)"); }
|
||||
~Test() { dprintln("destruct"); }
|
||||
Test& operator=(const Test&) { dprintln("assign (copy)"); return *this; }
|
||||
Test& operator=(Test&&) { dprintln("assign (move)"); return *this; }
|
||||
};
|
||||
|
||||
namespace BAN::Formatter
|
||||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument(F putc, const Test& test, const ValueFormat& format)
|
||||
{
|
||||
print_argument(putc, &test, format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" void kernel_main()
|
||||
{
|
||||
using namespace Kernel;
|
||||
|
|
Loading…
Reference in New Issue