diff --git a/include/libhal-util/bit.hpp b/include/libhal-util/bit.hpp index 1b7f6fb..60996ce 100644 --- a/include/libhal-util/bit.hpp +++ b/include/libhal-util/bit.hpp @@ -19,8 +19,8 @@ #include #include -namespace hal::bit { -struct mask +namespace hal { +struct bitmask { /// Where the bit mask starts std::uint32_t position; @@ -28,96 +28,102 @@ struct mask std::uint32_t width; /** - * @brief Generate, at compile time, a mask that spans the from position1 to - * position2. + * @brief Generate, at compile time, a bitmask that spans the from position1 + * to position2. * - * If position1 is the same position2 then the mask will have length of 1 and - * the bit position will be the value of position1. + * If position1 is the same position2 then the bitmask will have length of 1 + * and the bit position will be the value of position1. * * position1 and position2 can be in any order so long as they span the - * distance from the start and end of the masked range. + * distance from the start and end of the bitmask range. * * @tparam position1 - bit position 1 * @tparam position2 - bit position 2 - * @return consteval mask - bit mask represented by the two bit positions + * @return consteval bitmask - bit bitmask represented by the two bit + * positions */ template - static consteval mask from() + static consteval bitmask from() { constexpr std::uint32_t plus_one = 1; if constexpr (position1 < position2) { - return mask{ .position = position1, - .width = plus_one + (position2 - position1) }; + return bitmask{ .position = position1, + .width = plus_one + (position2 - position1) }; } else { - return mask{ .position = position2, - .width = plus_one + (position1 - position2) }; + return bitmask{ .position = position2, + .width = plus_one + (position1 - position2) }; } } /** - * @brief Generate, at compile time, a single bit width mask at position + * @brief Generate, at compile time, a single bit width bitmask at position * - * @tparam position - the bit to make the mask for - * @return constexpr mask - bit mask with the position bit set to position + * @tparam position - the bit to make the bitmask for + * @return constexpr bitmask - bit bitmask with the position bit set to + * position */ template - static constexpr mask from() + static constexpr bitmask from() { - return mask{ .position = position, .width = 1U }; + return bitmask{ .position = position, .width = 1U }; } /** - * @brief Generate, at compile time, a mask that spans the from position1 to - * position2. + * @brief Generate, at compile time, a bitmask that spans the from position1 + * to position2. * - * If position1 is the same position2 then the mask will have length of 1 and - * the bit position will be the value of position1. + * If position1 is the same position2 then the bitmask will have length of 1 + * and the bit position will be the value of position1. * * position1 and position2 can be in any order so long as they span the - * distance from the start and end of the masked range. + * distance from the start and end of the bitmask range. * * @param position1 - bit position 1 * @param position2 - bit position 2 - * @return consteval mask - bit mask represented by the two bit positions + * @return consteval bitmask - bit bitmask represented by the two bit + * positions */ - static consteval mask from(std::uint32_t position1, std::uint32_t position2) + static consteval bitmask from(std::uint32_t position1, + std::uint32_t position2) { constexpr std::uint32_t plus_one = 1; if (position1 < position2) { - return mask{ .position = position1, - .width = plus_one + (position2 - position1) }; + return bitmask{ .position = position1, + .width = plus_one + (position2 - position1) }; } else { - return mask{ .position = position2, - .width = plus_one + (position1 - position2) }; + return bitmask{ .position = position2, + .width = plus_one + (position1 - position2) }; } } /** - * @brief Generate, at runtime, a single bit width mask at position + * @brief Generate, at runtime, a single bit width bitmask at position * - * @param position - the bit to make the mask for - * @return constexpr mask - bit mask with the position bit set to position + * @param position - the bit to make the bitmask for + * @return constexpr bitmask - bit bitmask with the position bit set to + * position */ - static constexpr mask from(std::uint32_t position) + static constexpr bitmask from(std::uint32_t position) { - return mask{ .position = position, .width = 1U }; + return bitmask{ .position = position, .width = 1U }; } /** - * @brief Convert mask to a integral representation but with bit position at 0 + * @brief Convert bitmask to a integral representation but with bit position + * at 0 * * The integral presentation will have 1 bits starting from the position bit * up to bit position + width. All other bits will be 0s. * * For example: * - * value(mask{ + * value(bitmask{ * .position = 1, * .width = 4, * }); // returns = 0b0000'0000'0000'1111; * - * @tparam T - unsigned integral type to hold the mask - * @return constexpr auto - mask value as an unsigned integer + * @tparam T - unsigned integral type to hold the bitmask + * @return constexpr auto - bitmask value as an unsigned integer */ template constexpr auto origin() const @@ -129,8 +135,8 @@ struct mask // At compile time calculate the number of bits in the target parameter. constexpr size_t target_width = sizeof(T) * 8; - // Create mask by shifting the set of 1s down so that the number of 1s from - // bit position 0 is equal to the width parameter. + // Create bitmask by shifting the set of 1s down so that the number of 1s + // from bit position 0 is equal to the width parameter. T mask_at_origin = static_cast(field_of_ones >> (target_width - width)); return mask_at_origin; @@ -144,7 +150,7 @@ struct mask * * For example: * - * value(mask{ + * value(bitmask{ * .position = 1, * .width = 4, * }); // returns = 0b0000'0000'0001'1110; @@ -165,7 +171,7 @@ struct mask * @return true - the masks are the same * @return false - the masks are not the same */ - constexpr bool operator==(const mask& other) + constexpr bool operator==(const bitmask& other) { return other.position == position && other.width == width; } @@ -183,16 +189,16 @@ struct byte_mask * @brief Mask value defined at compile time * */ - static constexpr hal::bit::mask value{ .position = ByteIndex, .width = 8 }; + static constexpr hal::bitmask value{ .position = ByteIndex, .width = 8 }; }; /** - * @brief Shorthand for using hal::bit::byte_mask::value + * @brief Shorthand for using hal::byte_mask::value * * @tparam ByteIndex - the byte position to make a mask for */ template -constexpr hal::bit::mask byte_m = byte_mask::value; +constexpr hal::bitmask byte_m = byte_mask::value; /** * @brief Helper for generating nibble position masks @@ -202,18 +208,18 @@ constexpr hal::bit::mask byte_m = byte_mask::value; template struct nibble_mask { - static constexpr hal::bit::mask value{ .position = NibbleIndex, .width = 4 }; + static constexpr hal::bitmask value{ .position = NibbleIndex, .width = 4 }; }; /** - * @brief Shorthand for using hal::bit::nibble_mask::value + * @brief Shorthand for using hal::nibble_mask::value * * @tparam NibbleIndex - the nibble position to make a mask for */ template -constexpr hal::bit::mask nibble_m = nibble_mask::value; +constexpr hal::bitmask nibble_m = nibble_mask::value; -template +template constexpr auto extract(std::unsigned_integral auto p_value) { using T = decltype(p_value); @@ -225,7 +231,7 @@ constexpr auto extract(std::unsigned_integral auto p_value) return static_cast(masked); } -constexpr auto extract(mask p_field, std::unsigned_integral auto p_value) +constexpr auto extract(bitmask p_field, std::unsigned_integral auto p_value) { using T = decltype(p_value); // Shift desired value to the right to position 0 @@ -247,7 +253,7 @@ class value { } - template + template constexpr auto& set() { static_assert(field.position < width, @@ -259,7 +265,7 @@ class value return *this; } - constexpr auto& set(mask p_field) + constexpr auto& set(bitmask p_field) { const auto mask = static_cast(1U << p_field.position); @@ -268,7 +274,7 @@ class value return *this; } - template + template constexpr auto& clear() { static_assert(field.position < width, @@ -281,7 +287,7 @@ class value return *this; } - constexpr auto& clear(mask p_field) + constexpr auto& clear(bitmask p_field) { const auto mask = static_cast(1U << p_field.position); const auto inverted_mask = ~mask; @@ -291,7 +297,7 @@ class value return *this; } - template + template constexpr auto& toggle() { static_assert(field.position < width, @@ -304,7 +310,7 @@ class value return *this; } - constexpr auto& toggle(mask p_field) + constexpr auto& toggle(bitmask p_field) { const auto mask = static_cast(1U << p_field.position); @@ -313,7 +319,7 @@ class value return *this; } - template + template constexpr auto& insert(std::unsigned_integral auto p_value) { const auto value_to_insert = static_cast(p_value); @@ -330,7 +336,7 @@ class value return *this; } - constexpr auto& insert(mask p_field, std::unsigned_integral auto p_value) + constexpr auto& insert(bitmask p_field, std::unsigned_integral auto p_value) { // AND value with mask to remove any bits beyond the specified width. // Shift masked value into bit position and OR with target value. @@ -378,4 +384,4 @@ class modify : public value private: volatile T* m_pointer; }; -} // namespace hal::bit +} // namespace hal diff --git a/include/libhal-util/streams.hpp b/include/libhal-util/streams.hpp index a597127..ac92e26 100644 --- a/include/libhal-util/streams.hpp +++ b/include/libhal-util/streams.hpp @@ -22,12 +22,11 @@ #include namespace hal { -namespace stream { /** * @brief Discard received bytes until the sequence is found * */ -class find +class stream_find { public: /** @@ -37,11 +36,11 @@ class find * pointed to by this span must outlive this object, or not be used when the * lifetime of that data is no longer available. */ - explicit find(std::span p_sequence); + explicit stream_find(std::span p_sequence); friend std::span operator|( const std::span& p_input_data, - find& p_self); + stream_find& p_self); work_state state(); @@ -54,7 +53,7 @@ class find * @brief Non-blocking callable for reading serial data into a buffer * */ -class fill +class stream_fill { public: /** @@ -62,7 +61,7 @@ class fill * * @param p_buffer - buffer to read data into */ - explicit fill(std::span p_buffer); + explicit stream_fill(std::span p_buffer); /** * @brief Construct a new fill object @@ -71,11 +70,11 @@ class fill * @param p_fill_amount - reference to a size value to limit the fill amount * by. */ - fill(std::span p_buffer, const size_t& p_fill_amount); + stream_fill(std::span p_buffer, const size_t& p_fill_amount); friend std::span operator|( const std::span& p_input_data, - fill& p_self); + stream_fill& p_self); work_state state(); @@ -88,7 +87,7 @@ class fill * @brief Discard received bytes until the sequence is found * */ -class fill_upto +class stream_fill_upto { public: /** @@ -99,12 +98,12 @@ class fill_upto * lifetime of that data is no longer available. * @param p_buffer - buffer to fill data into */ - fill_upto(std::span p_sequence, - std::span p_buffer); + stream_fill_upto(std::span p_sequence, + std::span p_buffer); friend std::span operator|( const std::span& p_input_data, - fill_upto& p_self); + stream_fill_upto& p_self); work_state state(); @@ -123,17 +122,17 @@ class fill_upto * */ template -class parse +class stream_parse { public: /** * @brief Construct a new parse object */ - explicit parse() = default; + explicit stream_parse() = default; friend std::span operator|( const std::span& p_input_data, - parse& p_self) + stream_parse& p_self) { if (p_self.m_finished) { return p_input_data; @@ -176,23 +175,22 @@ class parse * @brief Skip number of bytes in a byte stream * */ -class skip +class stream_skip { public: /** * @brief Construct a new skip object * */ - explicit skip(size_t p_skip); + explicit stream_skip(size_t p_skip); friend std::span operator|( const std::span& p_input_data, - skip& p_self); + stream_skip& p_self); work_state state(); private: size_t m_skip; }; -} // namespace stream } // namespace hal diff --git a/src/streams.cpp b/src/streams.cpp index c8b13ca..82857e7 100644 --- a/src/streams.cpp +++ b/src/streams.cpp @@ -23,15 +23,14 @@ #include namespace hal { -namespace stream { -find::find(std::span p_sequence) +stream_find::stream_find(std::span p_sequence) : m_sequence(p_sequence) { } std::span operator|( const std::span& p_input_data, - find& p_self) + stream_find& p_self) { if (p_input_data.empty()) { return p_input_data; @@ -58,7 +57,7 @@ std::span operator|( return p_input_data.subspan(p_input_data.size()); } -work_state find::state() +work_state stream_find::state() { if (m_search_index == m_sequence.size()) { return work_state::finished; @@ -66,12 +65,13 @@ work_state find::state() return work_state::in_progress; } -fill::fill(std::span p_buffer) +stream_fill::stream_fill(std::span p_buffer) : m_buffer(p_buffer) { } -fill::fill(std::span p_buffer, const size_t& p_fill_amount) +stream_fill::stream_fill(std::span p_buffer, + const size_t& p_fill_amount) : m_buffer(p_buffer) , m_fill_amount(&p_fill_amount) { @@ -79,7 +79,7 @@ fill::fill(std::span p_buffer, const size_t& p_fill_amount) std::span operator|( const std::span& p_input_data, - fill& p_self) + stream_fill& p_self) { if (p_input_data.empty() || p_self.m_buffer.empty()) { return p_input_data; @@ -101,7 +101,7 @@ std::span operator|( return p_input_data.subspan(min_size); } -work_state fill::state() +work_state stream_fill::state() { if (m_buffer.empty()) { return work_state::finished; @@ -109,8 +109,8 @@ work_state fill::state() return work_state::in_progress; } -fill_upto::fill_upto(std::span p_sequence, - std::span p_buffer) +stream_fill_upto::stream_fill_upto(std::span p_sequence, + std::span p_buffer) : m_sequence(p_sequence) , m_buffer(p_buffer) { @@ -118,7 +118,7 @@ fill_upto::fill_upto(std::span p_sequence, std::span operator|( const std::span& p_input_data, - fill_upto& p_self) + stream_fill_upto& p_self) { if (p_input_data.empty() || p_self.m_sequence.size() == p_self.m_search_index || @@ -150,7 +150,7 @@ std::span operator|( return p_input_data.subspan(min_size); } -work_state fill_upto::state() +work_state stream_fill_upto::state() { if (m_buffer.empty() && m_search_index != m_sequence.size()) { return work_state::failed; @@ -161,24 +161,24 @@ work_state fill_upto::state() return work_state::in_progress; } -std::span fill_upto::span() +std::span stream_fill_upto::span() { return m_buffer.subspan(0, m_fill_amount); } -std::span fill_upto::unfilled() +std::span stream_fill_upto::unfilled() { return m_buffer.subspan(m_fill_amount); } -skip::skip(size_t p_skip) +stream_skip::stream_skip(size_t p_skip) : m_skip(p_skip) { } std::span operator|( const std::span& p_input_data, - skip& p_self) + stream_skip& p_self) { if (p_input_data.empty()) { return p_input_data; @@ -193,9 +193,8 @@ std::span operator|( return p_input_data.subspan(min); } -work_state skip::state() +work_state stream_skip::state() { return (m_skip == 0) ? work_state::finished : work_state::in_progress; } -} // namespace stream -} // namespace hal +} // namespace hal \ No newline at end of file diff --git a/tests/bit.test.cpp b/tests/bit.test.cpp index 854e544..80eed56 100644 --- a/tests/bit.test.cpp +++ b/tests/bit.test.cpp @@ -24,21 +24,21 @@ void bit_test() "hal::bit