From bc6d7e23c31cf8b7490b2eec2c874d5017ef2034 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Mon, 6 Jan 2025 07:02:03 -0700 Subject: [PATCH] :art: Add return type control to `byterator` peek/read aliases Problem: - Byterator supports e.g. `peek()` but not `peeku8()`. Solution: - Add return type control to the alias functions. --- include/stdx/byterator.hpp | 24 ++++++++++++++++++------ test/byterator.cpp | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/stdx/byterator.hpp b/include/stdx/byterator.hpp index 8c643a9..06f7782 100644 --- a/include/stdx/byterator.hpp +++ b/include/stdx/byterator.hpp @@ -188,20 +188,32 @@ template class byterator { ptr += sizeof(R); } - [[nodiscard]] auto peeku8() { return peek(); } - [[nodiscard]] auto readu8() { return read(); } + template [[nodiscard]] auto peeku8() { + return peek(); + } + template [[nodiscard]] auto readu8() { + return read(); + } template [[nodiscard]] auto writeu8(V &&v) { return write(static_cast(std::forward(v))); } - [[nodiscard]] auto peeku16() { return peek(); } - [[nodiscard]] auto readu16() { return read(); } + template [[nodiscard]] auto peeku16() { + return peek(); + } + template [[nodiscard]] auto readu16() { + return read(); + } template [[nodiscard]] auto writeu16(V &&v) { return write(static_cast(std::forward(v))); } - [[nodiscard]] auto peeku32() { return peek(); } - [[nodiscard]] auto readu32() { return read(); } + template [[nodiscard]] auto peeku32() { + return peek(); + } + template [[nodiscard]] auto readu32() { + return read(); + } template [[nodiscard]] auto writeu32(V &&v) { return write(static_cast(std::forward(v))); } diff --git a/test/byterator.cpp b/test/byterator.cpp index 7fa34ec..1353ef3 100644 --- a/test/byterator.cpp +++ b/test/byterator.cpp @@ -258,3 +258,21 @@ TEST_CASE("write enum (constrained size)", "[byterator]") { CHECK(a[0] == stdx::to_be(0x0302)); CHECK((i == j)); } + +TEST_CASE("peek enum (constrained size alias)", "[byterator]") { + auto const a = std::array{stdx::to_be(0x0102), + stdx::to_be(0x0304)}; + auto i = stdx::byterator{std::begin(a)}; + static_assert(std::is_same_v); + CHECK(i.peeku8() == E2::A); +} + +TEST_CASE("read enum (constrained size alias)", "[byterator]") { + auto const a = std::array{stdx::to_be(0x0102), + stdx::to_be(0x0304)}; + auto i = stdx::byterator{std::begin(a)}; + auto j = std::next(i); + static_assert(std::is_same_v); + CHECK(i.readu8() == E2::A); + CHECK((i == j)); +}