diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-08-18 14:28:04 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-08-18 14:28:04 +0200 |
| commit | 2adaaa4c52b731dc7d796baa978ea86fd26d24b8 (patch) | |
| tree | 6a7f80721498fa67bf79591a7c7116f4001a6f84 | |
| parent | c1c2b677dda99bf1713ba28df21fd57d676b8eec (diff) | |
| download | kernel-2adaaa4c52b731dc7d796baa978ea86fd26d24b8.tar.xz kernel-2adaaa4c52b731dc7d796baa978ea86fd26d24b8.zip | |
kstd: enable formatting and decay for bytes
| -rw-r--r-- | kernel/kernel/main.cpp | 36 | ||||
| -rw-r--r-- | libs/kstd/kstd/bits/basic_unit.hpp | 4 | ||||
| -rw-r--r-- | libs/kstd/kstd/units.hpp | 12 |
3 files changed, 31 insertions, 21 deletions
diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp index 13697eb6..db47f167 100644 --- a/kernel/kernel/main.cpp +++ b/kernel/kernel/main.cpp @@ -42,9 +42,9 @@ auto run_demo() -> void // 2) read from the file kstd::vector<std::byte> buffer_1{10}; - auto bytes_read = kapi::filesystem::read(fd_1.value(), buffer_1)->value; + auto bytes_read = *kapi::filesystem::read(fd_1.value(), buffer_1); auto buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_1.data()), bytes_read}; - kstd::println("--> read {} bytes from /entrance/tickets.txt: {}", bytes_read, buffer_as_str); + kstd::println("--> read {} from /entrance/tickets.txt: {}", bytes_read, buffer_as_str); kstd::println(""); // 3) show that /entrance/information/info_1.txt is not accessible before mounting @@ -81,9 +81,9 @@ auto run_demo() -> void // 6) read from the new file kstd::vector<std::byte> buffer_2{10}; - bytes_read = kapi::filesystem::read(fd_2.value(), buffer_2)->value; + bytes_read = *kapi::filesystem::read(fd_2.value(), buffer_2); buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_2.data()), static_cast<size_t>(bytes_read)}; - kstd::println("--> read {} bytes from /entrance/information/info_1.txt: {} ", bytes_read, buffer_as_str); + kstd::println("--> read {} from /entrance/information/info_1.txt: {}", bytes_read, buffer_as_str); // 7) open device as file kstd::println("attempting to open /dev/ram2 as a file"); @@ -99,14 +99,14 @@ auto run_demo() -> void // 8) read from the device file kstd::vector<std::byte> buffer_3{2}; - bytes_read = kapi::filesystem::read(fd_3.value(), buffer_3)->value; - kstd::println("--> read {} bytes from /dev/ram2: {::#04x} ", bytes_read, buffer_3); + bytes_read = *kapi::filesystem::read(fd_3.value(), buffer_3); + kstd::println("--> read {} from /dev/ram2: {::#04x}", bytes_read, buffer_3); // 9) write to the device file auto const default_buffer_value = std::byte{0xAA}; kstd::vector<std::byte> write_buffer{default_buffer_value, default_buffer_value}; - auto bytes_written = kapi::filesystem::write(fd_3.value(), write_buffer)->value; - kstd::println("--> written {} bytes to /dev/ram2: {::#04x}", bytes_written, write_buffer); + auto bytes_written = *kapi::filesystem::write(fd_3.value(), write_buffer); + kstd::println("--> wrote {} to /dev/ram2: {::#04x}", bytes_written, write_buffer); // 10) do memory dump to show that the write to the device file had an effect @@ -137,8 +137,8 @@ auto run_demo() -> void kstd::vector<std::byte> test_write_buffer{ std::byte{'H'}, std::byte{'e'}, std::byte{'l'}, std::byte{'l'}, std::byte{'o'}, std::byte{' '}, std::byte{'T'}, std::byte{'e'}, std::byte{'a'}, std::byte{'c'}, std::byte{'h'}, std::byte{'O'}, std::byte{'S'}}; - bytes_written = kapi::filesystem::write(*fd_4, test_write_buffer)->value; - kstd::println("--> written {} bytes to /test_files/test_file.txt: {::#04x}", bytes_written, test_write_buffer); + bytes_written = *kapi::filesystem::write(*fd_4, test_write_buffer); + kstd::println("--> wrote {} to /test_files/test_file.txt: {::#04x}", bytes_written, test_write_buffer); // 14) dmp the module after create new directory and file // -exec monitor memsave 0xffffffff8025b000 0xA00000 dump_after.bin @@ -161,9 +161,9 @@ auto run_demo() -> void } else { - bytes_read = read->value; + bytes_read = *read; } - kstd::println("--> read {} bytes from /dev/null", bytes_read); + kstd::println("--> read {} from /dev/null", bytes_read); if (auto written = kapi::filesystem::write(fd_5.value(), buffer_4); !written) { @@ -171,9 +171,9 @@ auto run_demo() -> void } else { - bytes_written = written->value; + bytes_written = *written; } - kstd::println("--> written {} bytes to /dev/null", bytes_written); + kstd::println("--> wrote {} to /dev/null", bytes_written); // 16) read from /dev/null auto fd_6 = kapi::filesystem::open("/dev/zero"); @@ -193,9 +193,9 @@ auto run_demo() -> void } else { - bytes_read = read->value; + bytes_read = *read; } - kstd::println("--> read {} bytes from /dev/zero: {::#04x}", bytes_read, buffer_5); + kstd::println("--> read {} from /dev/zero: {::#04x}", bytes_read, buffer_5); if (auto written = kapi::filesystem::write(fd_6.value(), buffer_5); !written) { @@ -203,9 +203,9 @@ auto run_demo() -> void } else { - bytes_written = written->value; + bytes_written = *written; } - kstd::println("--> written {} bytes to /dev/zero", bytes_written); + kstd::println("--> wrote {} to /dev/zero", bytes_written); } auto main() -> int diff --git a/libs/kstd/kstd/bits/basic_unit.hpp b/libs/kstd/kstd/bits/basic_unit.hpp index c7f794ea..750b0da6 100644 --- a/libs/kstd/kstd/bits/basic_unit.hpp +++ b/libs/kstd/kstd/bits/basic_unit.hpp @@ -8,7 +8,7 @@ namespace kstd { //! A basic template for strongly typed units. - template<typename ValueType, typename Tags> + template<typename ValueType, typename Tags, bool ImplicitDecay = false> struct basic_unit { using value_type = ValueType; @@ -22,7 +22,7 @@ namespace kstd {} template<std::integral T> - explicit constexpr operator T() const noexcept + explicit(!ImplicitDecay) constexpr operator T() const noexcept { return value; } diff --git a/libs/kstd/kstd/units.hpp b/libs/kstd/kstd/units.hpp index 61224336..95e531b8 100644 --- a/libs/kstd/kstd/units.hpp +++ b/libs/kstd/kstd/units.hpp @@ -2,6 +2,7 @@ #define KSTD_UNITS_HPP #include <kstd/bits/basic_unit.hpp> +#include <kstd/format.hpp> #include <kstd/result.hpp> #include <kstd/system_error.hpp> @@ -11,7 +12,7 @@ namespace kstd { - using bytes = basic_unit<std::size_t, struct bytes_tag>; + using bytes = basic_unit<std::size_t, struct bytes_tag, true>; using offset = basic_unit<std::ptrdiff_t, struct byte_offset_tag>; @@ -127,4 +128,13 @@ namespace kstd } // namespace kstd +template<> +struct kstd::formatter<kstd::bytes> : kstd::formatter<std::size_t> +{ + constexpr auto format(kstd::bytes const & id, kstd::format_context & ctx) const -> void + { + kstd::formatter<std::size_t>::format(id.value, ctx); + ctx.push(" bytes"); + } +}; #endif |
