[ox/mc] Cleanup field presence indicator code

This commit is contained in:
Gary Talent 2021-04-17 16:59:16 -05:00
parent 69fb4acd24
commit 9822a6ec47
4 changed files with 61 additions and 40 deletions

View File

@ -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<uint8_t*>(mask);
m_maskLen = maxLen;
template class FieldBitmapReader<uint8_t*>;
template class FieldBitmapReader<const uint8_t*>;
FieldBitmap::FieldBitmap(uint8_t *mask, std::size_t maxLen) noexcept: FieldBitmapReader<uint8_t*>(mask, maxLen) {
}
Result<bool> 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;
}
}

View File

@ -13,25 +13,67 @@
namespace ox {
class FieldPresenceIndicator {
private:
uint8_t *m_mask = nullptr;
std::size_t m_maskLen = 0;
template<typename T>
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<bool> get(std::size_t i) const;
Error set(std::size_t i, bool on);
Result<bool> get(std::size_t i) const noexcept;
void setFields(int) noexcept;
void setMaxLen(int) noexcept;
void setMaxLen(int) noexcept;
int getMaxLen() const noexcept;
};
template<typename T>
FieldBitmapReader<T>::FieldBitmapReader(T map, std::size_t maxLen) noexcept {
m_map = map;
m_mapLen = maxLen;
}
template<typename T>
Result<bool> FieldBitmapReader<T>::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<typename T>
void FieldBitmapReader<T>::setFields(int fields) noexcept {
m_fields = fields;
m_mapLen = (fields / 8 + 1) - (fields % 8 == 0);
}
template<typename T>
void FieldBitmapReader<T>::setMaxLen(int maxLen) noexcept {
m_mapLen = maxLen;
}
template<typename T>
int FieldBitmapReader<T>::getMaxLen() const noexcept {
return m_mapLen;
}
extern template class FieldBitmapReader<uint8_t*>;
extern template class FieldBitmapReader<const uint8_t*>;
class FieldBitmap: public FieldBitmapReader<uint8_t*> {
public:
FieldBitmap(uint8_t *mask, std::size_t maxLen) noexcept;
Error set(std::size_t i, bool on) noexcept;
};
}

View File

@ -25,7 +25,7 @@ namespace ox {
class MetalClawReader {
private:
FieldPresenceIndicator m_fieldPresence;
FieldBitmapReader<const uint8_t*> m_fieldPresence;
int m_fields = 0;
int m_field = 0;
int m_unionIdx = -1;

View File

@ -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;