Skip to content

Commit af3620e

Browse files
committed
Add enum read/write to bit streams
* Include missing header
1 parent 610828a commit af3620e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

include/nalchi/bit_stream.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ class bit_stream_writer final
336336
return do_write<true>(data, min, max);
337337
}
338338

339+
/// @brief Writes an enum value to the bit stream.
340+
/// @tparam Enum Enum type.
341+
/// @param data Data to write.
342+
/// @param min Minimum value allowed for @p data.
343+
/// @param max Maximum value allowed for @p data.
344+
/// @return The stream itself.
345+
template <typename Enum>
346+
requires std::is_enum_v<Enum>
347+
auto write(Enum data, Enum min = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::min()),
348+
Enum max = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::max()))
349+
-> bit_stream_writer&
350+
{
351+
return do_write<true>(static_cast<std::underlying_type_t<Enum>>(data),
352+
static_cast<std::underlying_type_t<Enum>>(min),
353+
static_cast<std::underlying_type_t<Enum>>(max));
354+
}
355+
339356
/// @brief Writes a float value to the bit stream.
340357
/// @param data Data to write.
341358
/// @return The stream itself.
@@ -620,6 +637,22 @@ class bit_stream_measurer final
620637
return *this;
621638
}
622639

640+
/// @brief Fake-writes an enum value to the bit stream.
641+
/// @tparam Enum Enum type.
642+
/// @param data Data to fake-write.
643+
/// @param min Minimum value allowed for @p data.
644+
/// @param max Maximum value allowed for @p data.
645+
/// @return The stream itself.
646+
template <typename Enum>
647+
requires std::is_enum_v<Enum>
648+
auto write(Enum data, Enum min = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::min()),
649+
Enum max = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::max()))
650+
-> bit_stream_measurer&
651+
{
652+
return write(static_cast<std::underlying_type_t<Enum>>(data), static_cast<std::underlying_type_t<Enum>>(min),
653+
static_cast<std::underlying_type_t<Enum>>(max));
654+
}
655+
623656
/// @brief Fake-writes a float value to the bit stream.
624657
/// @param data Data to fake-write.
625658
/// @return The stream itself.
@@ -898,6 +931,27 @@ class bit_stream_reader final
898931
return do_read<true>(data, min, max);
899932
}
900933

934+
/// @brief Reads an enum value from the bit stream.
935+
/// @tparam Enum Enum type.
936+
/// @param data Data to read to.
937+
/// @param min Minimum value allowed for @p data.
938+
/// @param max Maximum value allowed for @p data.
939+
/// @return The stream itself.
940+
template <typename Enum>
941+
requires std::is_enum_v<Enum>
942+
auto read(Enum& data, Enum min = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::min()),
943+
Enum max = static_cast<Enum>(std::numeric_limits<std::underlying_type_t<Enum>>::max()))
944+
-> bit_stream_reader&
945+
{
946+
std::underlying_type_t<Enum> num;
947+
948+
if (do_read<true>(num, static_cast<std::underlying_type_t<Enum>>(min),
949+
static_cast<std::underlying_type_t<Enum>>(max)))
950+
data = static_cast<Enum>(num);
951+
952+
return *this;
953+
}
954+
901955
/// @brief Reads a float value from the bit stream.
902956
/// @param data Data to read to.
903957
/// @return The stream itself.

src/math.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <bit>
44
#include <concepts>
5+
#include <cstddef>
56

67
namespace nalchi
78
{

0 commit comments

Comments
 (0)