Skip to content

Commit bc6d7e2

Browse files
committed
🎨 Add return type control to byterator peek/read aliases
Problem: - Byterator supports e.g. `peek<std::uint8_t, E>()` but not `peeku8<E>()`. Solution: - Add return type control to the alias functions.
1 parent 4d57b2e commit bc6d7e2

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

include/stdx/byterator.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,32 @@ template <typename T> class byterator {
188188
ptr += sizeof(R);
189189
}
190190

191-
[[nodiscard]] auto peeku8() { return peek<std::uint8_t>(); }
192-
[[nodiscard]] auto readu8() { return read<std::uint8_t>(); }
191+
template <typename V = std::uint8_t> [[nodiscard]] auto peeku8() {
192+
return peek<std::uint8_t, V>();
193+
}
194+
template <typename V = std::uint8_t> [[nodiscard]] auto readu8() {
195+
return read<std::uint8_t, V>();
196+
}
193197
template <typename V> [[nodiscard]] auto writeu8(V &&v) {
194198
return write(static_cast<std::uint8_t>(std::forward<V>(v)));
195199
}
196200

197-
[[nodiscard]] auto peeku16() { return peek<std::uint16_t>(); }
198-
[[nodiscard]] auto readu16() { return read<std::uint16_t>(); }
201+
template <typename V = std::uint16_t> [[nodiscard]] auto peeku16() {
202+
return peek<std::uint16_t, V>();
203+
}
204+
template <typename V = std::uint16_t> [[nodiscard]] auto readu16() {
205+
return read<std::uint16_t, V>();
206+
}
199207
template <typename V> [[nodiscard]] auto writeu16(V &&v) {
200208
return write(static_cast<std::uint16_t>(std::forward<V>(v)));
201209
}
202210

203-
[[nodiscard]] auto peeku32() { return peek<std::uint32_t>(); }
204-
[[nodiscard]] auto readu32() { return read<std::uint32_t>(); }
211+
template <typename V = std::uint32_t> [[nodiscard]] auto peeku32() {
212+
return peek<std::uint32_t, V>();
213+
}
214+
template <typename V = std::uint32_t> [[nodiscard]] auto readu32() {
215+
return read<std::uint32_t, V>();
216+
}
205217
template <typename V> [[nodiscard]] auto writeu32(V &&v) {
206218
return write(static_cast<std::uint32_t>(std::forward<V>(v)));
207219
}

test/byterator.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,21 @@ TEST_CASE("write enum (constrained size)", "[byterator]") {
258258
CHECK(a[0] == stdx::to_be<std::uint16_t>(0x0302));
259259
CHECK((i == j));
260260
}
261+
262+
TEST_CASE("peek enum (constrained size alias)", "[byterator]") {
263+
auto const a = std::array{stdx::to_be<std::uint16_t>(0x0102),
264+
stdx::to_be<std::uint16_t>(0x0304)};
265+
auto i = stdx::byterator{std::begin(a)};
266+
static_assert(std::is_same_v<decltype(i.readu32()), std::uint32_t>);
267+
CHECK(i.peeku8<E2>() == E2::A);
268+
}
269+
270+
TEST_CASE("read enum (constrained size alias)", "[byterator]") {
271+
auto const a = std::array{stdx::to_be<std::uint16_t>(0x0102),
272+
stdx::to_be<std::uint16_t>(0x0304)};
273+
auto i = stdx::byterator{std::begin(a)};
274+
auto j = std::next(i);
275+
static_assert(std::is_same_v<decltype(i.readu32()), std::uint32_t>);
276+
CHECK(i.readu8<E2>() == E2::A);
277+
CHECK((i == j));
278+
}

0 commit comments

Comments
 (0)