diff --git a/deps/ox/src/ox/mc/presenceindicator.cpp b/deps/ox/src/ox/mc/presenceindicator.cpp index ba854a50..72a4e9a5 100644 --- a/deps/ox/src/ox/mc/presenceindicator.cpp +++ b/deps/ox/src/ox/mc/presenceindicator.cpp @@ -12,26 +12,18 @@ namespace ox { -FieldPresenceIndicator::FieldPresenceIndicator(const uint8_t *mask, std::size_t maxLen) { - // TODO: have separate read only version of this class - m_mask = const_cast(mask); - m_maskLen = maxLen; +template class FieldBitmapReader; +template class FieldBitmapReader; + +FieldBitmap::FieldBitmap(uint8_t *mask, std::size_t maxLen) noexcept: FieldBitmapReader(mask, maxLen) { } -Result FieldPresenceIndicator::get(std::size_t i) const { - if (i / 8 < m_maskLen) { - return (m_mask[i / 8] >> (i % 8)) & 1; - } else { - return OxError(MC_PRESENCEMASKOUTBOUNDS); - } -} - -Error FieldPresenceIndicator::set(std::size_t i, bool on) { - if (i / 8 < m_maskLen) { +Error FieldBitmap::set(std::size_t i, bool on) noexcept { + if (i / 8 < m_mapLen) { if (on) { - m_mask[i / 8] |= 1 << (i % 8); + m_map[i / 8] |= 1 << (i % 8); } else { - m_mask[i / 8] &= ~(1 << (i % 8)); + m_map[i / 8] &= ~(1 << (i % 8)); } return OxError(0); } else { @@ -39,17 +31,4 @@ Error FieldPresenceIndicator::set(std::size_t i, bool on) { } } -void FieldPresenceIndicator::setFields(int fields) noexcept { - m_fields = fields; - m_maskLen = (fields / 8 + 1) - (fields % 8 == 0); -} - - void FieldPresenceIndicator::setMaxLen(int maxLen) noexcept { - m_maskLen = maxLen; -} - -int FieldPresenceIndicator::getMaxLen() const noexcept { - return m_maskLen; -} - } diff --git a/deps/ox/src/ox/mc/presenceindicator.hpp b/deps/ox/src/ox/mc/presenceindicator.hpp index 6cff766b..928a7f32 100644 --- a/deps/ox/src/ox/mc/presenceindicator.hpp +++ b/deps/ox/src/ox/mc/presenceindicator.hpp @@ -13,25 +13,67 @@ namespace ox { -class FieldPresenceIndicator { - private: - uint8_t *m_mask = nullptr; - std::size_t m_maskLen = 0; +template +class FieldBitmapReader { + protected: + T m_map = nullptr; + std::size_t m_mapLen = 0; std::size_t m_fields = 0; public: - FieldPresenceIndicator(const uint8_t *mask, std::size_t maxLen); + FieldBitmapReader(T map, std::size_t maxLen) noexcept; - Result get(std::size_t i) const; - - Error set(std::size_t i, bool on); + Result get(std::size_t i) const noexcept; void setFields(int) noexcept; - void setMaxLen(int) noexcept; + void setMaxLen(int) noexcept; int getMaxLen() const noexcept; }; +template +FieldBitmapReader::FieldBitmapReader(T map, std::size_t maxLen) noexcept { + m_map = map; + m_mapLen = maxLen; +} + +template +Result FieldBitmapReader::get(std::size_t i) const noexcept { + if (i / 8 < m_mapLen) { + return (m_map[i / 8] >> (i % 8)) & 1; + } else { + return OxError(MC_PRESENCEMASKOUTBOUNDS); + } +} + +template +void FieldBitmapReader::setFields(int fields) noexcept { + m_fields = fields; + m_mapLen = (fields / 8 + 1) - (fields % 8 == 0); +} + +template +void FieldBitmapReader::setMaxLen(int maxLen) noexcept { + m_mapLen = maxLen; +} + +template +int FieldBitmapReader::getMaxLen() const noexcept { + return m_mapLen; +} + +extern template class FieldBitmapReader; +extern template class FieldBitmapReader; + +class FieldBitmap: public FieldBitmapReader { + + public: + FieldBitmap(uint8_t *mask, std::size_t maxLen) noexcept; + + Error set(std::size_t i, bool on) noexcept; + +}; + } diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 433d19c6..d4448aff 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -25,7 +25,7 @@ namespace ox { class MetalClawReader { private: - FieldPresenceIndicator m_fieldPresence; + FieldBitmapReader m_fieldPresence; int m_fields = 0; int m_field = 0; int m_unionIdx = -1; diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index 8a08971e..c10c44d5 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -28,7 +28,7 @@ namespace ox { class MetalClawWriter { private: - FieldPresenceIndicator m_fieldPresence; + FieldBitmap m_fieldPresence; int m_fields = 0; int m_field = 0; int m_unionIdx = -1;