feat: add alignment

This commit is contained in:
2025-02-08 19:45:45 +01:00
parent 530232ba16
commit 462086b13c
2 changed files with 122 additions and 10 deletions

View File

@@ -5,13 +5,68 @@
namespace sp {
template <typename ValueType>
/**
* \brief Example usage :
* sp::BitField<std::uint16_t, sp::Field<std::uint16_t, 12>, sp::Field<std::uint8_t, 4>>;
*/
template <typename TContainer, typename... TFields>
class BitField {
using AllFields = std::tuple<TFields...>;
public:
template <typename... T>
BitField(const std::tuple<T...>& args) {
Apply<0, T...>(args);
}
BitField() {}
template <typename... T>
BitField& operator=(const std::tuple<T...>& args) {
Apply<0, T...>(args);
return *this;
}
AllFields& GetFields() {
return m_Fields;
}
const AllFields& GetFields() const {
return m_Fields;
}
template <std::size_t FIndex>
auto& GetField() {
return std::get<FIndex>(this->GetFields()).GetValue();
}
private:
template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true>
void Apply(const std::tuple<T...>& args) {}
template <int IOffset, typename... T, std::enable_if_t<!(IOffset >= sizeof...(T)), bool> = true>
void Apply(const std::tuple<T...>& args) {
this->GetField<IOffset>() = std::get<IOffset>(args);
Apply<1 + IOffset, T...>(args);
}
TContainer m_Value;
AllFields m_Fields;
};
/**
*
* \tparam ValueType the type of the value to store
* \tparam IAlignment 0 means no alignment
*/
template <typename ValueType, int IAlignment>
class Field {
public:
// Provide an access to the stored value
ValueType& GetValue() {
return m_Value;
}
const ValueType& GetValue() const {
return m_Value;
}
@@ -21,6 +76,10 @@ class Field {
return *this;
}
constexpr int GetAlignment() const {
return IAlignment;
}
private:
ValueType m_Value;
};
@@ -42,7 +101,7 @@ struct FieldsBuilder<std::tuple<TFields...>> {
template <typename TField, typename... TFields>
struct FieldsBuilder<TField, TFields...> {
using Type = sp::tuple_cat_t<std::tuple<Field<TField>>, typename FieldsBuilder<TFields...>::Type>;
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
};
} // namespace details