From 35a996490200126e72775398fa3d6daa0ec4f435 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 13 Nov 2024 00:01:06 +0100 Subject: Introduce byte, use span on io Writer --- asl/format.hpp | 3 +-- asl/integers.hpp | 7 +++++++ asl/io.hpp | 4 ++-- asl/meta.hpp | 5 +++++ asl/print.cpp | 4 ++-- asl/span.hpp | 22 ++++++++++++++++++++-- asl/tests/format_tests.cpp | 8 ++++---- asl/tests/integers_tests.cpp | 2 ++ asl/tests/meta_tests.cpp | 5 +++++ asl/tests/span_tests.cpp | 28 ++++++++++++++++++++++++++++ 10 files changed, 76 insertions(+), 12 deletions(-) (limited to 'asl') diff --git a/asl/format.hpp b/asl/format.hpp index eb8379a..666e325 100644 --- a/asl/format.hpp +++ b/asl/format.hpp @@ -37,7 +37,6 @@ struct type_erased_arg {} }; -// @Todo Use span void format(writer*, const char* fmt, span args); } // namespace internals @@ -54,7 +53,7 @@ public: // @Todo Use string_view constexpr void write(const char* s, isize_t len) { - m_writer->write(s, len); + m_writer->write(as_bytes(span(s, len))); } }; diff --git a/asl/integers.hpp b/asl/integers.hpp index 8b7488f..65dadeb 100644 --- a/asl/integers.hpp +++ b/asl/integers.hpp @@ -23,3 +23,10 @@ using uint32_t = unsigned int; using size_t = uint64_t; using isize_t = int64_t; +namespace asl +{ + +enum class byte : uint8_t {}; + +} // namespace asl + diff --git a/asl/io.hpp b/asl/io.hpp index e7d1c9c..9c45624 100644 --- a/asl/io.hpp +++ b/asl/io.hpp @@ -2,6 +2,7 @@ #include "asl/integers.hpp" #include "asl/utility.hpp" +#include "asl/span.hpp" namespace asl { @@ -13,8 +14,7 @@ public: ASL_DELETE_COPY_MOVE(writer); virtual ~writer() = default; - // @Todo Use string view, or span of bytes? - virtual void write(const char* str, isize_t len) = 0; + virtual void write(span) = 0; }; } // namespace asl diff --git a/asl/meta.hpp b/asl/meta.hpp index 1c058c4..e64b4f2 100644 --- a/asl/meta.hpp +++ b/asl/meta.hpp @@ -82,6 +82,11 @@ template struct _un_const_helper { using type = T; }; template using un_const_t = _un_const_helper::type; +template struct _is_const_helper : false_type {}; +template struct _is_const_helper : true_type {}; + +template concept is_const = _is_const_helper::value; + template struct _un_volatile_helper { using type = T; }; template struct _un_volatile_helper { using type = T; }; diff --git a/asl/print.cpp b/asl/print.cpp index 1f55541..8180c04 100644 --- a/asl/print.cpp +++ b/asl/print.cpp @@ -12,9 +12,9 @@ public: : m_handle{handle} {} - void write(const char* str, isize_t len) override + void write(asl::span s) override { - fwrite(str, 1, static_cast(len), m_handle); + fwrite(s.data(), 1, static_cast(s.size()), m_handle); } }; diff --git a/asl/span.hpp b/asl/span.hpp index e0db059..7135509 100644 --- a/asl/span.hpp +++ b/asl/span.hpp @@ -171,8 +171,26 @@ public: ASL_ASSERT(sub_size >= 0 && sub_size <= size()); return span{ data() + size() - sub_size, sub_size }; } - - // @Todo as_(mutable_)bytes }; +template +inline span as_bytes(span s) +{ + return span( + reinterpret_cast(s.data()), + s.size_bytes()); +} + +template +inline span as_mutable_bytes(span s) + requires (!is_const) +{ + return span( + reinterpret_cast(s.data()), + s.size_bytes()); +} + +template +span(T (&)[kSize]) -> span; + } // namespace asl diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index bfde454..f051034 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -16,11 +16,11 @@ class StringSink : public asl::writer char* m_data{}; public: - void write(const char* str, isize_t len) override + void write(asl::span str) override { - m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1)); - memcpy(m_data + m_current_len, str, (size_t)len); - m_current_len += len; + m_data = (char*)realloc(m_data, (size_t)(m_current_len + str.size() + 1)); + memcpy(m_data + m_current_len, str.data(), (size_t)str.size()); + m_current_len += str.size(); m_data[m_current_len] = '\0'; } diff --git a/asl/tests/integers_tests.cpp b/asl/tests/integers_tests.cpp index d15168e..fdfa0fc 100644 --- a/asl/tests/integers_tests.cpp +++ b/asl/tests/integers_tests.cpp @@ -9,3 +9,5 @@ static_assert(sizeof(uint8_t) == 1); static_assert(sizeof(uint16_t) == 2); static_assert(sizeof(uint32_t) == 4); static_assert(sizeof(uint64_t) == 8); + +static_assert(sizeof(asl::byte) == 1); diff --git a/asl/tests/meta_tests.cpp b/asl/tests/meta_tests.cpp index 5fc4ef4..40b9046 100644 --- a/asl/tests/meta_tests.cpp +++ b/asl/tests/meta_tests.cpp @@ -197,3 +197,8 @@ static_assert(!asl::derived_from); static_assert(!asl::derived_from); static_assert(!asl::derived_from); +static_assert(!asl::is_const); +static_assert(asl::is_const); +static_assert(!asl::is_const); +static_assert(asl::is_const); + diff --git a/asl/tests/span_tests.cpp b/asl/tests/span_tests.cpp index 3baaf5c..547ba16 100644 --- a/asl/tests/span_tests.cpp +++ b/asl/tests/span_tests.cpp @@ -423,3 +423,31 @@ ASL_TEST(last_dynamic) ASL_TEST_EXPECT(s3[2] == 3); ASL_TEST_EXPECT(s3[3] == 4); } + +template +concept HasAsMutableBytes = requires(asl::span s) +{ + asl::as_mutable_bytes(s); +}; + +static_assert(HasAsMutableBytes); +static_assert(!HasAsMutableBytes); +static_assert(!HasAsMutableBytes); +static_assert(HasAsMutableBytes); + +ASL_TEST(as_bytes) +{ + uint32_t data[] = {0x01020304, 0x05060708}; + asl::span s1(data); + asl::span s2 = asl::as_bytes(s1); + + ASL_TEST_ASSERT(s2.size() == 8); + ASL_TEST_ASSERT(static_cast(s2[0]) == 0x04); + ASL_TEST_ASSERT(static_cast(s2[1]) == 0x03); + ASL_TEST_ASSERT(static_cast(s2[2]) == 0x02); + ASL_TEST_ASSERT(static_cast(s2[3]) == 0x01); + ASL_TEST_ASSERT(static_cast(s2[4]) == 0x08); + ASL_TEST_ASSERT(static_cast(s2[5]) == 0x07); + ASL_TEST_ASSERT(static_cast(s2[6]) == 0x06); + ASL_TEST_ASSERT(static_cast(s2[7]) == 0x05); +} -- cgit