C++: refactor flags into their own structs (for better code readability)

backup
Christian Seiler 2 weeks ago
parent 6602bec421
commit c77efa8461
  1. 28
      cpp/src/monoformat_bithelpers.hpp
  2. 43
      cpp/src/monoformat_parseonly.cpp
  3. 6
      cpp/src/monoformat_schema.cpp
  4. 67
      cpp/src/monoformat_schema.hpp
  5. 109
      cpp/src/monoformat_structured.cpp
  6. 30
      cpp/src/monoformat_structured.hpp

@ -63,6 +63,34 @@ constexpr inline void unsetBit(std::byte& value, OtherInt bit) {
value = static_cast<std::byte>(static_cast<std::uint8_t>(value) & ~mask); value = static_cast<std::byte>(static_cast<std::uint8_t>(value) & ~mask);
} }
template<std::integral Int, std::integral OtherInt>
constexpr inline void maybeSetBit(Int& value, OtherInt bit, bool set) {
if (bit < 0 || bit >= (sizeof(Int) * CHAR_BIT)) [[unlikely]] {
return;
}
Int mask = Int{1} << static_cast<Int>(bit);
if (set) {
value |= mask;
} else {
value &= ~mask;
}
}
template<std::integral OtherInt>
constexpr inline void maybeSetBit(std::byte& value, OtherInt bit, bool set) {
if (bit < 0 || bit >= CHAR_BIT) [[unlikely]] {
return;
}
std::uint8_t mask = std::uint8_t{1} << static_cast<std::uint8_t>(bit);
if (set) {
value = static_cast<std::byte>(static_cast<std::uint8_t>(value) | mask);
} else {
value = static_cast<std::byte>(static_cast<std::uint8_t>(value) & ~mask);
}
}
} // namespace monoformat } // namespace monoformat
#endif // MONOFORMAT_BITHELPERS_HPP #endif // MONOFORMAT_BITHELPERS_HPP

@ -464,10 +464,12 @@ std::expected<void, ParseError> handleHScrollImage(std::span<std::byte const>& b
return {}; return {};
} }
bool restarting = !isBitSet(*flags, 0); ScrollFlags scrollFlags{*flags};
bool invert = isBitSet(*flags, 1);
bool padBefore = isBitSet(*flags, 2); bool restarting = !scrollFlags.endless;
bool padAfter = isBitSet(*flags, 3); bool invert = scrollFlags.invertDirection;
bool padBefore = scrollFlags.padBefore;
bool padAfter = scrollFlags.padAfter;
if (!padBefore && !padAfter && *contentWidth < *width) { if (!padBefore && !padAfter && *contentWidth < *width) {
std::int16_t offset = invert ? (*width - *contentWidth) : 0; std::int16_t offset = invert ? (*width - *contentWidth) : 0;
@ -627,10 +629,12 @@ std::expected<void, ParseError> handleVScrollImage(std::span<std::byte const>& b
return {}; return {};
} }
bool restarting = !isBitSet(*flags, 0); ScrollFlags scrollFlags{*flags};
bool invert = isBitSet(*flags, 1);
bool padBefore = isBitSet(*flags, 2); bool restarting = !scrollFlags.endless;
bool padAfter = isBitSet(*flags, 3); bool invert = scrollFlags.invertDirection;
bool padBefore = scrollFlags.padBefore;
bool padAfter = scrollFlags.padAfter;
if (!padBefore && !padAfter && *contentHeight < *height) { if (!padBefore && !padAfter && *contentHeight < *height) {
std::int16_t offset = invert ? (*height - *contentHeight) : 0; std::int16_t offset = invert ? (*height - *contentHeight) : 0;
@ -768,6 +772,9 @@ std::expected<void, ParseError> handleLine(std::span<std::byte const>& buffer, O
<< std::endl; << std::endl;
#endif #endif
LineFlags lineFlags{*flags};
std::ignore = lineFlags;
std::int32_t dx = static_cast<std::int32_t>(*targetX) - static_cast<std::int32_t>(*originX); std::int32_t dx = static_cast<std::int32_t>(*targetX) - static_cast<std::int32_t>(*originX);
std::int32_t dy = static_cast<std::int32_t>(*targetY) - static_cast<std::int32_t>(*originY); std::int32_t dy = static_cast<std::int32_t>(*targetY) - static_cast<std::int32_t>(*originY);
bool value = true; bool value = true;
@ -944,10 +951,12 @@ std::expected<void, ParseError> handleHScrollText(std::span<std::byte const>& bu
return {}; return {};
} }
bool restarting = !isBitSet(*flags, 0); ScrollFlags scrollFlags{*flags};
bool invert = isBitSet(*flags, 1);
bool padBefore = isBitSet(*flags, 2); bool restarting = !scrollFlags.endless;
bool padAfter = isBitSet(*flags, 3); bool invert = scrollFlags.invertDirection;
bool padBefore = scrollFlags.padBefore;
bool padAfter = scrollFlags.padAfter;
if (!padBefore && !padAfter && contentWidth < *width) { if (!padBefore && !padAfter && contentWidth < *width) {
std::uint16_t offset = invert ? (*width - contentWidth) : 0; std::uint16_t offset = invert ? (*width - contentWidth) : 0;
@ -1081,10 +1090,12 @@ std::expected<void, ParseError> handleCurrentTime(std::span<std::byte const>& bu
} }
ClippedImage target{screen, *x, *y, *width, *height}; ClippedImage target{screen, *x, *y, *width, *height};
bool use12h = isBitSet(*flags, 0); TimeDisplayFlags timeDisplayFlags{*flags};
bool showHours = isBitSet(*flags, 1);
bool showMinutes = isBitSet(*flags, 2); bool use12h = timeDisplayFlags.use12h;
bool showSeconds = isBitSet(*flags, 3); bool showHours = timeDisplayFlags.showHours;
bool showMinutes = timeDisplayFlags.showMinutes;
bool showSeconds = timeDisplayFlags.showSeconds;
if (showHours && showSeconds) { if (showHours && showSeconds) {
showMinutes = true; showMinutes = true;
} }

@ -37,11 +37,7 @@ void MemoryOneBitBuffer::setPixel(std::uint16_t x, std::uint16_t y, bool value)
std::size_t pxIndex = static_cast<std::size_t>(y) * m_width + x; std::size_t pxIndex = static_cast<std::size_t>(y) * m_width + x;
std::size_t byteIndex = pxIndex / 8; std::size_t byteIndex = pxIndex / 8;
std::size_t bitIndex = pxIndex % 8; std::size_t bitIndex = pxIndex % 8;
if (value) { maybeSetBit(m_buffer[byteIndex], bitIndex, value);
setBit(m_buffer[byteIndex], bitIndex);
} else {
unsetBit(m_buffer[byteIndex], bitIndex);
}
} }
ConstMemoryOneBitBuffer::ConstMemoryOneBitBuffer(std::span<std::byte const> buffer, std::uint16_t width, std::uint16_t height) ConstMemoryOneBitBuffer::ConstMemoryOneBitBuffer(std::span<std::byte const> buffer, std::uint16_t width, std::uint16_t height)

@ -1,6 +1,8 @@
#ifndef MONOFORMAT_SCHEMA_HPP #ifndef MONOFORMAT_SCHEMA_HPP
#define MONOFORMAT_SCHEMA_HPP #define MONOFORMAT_SCHEMA_HPP
#include "monoformat_bithelpers.hpp"
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <span> #include <span>
@ -29,6 +31,71 @@ enum class LineStyle : std::uint8_t {
Solid = 0, Solid = 0,
}; };
struct ScrollFlags {
bool endless{};
bool invertDirection{};
bool padBefore{};
bool padAfter{};
ScrollFlags() = default;
explicit ScrollFlags(std::uint8_t flags) noexcept
: endless{isBitSet(flags, 0)}
, invertDirection{isBitSet(flags, 1)}
, padBefore{isBitSet(flags, 2)}
, padAfter{isBitSet(flags, 3)}
{
}
explicit operator std::uint8_t() const noexcept {
std::uint8_t result{};
maybeSetBit(result, 0, endless);
maybeSetBit(result, 1, invertDirection);
maybeSetBit(result, 2, padBefore);
maybeSetBit(result, 3, padAfter);
return result;
}
};
struct LineFlags {
LineFlags() = default;
explicit LineFlags(std::uint8_t flags) noexcept
{
}
explicit operator std::uint8_t() const noexcept {
std::uint8_t result{};
return result;
}
};
struct TimeDisplayFlags {
bool use12h{};
bool showHours{};
bool showMinutes{};
bool showSeconds{};
TimeDisplayFlags() = default;
explicit TimeDisplayFlags(std::uint16_t flags) noexcept
: use12h{isBitSet(flags, 0)}
, showHours{isBitSet(flags, 1)}
, showMinutes{isBitSet(flags, 2)}
, showSeconds{isBitSet(flags, 3)}
{
}
explicit operator std::uint16_t() const noexcept {
std::uint16_t result{};
maybeSetBit(result, 0, use12h);
maybeSetBit(result, 1, showHours);
maybeSetBit(result, 2, showMinutes);
maybeSetBit(result, 3, showSeconds);
return result;
}
};
struct OneBitBufferInterface { struct OneBitBufferInterface {
using Color = bool; using Color = bool;

@ -1,5 +1,6 @@
#include "monoformat_structured.hpp" #include "monoformat_structured.hpp"
#include "monoformat_fontreader.hpp" #include "monoformat_fontreader.hpp"
#include "monoformat_bithelpers.hpp"
#include <iostream> #include <iostream>
@ -292,7 +293,7 @@ std::expected<std::unique_ptr<AnimationElement>, ParseError> AnimationElement::p
return result; return result;
} }
HScrollImageElement::HScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentWidth, std::uint8_t flags, std::uint8_t scrollSpeed) HScrollImageElement::HScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentWidth, ScrollFlags flags, std::uint8_t scrollSpeed)
: m_x{x} : m_x{x}
, m_y{y} , m_y{y}
, m_width{width} , m_width{width}
@ -325,7 +326,7 @@ std::uint16_t HScrollImageElement::contentWidth() const noexcept {
return m_contentWidth; return m_contentWidth;
} }
std::uint8_t HScrollImageElement::flags() const noexcept { ScrollFlags HScrollImageElement::flags() const noexcept {
return m_flags; return m_flags;
} }
@ -370,7 +371,7 @@ std::size_t HScrollImageElement::serializeTo(std::span<std::byte> target) const
pos = writeU16LE(target, pos, m_width); pos = writeU16LE(target, pos, m_width);
pos = writeU16LE(target, pos, m_height); pos = writeU16LE(target, pos, m_height);
pos = writeU16LE(target, pos, m_contentWidth); pos = writeU16LE(target, pos, m_contentWidth);
pos = writeU8LE(target, pos, m_flags); pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_flags));
pos = writeU8LE(target, pos, m_scrollSpeed); pos = writeU8LE(target, pos, m_scrollSpeed);
pos = writeU16LE(target, pos, 0); pos = writeU16LE(target, pos, 0);
pos = writeBuffer(target, pos, m_buffer); pos = writeBuffer(target, pos, m_buffer);
@ -388,10 +389,10 @@ void HScrollImageElement::drawTo(OneBitBufferInterface* imageBuffer, std::size_t
return; return;
} }
bool restarting = (m_flags & 0x01) == 0x00; bool restarting = !m_flags.endless;
bool invert = (m_flags & 0x02) == 0x02; bool invert = m_flags.invertDirection;
bool padBefore = (m_flags & 0x04) == 0x04; bool padBefore = m_flags.padBefore;
bool padAfter = (m_flags & 0x08) == 0x08; bool padAfter = m_flags.padAfter;
if (!padBefore && !padAfter && m_contentWidth < m_width) { if (!padBefore && !padAfter && m_contentWidth < m_width) {
std::int16_t offset = invert ? (m_width - m_contentWidth) : 0; std::int16_t offset = invert ? (m_width - m_contentWidth) : 0;
@ -531,12 +532,12 @@ std::expected<std::unique_ptr<HScrollImageElement>, ParseError> HScrollImageElem
buffer = buffer.subspan(rounded - imageSize); buffer = buffer.subspan(rounded - imageSize);
} }
auto result = std::make_unique<HScrollImageElement>(*x, *y, *width, *height, *contentWidth, *flags, *scrollSpeed); auto result = std::make_unique<HScrollImageElement>(*x, *y, *width, *height, *contentWidth, ScrollFlags{*flags}, *scrollSpeed);
result->updateBuffer(*imageData); result->updateBuffer(*imageData);
return result; return result;
} }
VScrollImageElement::VScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentHeight, std::uint8_t flags, std::uint8_t scrollSpeed) VScrollImageElement::VScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentHeight, ScrollFlags flags, std::uint8_t scrollSpeed)
: m_x{x} : m_x{x}
, m_y{y} , m_y{y}
, m_width{width} , m_width{width}
@ -569,7 +570,7 @@ std::uint16_t VScrollImageElement::contentHeight() const noexcept {
return m_contentHeight; return m_contentHeight;
} }
std::uint8_t VScrollImageElement::flags() const noexcept { ScrollFlags VScrollImageElement::flags() const noexcept {
return m_flags; return m_flags;
} }
@ -614,7 +615,7 @@ std::size_t VScrollImageElement::serializeTo(std::span<std::byte> target) const
pos = writeU16LE(target, pos, m_width); pos = writeU16LE(target, pos, m_width);
pos = writeU16LE(target, pos, m_height); pos = writeU16LE(target, pos, m_height);
pos = writeU16LE(target, pos, m_contentHeight); pos = writeU16LE(target, pos, m_contentHeight);
pos = writeU8LE(target, pos, m_flags); pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_flags));
pos = writeU8LE(target, pos, m_scrollSpeed); pos = writeU8LE(target, pos, m_scrollSpeed);
pos = writeU16LE(target, pos, 0); pos = writeU16LE(target, pos, 0);
pos = writeBuffer(target, pos, m_buffer); pos = writeBuffer(target, pos, m_buffer);
@ -632,10 +633,10 @@ void VScrollImageElement::drawTo(OneBitBufferInterface* imageBuffer, std::size_t
return; return;
} }
bool restarting = (m_flags & 0x01) == 0x00; bool restarting = !m_flags.endless;
bool invert = (m_flags & 0x02) == 0x02; bool invert = m_flags.invertDirection;
bool padBefore = (m_flags & 0x04) == 0x04; bool padBefore = m_flags.padBefore;
bool padAfter = (m_flags & 0x08) == 0x08; bool padAfter = m_flags.padAfter;
if (!padBefore && !padAfter && m_contentHeight < m_height) { if (!padBefore && !padAfter && m_contentHeight < m_height) {
std::int16_t offset = invert ? (m_height - m_contentHeight) : 0; std::int16_t offset = invert ? (m_height - m_contentHeight) : 0;
@ -775,13 +776,13 @@ std::expected<std::unique_ptr<VScrollImageElement>, ParseError> VScrollImageElem
buffer = buffer.subspan(rounded - imageSize); buffer = buffer.subspan(rounded - imageSize);
} }
auto result = std::make_unique<VScrollImageElement>(*x, *y, *width, *height, *contentHeight, *flags, *scrollSpeed); auto result = std::make_unique<VScrollImageElement>(*x, *y, *width, *height, *contentHeight, ScrollFlags{*flags}, *scrollSpeed);
result->updateBuffer(*imageData); result->updateBuffer(*imageData);
return result; return result;
} }
LineElement::LineElement(std::uint16_t originX, std::uint16_t originY, std::uint16_t targetX, std::uint16_t targetY, LineStyle lineStyle, std::uint8_t flags) LineElement::LineElement(std::uint16_t originX, std::uint16_t originY, std::uint16_t targetX, std::uint16_t targetY, LineStyle lineStyle, LineFlags flags)
: m_originX{originX} : m_originX{originX}
, m_originY{originY} , m_originY{originY}
, m_targetX{targetX} , m_targetX{targetX}
@ -811,7 +812,7 @@ LineStyle LineElement::lineStyle() const noexcept {
return m_lineStyle; return m_lineStyle;
} }
std::uint8_t LineElement::flags() const noexcept { LineFlags LineElement::flags() const noexcept {
return m_flags; return m_flags;
} }
@ -830,7 +831,7 @@ std::size_t LineElement::serializeTo(std::span<std::byte> target) const {
pos = writeU16LE(target, pos, m_targetX); pos = writeU16LE(target, pos, m_targetX);
pos = writeU16LE(target, pos, m_targetY); pos = writeU16LE(target, pos, m_targetY);
pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_lineStyle)); pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_lineStyle));
pos = writeU8LE(target, pos, m_flags); pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_flags));
return alignNextWrite(target, pos, 4); return alignNextWrite(target, pos, 4);
} }
@ -895,7 +896,7 @@ std::expected<std::unique_ptr<LineElement>, ParseError> LineElement::parse(std::
if (!flags) { if (!flags) {
return std::unexpected(flags.error()); return std::unexpected(flags.error());
} }
auto result = std::make_unique<LineElement>(*originX, *originY, *targetX, *targetY, static_cast<LineStyle>(*lineStyle), *flags); auto result = std::make_unique<LineElement>(*originX, *originY, *targetX, *targetY, static_cast<LineStyle>(*lineStyle), LineFlags{*flags});
return result; return result;
} }
@ -1019,7 +1020,7 @@ std::expected<std::unique_ptr<ClippedTextElement>, ParseError> ClippedTextElemen
return result; return result;
} }
HScrollTextElement::HScrollTextElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint8_t flags, std::uint8_t scrollSpeed, std::uint16_t fontIndex, std::string text) HScrollTextElement::HScrollTextElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, ScrollFlags flags, std::uint8_t scrollSpeed, std::uint16_t fontIndex, std::string text)
: m_x{x} : m_x{x}
, m_y{y} , m_y{y}
, m_width{width} , m_width{width}
@ -1047,7 +1048,7 @@ std::uint16_t HScrollTextElement::height() const noexcept {
return m_height; return m_height;
} }
std::uint8_t HScrollTextElement::flags() const noexcept { ScrollFlags HScrollTextElement::flags() const noexcept {
return m_flags; return m_flags;
} }
@ -1077,7 +1078,7 @@ std::size_t HScrollTextElement::serializeTo(std::span<std::byte> target) const {
pos = writeU16LE(target, pos, m_y); pos = writeU16LE(target, pos, m_y);
pos = writeU16LE(target, pos, m_width); pos = writeU16LE(target, pos, m_width);
pos = writeU16LE(target, pos, m_height); pos = writeU16LE(target, pos, m_height);
pos = writeU8LE(target, pos, m_flags); pos = writeU8LE(target, pos, static_cast<std::uint8_t>(m_flags));
pos = writeU8LE(target, pos, m_scrollSpeed); pos = writeU8LE(target, pos, m_scrollSpeed);
pos = writeU16LE(target, pos, m_fontIndex); pos = writeU16LE(target, pos, m_fontIndex);
pos = writeU16LE(target, pos, m_text.size()); pos = writeU16LE(target, pos, m_text.size());
@ -1116,10 +1117,10 @@ void HScrollTextElement::drawTo(OneBitBufferInterface* imageBuffer, std::size_t
return; return;
} }
bool restarting = (m_flags & 0x01) == 0x00; bool restarting = !m_flags.endless;
bool invert = (m_flags & 0x02) == 0x02; bool invert = m_flags.invertDirection;
bool padBefore = (m_flags & 0x04) == 0x04; bool padBefore = m_flags.padBefore;
bool padAfter = (m_flags & 0x08) == 0x08; bool padAfter = m_flags.padAfter;
if (!padBefore && !padAfter && contentWidth < m_width) { if (!padBefore && !padAfter && contentWidth < m_width) {
std::uint16_t offset = invert ? (m_width - contentWidth) : 0; std::uint16_t offset = invert ? (m_width - contentWidth) : 0;
@ -1235,11 +1236,11 @@ std::expected<std::unique_ptr<HScrollTextElement>, ParseError> HScrollTextElemen
return std::unexpected(textData.error()); return std::unexpected(textData.error());
} }
std::string_view text{reinterpret_cast<char const*>(textData->data()), *textLength}; std::string_view text{reinterpret_cast<char const*>(textData->data()), *textLength};
auto result = std::make_unique<HScrollTextElement>(*x, *y, *width, *height, *flags, *scrollSpeed, *fontIndex, std::string{text}); auto result = std::make_unique<HScrollTextElement>(*x, *y, *width, *height, ScrollFlags{*flags}, *scrollSpeed, *fontIndex, std::string{text});
return result; return result;
} }
CurrentTimeElement::CurrentTimeElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t fontIndex, std::uint16_t utcOffset, std::uint16_t flags) CurrentTimeElement::CurrentTimeElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t fontIndex, std::uint16_t utcOffset, TimeDisplayFlags flags)
: m_x{x} : m_x{x}
, m_y{y} , m_y{y}
, m_width{width} , m_width{width}
@ -1274,7 +1275,7 @@ std::uint16_t CurrentTimeElement::utcOffset() const noexcept {
return m_utcOffset; return m_utcOffset;
} }
std::uint16_t CurrentTimeElement::flags() const noexcept { TimeDisplayFlags CurrentTimeElement::flags() const noexcept {
return m_flags; return m_flags;
} }
@ -1294,7 +1295,7 @@ std::size_t CurrentTimeElement::serializeTo(std::span<std::byte> target) const {
pos = writeU16LE(target, pos, m_height); pos = writeU16LE(target, pos, m_height);
pos = writeU16LE(target, pos, m_fontIndex); pos = writeU16LE(target, pos, m_fontIndex);
pos = writeU16LE(target, pos, m_utcOffset); pos = writeU16LE(target, pos, m_utcOffset);
pos = writeU16LE(target, pos, m_flags); pos = writeU16LE(target, pos, static_cast<std::uint16_t>(m_flags));
return alignNextWrite(target, pos, 4); return alignNextWrite(target, pos, 4);
} }
@ -1315,10 +1316,10 @@ void CurrentTimeElement::drawTo(OneBitBufferInterface* imageBuffer, std::size_t
return; return;
} }
bool use12h = (m_flags & 0x01u) == 0x01u; bool use12h = m_flags.use12h;
bool showHours = (m_flags & 0x02u) == 0x02u; bool showHours = m_flags.showHours;
bool showMinutes = (m_flags & 0x04u) == 0x04u; bool showMinutes = m_flags.showMinutes;
bool showSeconds = (m_flags & 0x08u) == 0x08u; bool showSeconds = m_flags.showSeconds;
if (showHours && showSeconds) { if (showHours && showSeconds) {
showMinutes = true; showMinutes = true;
} }
@ -1391,7 +1392,7 @@ std::expected<std::unique_ptr<CurrentTimeElement>, ParseError> CurrentTimeElemen
if (!flags) { if (!flags) {
return std::unexpected(flags.error()); return std::unexpected(flags.error());
} }
auto result = std::make_unique<CurrentTimeElement>(*x, *y, *width, *height, *fontIndex, *utcOffset, *flags); auto result = std::make_unique<CurrentTimeElement>(*x, *y, *width, *height, *fontIndex, *utcOffset, TimeDisplayFlags{*flags});
return result; return result;
} }
@ -1474,15 +1475,9 @@ std::size_t AlwaysDrawnSection::serializeTo(std::span<std::byte> target) const {
// Will be replaced later // Will be replaced later
pos = writeU24LE(target, pos, 0); pos = writeU24LE(target, pos, 0);
std::uint16_t flags = 0; std::uint16_t flags = 0;
if (m_drawOnFront) { maybeSetBit(flags, 0, m_drawOnFront);
flags |= std::uint16_t{1}; maybeSetBit(flags, 1, m_drawOnBack);
} maybeSetBit(flags, 2, m_clearBeforeDrawing);
if (m_drawOnBack) {
flags |= std::uint16_t{2};
}
if (m_clearBeforeDrawing) {
flags |= std::uint16_t{4};
}
pos = writeU16LE(target, pos, flags); pos = writeU16LE(target, pos, flags);
pos = writeU16LE(target, pos, m_elements.size()); pos = writeU16LE(target, pos, m_elements.size());
for (auto const& element : m_elements) { for (auto const& element : m_elements) {
@ -1523,9 +1518,9 @@ std::expected<std::unique_ptr<AlwaysDrawnSection>, ParseError> AlwaysDrawnSectio
auto sectionData = buffer.subspan(0, *size - 8); auto sectionData = buffer.subspan(0, *size - 8);
buffer = buffer.subspan(*size - 8); buffer = buffer.subspan(*size - 8);
auto section = std::make_unique<AlwaysDrawnSection>(); auto section = std::make_unique<AlwaysDrawnSection>();
section->setDrawOnFront((((*flags) >> 0u) & 0x01u) == 0x01u); section->setDrawOnFront(isBitSet(*flags, 0));
section->setDrawOnBack((((*flags) >> 1u) & 0x01u) == 0x01u); section->setDrawOnBack(isBitSet(*flags, 1));
section->setClearBeforeDrawing((((*flags) >> 2u) & 0x01u) == 0x01u); section->setClearBeforeDrawing(isBitSet(*flags, 2));
section->m_elements.reserve(*elementCount); section->m_elements.reserve(*elementCount);
for (std::size_t i = 0; i < *elementCount; ++i) { for (std::size_t i = 0; i < *elementCount; ++i) {
auto type = peekU16LE(sectionData); auto type = peekU16LE(sectionData);
@ -1648,15 +1643,9 @@ std::size_t TimeBasedDrawnSection::serializeTo(std::span<std::byte> target) cons
// Will be replaced later // Will be replaced later
pos = writeU24LE(target, pos, 0); pos = writeU24LE(target, pos, 0);
std::uint16_t flags = 0; std::uint16_t flags = 0;
if (m_drawOnFront) { maybeSetBit(flags, 0, m_drawOnFront);
flags |= std::uint16_t{1}; maybeSetBit(flags, 1, m_drawOnBack);
} maybeSetBit(flags, 2, m_clearBeforeDrawing);
if (m_drawOnBack) {
flags |= std::uint16_t{2};
}
if (m_clearBeforeDrawing) {
flags |= std::uint16_t{4};
}
pos = writeU16LE(target, pos, flags); pos = writeU16LE(target, pos, flags);
pos = writeU16LE(target, pos, m_elements.size()); pos = writeU16LE(target, pos, m_elements.size());
pos = writeU64LE(target, pos, std::bit_cast<std::uint64_t>(m_startTimestamp)); pos = writeU64LE(target, pos, std::bit_cast<std::uint64_t>(m_startTimestamp));
@ -1707,9 +1696,9 @@ std::expected<std::unique_ptr<TimeBasedDrawnSection>, ParseError> TimeBasedDrawn
auto sectionData = buffer.subspan(0, *size - 24); auto sectionData = buffer.subspan(0, *size - 24);
buffer = buffer.subspan(*size - 24); buffer = buffer.subspan(*size - 24);
auto section = std::make_unique<TimeBasedDrawnSection>(); auto section = std::make_unique<TimeBasedDrawnSection>();
section->setDrawOnFront((((*flags) >> 0u) & 0x01u) == 0x01u); section->setDrawOnFront(isBitSet(*flags, 0));
section->setDrawOnBack((((*flags) >> 1u) & 0x01u) == 0x01u); section->setDrawOnBack(isBitSet(*flags, 1));
section->setClearBeforeDrawing((((*flags) >> 2u) & 0x01u) == 0x01u); section->setClearBeforeDrawing(isBitSet(*flags, 2));
section->setStartTimestamp(std::bit_cast<std::int64_t>(*startTimestamp)); section->setStartTimestamp(std::bit_cast<std::int64_t>(*startTimestamp));
section->setEndTimestamp(std::bit_cast<std::int64_t>(*endTimestamp)); section->setEndTimestamp(std::bit_cast<std::int64_t>(*endTimestamp));
section->m_elements.reserve(*elementCount); section->m_elements.reserve(*elementCount);

@ -96,14 +96,14 @@ private:
}; };
struct HScrollImageElement : public Element { struct HScrollImageElement : public Element {
HScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentWidth, std::uint8_t flags, std::uint8_t scrollSpeed); HScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentWidth, ScrollFlags flags, std::uint8_t scrollSpeed);
std::uint16_t x() const noexcept; std::uint16_t x() const noexcept;
std::uint16_t y() const noexcept; std::uint16_t y() const noexcept;
std::uint16_t width() const noexcept; std::uint16_t width() const noexcept;
std::uint16_t height() const noexcept; std::uint16_t height() const noexcept;
std::uint16_t contentWidth() const noexcept; std::uint16_t contentWidth() const noexcept;
std::uint8_t flags() const noexcept; ScrollFlags flags() const noexcept;
std::uint8_t scrollSpeed() const noexcept; std::uint8_t scrollSpeed() const noexcept;
std::span<std::byte> buffer() noexcept; std::span<std::byte> buffer() noexcept;
std::span<std::byte const> buffer() const noexcept; std::span<std::byte const> buffer() const noexcept;
@ -124,20 +124,20 @@ private:
std::uint16_t m_width{}; std::uint16_t m_width{};
std::uint16_t m_height{}; std::uint16_t m_height{};
std::uint16_t m_contentWidth{}; std::uint16_t m_contentWidth{};
std::uint8_t m_flags{}; ScrollFlags m_flags;
std::uint8_t m_scrollSpeed{}; std::uint8_t m_scrollSpeed{};
std::vector<std::byte> m_buffer; std::vector<std::byte> m_buffer;
}; };
struct VScrollImageElement : public Element { struct VScrollImageElement : public Element {
VScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentHeight, std::uint8_t flags, std::uint8_t scrollSpeed); VScrollImageElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t contentHeight, ScrollFlags flags, std::uint8_t scrollSpeed);
std::uint16_t x() const noexcept; std::uint16_t x() const noexcept;
std::uint16_t y() const noexcept; std::uint16_t y() const noexcept;
std::uint16_t width() const noexcept; std::uint16_t width() const noexcept;
std::uint16_t height() const noexcept; std::uint16_t height() const noexcept;
std::uint16_t contentHeight() const noexcept; std::uint16_t contentHeight() const noexcept;
std::uint8_t flags() const noexcept; ScrollFlags flags() const noexcept;
std::uint8_t scrollSpeed() const noexcept; std::uint8_t scrollSpeed() const noexcept;
std::span<std::byte> buffer() noexcept; std::span<std::byte> buffer() noexcept;
std::span<std::byte const> buffer() const noexcept; std::span<std::byte const> buffer() const noexcept;
@ -158,20 +158,20 @@ private:
std::uint16_t m_width{}; std::uint16_t m_width{};
std::uint16_t m_height{}; std::uint16_t m_height{};
std::uint16_t m_contentHeight{}; std::uint16_t m_contentHeight{};
std::uint8_t m_flags{}; ScrollFlags m_flags;
std::uint8_t m_scrollSpeed{}; std::uint8_t m_scrollSpeed{};
std::vector<std::byte> m_buffer; std::vector<std::byte> m_buffer;
}; };
struct LineElement : public Element { struct LineElement : public Element {
LineElement(std::uint16_t originX, std::uint16_t originY, std::uint16_t targetX, std::uint16_t targetY, LineStyle lineStyle, std::uint8_t flags); LineElement(std::uint16_t originX, std::uint16_t originY, std::uint16_t targetX, std::uint16_t targetY, LineStyle lineStyle, LineFlags flags);
std::uint16_t originX() const noexcept; std::uint16_t originX() const noexcept;
std::uint16_t originY() const noexcept; std::uint16_t originY() const noexcept;
std::uint16_t targetX() const noexcept; std::uint16_t targetX() const noexcept;
std::uint16_t targetY() const noexcept; std::uint16_t targetY() const noexcept;
LineStyle lineStyle() const noexcept; LineStyle lineStyle() const noexcept;
std::uint8_t flags() const noexcept; LineFlags flags() const noexcept;
virtual ~LineElement() override; virtual ~LineElement() override;
virtual ElementType elementType() const override; virtual ElementType elementType() const override;
@ -186,7 +186,7 @@ private:
std::uint16_t m_targetX{}; std::uint16_t m_targetX{};
std::uint16_t m_targetY{}; std::uint16_t m_targetY{};
LineStyle m_lineStyle{}; LineStyle m_lineStyle{};
std::uint8_t m_flags{}; LineFlags m_flags;
}; };
struct ClippedTextElement : public Element { struct ClippedTextElement : public Element {
@ -216,13 +216,13 @@ private:
}; };
struct HScrollTextElement : public Element { struct HScrollTextElement : public Element {
HScrollTextElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint8_t flags, std::uint8_t scrollSpeed, std::uint16_t fontIndex, std::string text); HScrollTextElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, ScrollFlags flags, std::uint8_t scrollSpeed, std::uint16_t fontIndex, std::string text);
std::uint16_t x() const noexcept; std::uint16_t x() const noexcept;
std::uint16_t y() const noexcept; std::uint16_t y() const noexcept;
std::uint16_t width() const noexcept; std::uint16_t width() const noexcept;
std::uint16_t height() const noexcept; std::uint16_t height() const noexcept;
std::uint8_t flags() const noexcept; ScrollFlags flags() const noexcept;
std::uint8_t scrollSpeed() const noexcept; std::uint8_t scrollSpeed() const noexcept;
std::uint16_t fontIndex() const noexcept; std::uint16_t fontIndex() const noexcept;
std::string text() const; std::string text() const;
@ -239,14 +239,14 @@ private:
std::uint16_t m_y{}; std::uint16_t m_y{};
std::uint16_t m_width{}; std::uint16_t m_width{};
std::uint16_t m_height{}; std::uint16_t m_height{};
std::uint8_t m_flags{}; ScrollFlags m_flags;
std::uint8_t m_scrollSpeed{}; std::uint8_t m_scrollSpeed{};
std::uint16_t m_fontIndex{}; std::uint16_t m_fontIndex{};
std::string m_text; std::string m_text;
}; };
struct CurrentTimeElement : public Element { struct CurrentTimeElement : public Element {
CurrentTimeElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t fontIndex, std::uint16_t utcOffset, std::uint16_t flags); CurrentTimeElement(std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height, std::uint16_t fontIndex, std::uint16_t utcOffset, TimeDisplayFlags flags);
std::uint16_t x() const noexcept; std::uint16_t x() const noexcept;
std::uint16_t y() const noexcept; std::uint16_t y() const noexcept;
@ -254,7 +254,7 @@ struct CurrentTimeElement : public Element {
std::uint16_t height() const noexcept; std::uint16_t height() const noexcept;
std::uint16_t fontIndex() const noexcept; std::uint16_t fontIndex() const noexcept;
std::uint16_t utcOffset() const noexcept; std::uint16_t utcOffset() const noexcept;
std::uint16_t flags() const noexcept; TimeDisplayFlags flags() const noexcept;
virtual ~CurrentTimeElement() override; virtual ~CurrentTimeElement() override;
virtual ElementType elementType() const override; virtual ElementType elementType() const override;
@ -270,7 +270,7 @@ private:
std::uint16_t m_height{}; std::uint16_t m_height{};
std::uint16_t m_fontIndex{}; std::uint16_t m_fontIndex{};
std::uint16_t m_utcOffset{}; std::uint16_t m_utcOffset{};
std::uint16_t m_flags{}; TimeDisplayFlags m_flags;
}; };
struct AlwaysDrawnSection : public Section { struct AlwaysDrawnSection : public Section {

Loading…
Cancel
Save