feat: add alignment
This commit is contained in:
@@ -104,6 +104,7 @@ class MessageImplFieldsBase : public TBase {
|
||||
AllFields& GetFields() {
|
||||
return m_Fields;
|
||||
}
|
||||
|
||||
const AllFields& GetFields() const {
|
||||
return m_Fields;
|
||||
}
|
||||
@@ -119,23 +120,75 @@ class MessageImplFieldsBase : public TBase {
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsReadBase : public TBase {
|
||||
protected:
|
||||
private:
|
||||
// normal reading
|
||||
template <typename TField>
|
||||
void ReadField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->ReadData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// reading field in bitfield
|
||||
template <typename TFieldType, typename TField, int IAlignment>
|
||||
void ReadField(Field<TField, IAlignment>& field, TFieldType data, std::size_t offset) {
|
||||
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
||||
// we suppose that the first element is at the highest bits
|
||||
field.GetValue() = (data >> TotalBitCount - IAlignment - offset) & ((1 << IAlignment) - 1);
|
||||
}
|
||||
|
||||
// reading bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void ReadField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data;
|
||||
this->ReadData(data, buffer);
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[data, this, &offset](auto& field) {
|
||||
this->ReadField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
}
|
||||
|
||||
void ReadImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
std::apply([&buffer, this](auto& field) {
|
||||
this->ReadData(field.GetValue(), buffer);
|
||||
}, allFields);
|
||||
TupleForEach([&buffer, this](auto& field) { this->ReadField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsWriteBase : public TBase {
|
||||
protected:
|
||||
private:
|
||||
// normal writing
|
||||
template <typename TField>
|
||||
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->WriteData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// writing field in bitfield
|
||||
template <typename TFieldType, typename TField, int IAlignment>
|
||||
void WriteField(Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) {
|
||||
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
||||
// we suppose that the first element is at the highest bits
|
||||
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
||||
}
|
||||
|
||||
// writing bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data = 0;
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[&data, this, &offset](auto& field) {
|
||||
this->WriteField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
this->WriteData(data, buffer);
|
||||
}
|
||||
|
||||
void WriteImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
std::apply([&buffer, this](auto& field) {
|
||||
this->WriteData(field.GetValue(), buffer);
|
||||
}, allFields);
|
||||
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user