diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-12-19 11:46:46 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-12-19 11:46:46 +0100 |
| commit | de96b0588ab680e1002c12df7ea7900d7eb71cf8 (patch) | |
| tree | 41728f4f5c77a4d96dc3d89096483dfee75b3482 /libs | |
| parent | 266dde7d535d997a45f6eef41e44ebcaa516b75a (diff) | |
| download | teachos-de96b0588ab680e1002c12df7ea7900d7eb71cf8.tar.xz teachos-de96b0588ab680e1002c12df7ea7900d7eb71cf8.zip | |
kstd: move println to kstd
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/include/kstd/print | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/print b/libs/kstd/include/kstd/print new file mode 100644 index 0000000..df42997 --- /dev/null +++ b/libs/kstd/include/kstd/print @@ -0,0 +1,86 @@ +#ifndef KSTD_PRINT +#define KSTD_PRINT + +#include <kstd/format> + +#include <array> +#include <string_view> +#include <type_traits> + +namespace kstd +{ + + enum struct print_sink + { + stdout, + stderr, + }; + + namespace os + { + auto vprint(print_sink sink, std::string_view format, kstd::format_args args) -> void; + } // namespace os + + //! @qualifier kernel-defined + //! Format the given string using the given arguments and print it to the currently active output device. + //! + //! @param format The format string + //! @param args The arguments to use to place in the format string's placeholders. + template<typename... Args> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) + auto print(kstd::format_string<std::type_identity_t<Args>...> format, Args &&... args) -> void + { + auto arguments = std::array<kstd::format_arg, sizeof...(Args)>{ + kstd::format_arg{&args, kstd::format_dispatcher<std::remove_cvref_t<Args>>} + ... + }; + os::vprint(print_sink::stdout, format.str, kstd::format_args{arguments.data(), sizeof...(Args)}); + } + + //! @qualifier kernel-defined + //! Format the given error string using the given arguments and print it to the currently active output device. + //! + //! @param format The format string + //! @param args The arguments to use to place in the format string's placeholders. + template<typename... Args> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) + auto print(print_sink sink, kstd::format_string<std::type_identity_t<Args>...> format, Args &&... args) -> void + { + auto arguments = std::array<kstd::format_arg, sizeof...(Args)>{ + kstd::format_arg{&args, kstd::format_dispatcher<std::remove_cvref_t<Args>>} + ... + }; + os::vprint(sink, format.str, kstd::format_args{arguments.data(), sizeof...(Args)}); + } + + //! @qualifier kernel-defined + //! Format the given string using the given arguments and print it, including a newline, to the currently active + //! output device. + //! + //! @param format The format string + //! @param args The arguments to use to place in the format string's placeholders. + template<typename... Args> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) + auto println(kstd::format_string<std::type_identity_t<Args>...> format, Args &&... args) -> void + { + print(format, std::forward<Args>(args)...); + print(print_sink::stdout, "\n"); + } + + //! @qualifier kernel-defined + //! Format the given error string using the given arguments and print it, including a newline, to the currently active + //! output device. + //! + //! @param format The format string + //! @param args The arguments + template<typename... Args> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) + auto println(print_sink sink, kstd::format_string<std::type_identity_t<Args>...> format, Args &&... args) -> void + { + print(sink, format, std::forward<Args>(args)...); + print(sink, "\n"); + } + +} // namespace kstd + +#endif
\ No newline at end of file |
