Add console printing
This commit is contained in:
@ -5,15 +5,18 @@ cc_library(
|
|||||||
"assert.hpp",
|
"assert.hpp",
|
||||||
"format.hpp",
|
"format.hpp",
|
||||||
"integers.hpp",
|
"integers.hpp",
|
||||||
|
"io.hpp",
|
||||||
"layout.hpp",
|
"layout.hpp",
|
||||||
"maybe_uninit.hpp",
|
"maybe_uninit.hpp",
|
||||||
"memory.hpp",
|
"memory.hpp",
|
||||||
"meta.hpp",
|
"meta.hpp",
|
||||||
"option.hpp",
|
"option.hpp",
|
||||||
|
"print.hpp",
|
||||||
"utility.hpp",
|
"utility.hpp",
|
||||||
],
|
],
|
||||||
srcs = [
|
srcs = [
|
||||||
"format.cpp",
|
"format.cpp",
|
||||||
|
"print_win32.cpp",
|
||||||
],
|
],
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
)
|
)
|
||||||
|
@ -80,6 +80,18 @@ void asl::AslFormat(formatter& f, double)
|
|||||||
f.write("<DOUBLE>", 8); // @Todo Float formatting
|
f.write("<DOUBLE>", 8); // @Todo Float formatting
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void asl::AslFormat(formatter& f, bool v)
|
||||||
|
{
|
||||||
|
if (v)
|
||||||
|
{
|
||||||
|
f.write("true", 4);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f.write("false", 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void asl::AslFormat(formatter& f, uint8_t v)
|
void asl::AslFormat(formatter& f, uint8_t v)
|
||||||
{
|
{
|
||||||
AslFormat(f, static_cast<uint64_t>(v));
|
AslFormat(f, static_cast<uint64_t>(v));
|
||||||
|
@ -2,23 +2,11 @@
|
|||||||
|
|
||||||
#include "asl/integers.hpp"
|
#include "asl/integers.hpp"
|
||||||
#include "asl/meta.hpp"
|
#include "asl/meta.hpp"
|
||||||
#include "asl/utility.hpp"
|
#include "asl/io.hpp"
|
||||||
|
|
||||||
namespace asl
|
namespace asl
|
||||||
{
|
{
|
||||||
|
|
||||||
// @Todo Move this to io
|
|
||||||
class writer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
writer() = default;
|
|
||||||
ASL_DELETE_COPY_MOVE(writer);
|
|
||||||
virtual ~writer() = default;
|
|
||||||
|
|
||||||
// @Todo Use string view, or span of bytes?
|
|
||||||
virtual void write(const char* str, int64_t len) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class formatter;
|
class formatter;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -70,21 +58,21 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// @Todo Use string_view
|
// @Todo Use string_view
|
||||||
template<typename... Args>
|
template<formattable... Args>
|
||||||
void format(writer* w, const char* fmt, const Args&... args)
|
void format(writer* w, const char* fmt, const Args&... args)
|
||||||
{
|
{
|
||||||
format_internals::type_erased_arg type_erased_args[] = {
|
if constexpr (types_count<Args...> > 0)
|
||||||
format_internals::type_erased_arg(args)...
|
{
|
||||||
};
|
format_internals::type_erased_arg type_erased_args[] = {
|
||||||
|
format_internals::type_erased_arg(args)...
|
||||||
|
};
|
||||||
|
|
||||||
// @Todo Use array extent
|
format_internals::format(w, fmt, type_erased_args, types_count<Args...>);
|
||||||
format_internals::format(w, fmt, type_erased_args, types_count<Args...>);
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
// @Todo Use string_view
|
format_internals::format(w, fmt, nullptr, 0);
|
||||||
inline void format(writer* w, const char* fmt)
|
}
|
||||||
{
|
|
||||||
format_internals::format(w, fmt, nullptr, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int64_t N>
|
template<int64_t N>
|
||||||
@ -96,6 +84,8 @@ void AslFormat(formatter& f, const char (&str)[N])
|
|||||||
void AslFormat(formatter& f, float);
|
void AslFormat(formatter& f, float);
|
||||||
void AslFormat(formatter& f, double);
|
void AslFormat(formatter& f, double);
|
||||||
|
|
||||||
|
void AslFormat(formatter& f, bool);
|
||||||
|
|
||||||
void AslFormat(formatter& f, uint8_t);
|
void AslFormat(formatter& f, uint8_t);
|
||||||
void AslFormat(formatter& f, uint16_t);
|
void AslFormat(formatter& f, uint16_t);
|
||||||
void AslFormat(formatter& f, uint32_t);
|
void AslFormat(formatter& f, uint32_t);
|
||||||
|
@ -98,5 +98,9 @@ int main()
|
|||||||
asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
|
asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
|
||||||
assert(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
|
assert(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
|
||||||
|
|
||||||
|
sink.reset();
|
||||||
|
asl::format(&sink, "{} {}", true, false);
|
||||||
|
assert(strcmp(sink.cstr(), "true false") == 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
20
asl/io.hpp
Normal file
20
asl/io.hpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "asl/integers.hpp"
|
||||||
|
#include "asl/utility.hpp"
|
||||||
|
|
||||||
|
namespace asl
|
||||||
|
{
|
||||||
|
|
||||||
|
class writer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
writer() = default;
|
||||||
|
ASL_DELETE_COPY_MOVE(writer);
|
||||||
|
virtual ~writer() = default;
|
||||||
|
|
||||||
|
// @Todo Use string view, or span of bytes?
|
||||||
|
virtual void write(const char* str, int64_t len) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace asl
|
31
asl/print.hpp
Normal file
31
asl/print.hpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "asl/format.hpp"
|
||||||
|
|
||||||
|
namespace asl
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace print_internals
|
||||||
|
{
|
||||||
|
|
||||||
|
// @Todo Make print writers thread safe
|
||||||
|
writer* get_stdout_writer();
|
||||||
|
writer* get_stderr_writer();
|
||||||
|
|
||||||
|
} // namespace print_internals
|
||||||
|
|
||||||
|
// @Todo Use string_view
|
||||||
|
template<formattable... Args>
|
||||||
|
void print(const char* fmt, const Args&... args)
|
||||||
|
{
|
||||||
|
format(print_internals::get_stdout_writer(), fmt, args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Todo Use string_view
|
||||||
|
template<formattable... Args>
|
||||||
|
void eprint(const char* fmt, const Args&... args)
|
||||||
|
{
|
||||||
|
format(print_internals::get_stderr_writer(), fmt, args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace asl
|
33
asl/print_win32.cpp
Normal file
33
asl/print_win32.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "asl/print.hpp"
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
// @Todo Optimize this, maybe make buffered
|
||||||
|
class Win32ConsoleWriter : public asl::writer
|
||||||
|
{
|
||||||
|
HANDLE m_handle;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit Win32ConsoleWriter(HANDLE handle)
|
||||||
|
: m_handle{handle}
|
||||||
|
{}
|
||||||
|
|
||||||
|
void write(const char* str, int64_t len) override
|
||||||
|
{
|
||||||
|
::WriteConsoleA(m_handle, str, static_cast<DWORD>(len), nullptr, nullptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
asl::writer* asl::print_internals::get_stdout_writer()
|
||||||
|
{
|
||||||
|
static Win32ConsoleWriter writer{::GetStdHandle(STD_OUTPUT_HANDLE)};
|
||||||
|
return &writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
asl::writer* asl::print_internals::get_stderr_writer()
|
||||||
|
{
|
||||||
|
static Win32ConsoleWriter writer{::GetStdHandle(STD_ERROR_HANDLE)};
|
||||||
|
return &writer;
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user