diff options
Diffstat (limited to 'asl/io')
-rw-r--r-- | asl/io/BUILD.bazel | 26 | ||||
-rw-r--r-- | asl/io/print.cpp | 31 | ||||
-rw-r--r-- | asl/io/print.hpp | 30 | ||||
-rw-r--r-- | asl/io/writer.hpp | 20 |
4 files changed, 107 insertions, 0 deletions
diff --git a/asl/io/BUILD.bazel b/asl/io/BUILD.bazel new file mode 100644 index 0000000..b37787c --- /dev/null +++ b/asl/io/BUILD.bazel @@ -0,0 +1,26 @@ +cc_library( + name = "writer", + hdrs = [ + "writer.hpp", + ], + deps = [ + "//asl/base", + "//asl/types:span", + ], + visibility = ["//visibility:public"], +) + +cc_library( + name = "print", + hdrs = [ + "print.hpp", + ], + srcs = [ + "print.cpp", + ], + deps = [ + "//asl/formatting", + ":writer", + ], + visibility = ["//visibility:public"], +) diff --git a/asl/io/print.cpp b/asl/io/print.cpp new file mode 100644 index 0000000..0161bd1 --- /dev/null +++ b/asl/io/print.cpp @@ -0,0 +1,31 @@ +#include "asl/io/print.hpp" + +#include <cstdio> + +// @Todo Optimize this, maybe make buffered +class ConsoleWriter : public asl::Writer +{ + FILE* m_handle; + +public: + explicit ConsoleWriter(FILE* handle) + : m_handle{handle} + {} + + void write(asl::span<const asl::byte> s) override + { + fwrite(s.data(), 1, static_cast<size_t>(s.size()), m_handle); + } +}; + +asl::Writer* asl::print_internals::get_stdout_writer() +{ + static ConsoleWriter s_writer{stdout}; + return &s_writer; +} + +asl::Writer* asl::print_internals::get_stderr_writer() +{ + static ConsoleWriter s_writer{stderr}; + return &s_writer; +} diff --git a/asl/io/print.hpp b/asl/io/print.hpp new file mode 100644 index 0000000..a1899a8 --- /dev/null +++ b/asl/io/print.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "asl/formatting/format.hpp" +#include "asl/io/writer.hpp" + +namespace asl +{ + +namespace print_internals +{ + +// @Todo Make print writers thread safe +Writer* get_stdout_writer(); +Writer* get_stderr_writer(); + +} // namespace print_internals + +template<formattable... Args> +void print(string_view fmt, const Args&... args) +{ + format(print_internals::get_stdout_writer(), fmt, args...); +} + +template<formattable... Args> +void eprint(string_view fmt, const Args&... args) +{ + format(print_internals::get_stderr_writer(), fmt, args...); +} + +} // namespace asl diff --git a/asl/io/writer.hpp b/asl/io/writer.hpp new file mode 100644 index 0000000..e3cb313 --- /dev/null +++ b/asl/io/writer.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "asl/base/integers.hpp" +#include "asl/base/utility.hpp" +#include "asl/types/span.hpp" + +namespace asl +{ + +class Writer +{ +public: + Writer() = default; + ASL_DELETE_COPY_MOVE(Writer); + virtual ~Writer() = default; + + virtual void write(span<const byte>) = 0; +}; + +} // namespace asl |