libcamera: ipa_data_serializer: Add specialization for enums
Instead of handling enums specially in the code generation templates, create a specialization of `IPADataSerializer` that handles enums. Every enum is serialized as a `uint32_t`, with `static_assert` to ensure that every possible value fits. Previously, enums were (de)serialized in `(de)serializer_field()` based on the size of their underlying types. Afer this change, every enum is uniformly handled as a `uint32_t`. Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
This commit is contained in:
@@ -61,7 +61,7 @@ T readPOD(std::vector<uint8_t> &vec, size_t pos)
|
||||
|
||||
} /* namespace */
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename = void>
|
||||
class IPADataSerializer
|
||||
{
|
||||
public:
|
||||
@@ -344,6 +344,52 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
class IPADataSerializer<E, std::enable_if_t<std::is_enum_v<E>>>
|
||||
{
|
||||
using U = uint32_t;
|
||||
static_assert(sizeof(E) <= sizeof(U));
|
||||
|
||||
public:
|
||||
static std::tuple<std::vector<uint8_t>, std::vector<SharedFD>>
|
||||
serialize(const E &data, [[maybe_unused]] ControlSerializer *cs = nullptr)
|
||||
{
|
||||
std::vector<uint8_t> dataVec;
|
||||
appendPOD<U>(dataVec, static_cast<U>(data));
|
||||
|
||||
return { dataVec, {} };
|
||||
}
|
||||
|
||||
static E deserialize(std::vector<uint8_t> &data,
|
||||
[[maybe_unused]] ControlSerializer *cs = nullptr)
|
||||
{
|
||||
return deserialize(data.cbegin(), data.cend());
|
||||
}
|
||||
|
||||
static E deserialize(std::vector<uint8_t>::const_iterator dataBegin,
|
||||
std::vector<uint8_t>::const_iterator dataEnd,
|
||||
[[maybe_unused]] ControlSerializer *cs = nullptr)
|
||||
{
|
||||
return static_cast<E>(readPOD<U>(dataBegin, 0, dataEnd));
|
||||
}
|
||||
|
||||
static E deserialize(std::vector<uint8_t> &data,
|
||||
[[maybe_unused]] std::vector<SharedFD> &fds,
|
||||
[[maybe_unused]] ControlSerializer *cs = nullptr)
|
||||
{
|
||||
return deserialize(data.cbegin(), data.cend());
|
||||
}
|
||||
|
||||
static E deserialize(std::vector<uint8_t>::const_iterator dataBegin,
|
||||
std::vector<uint8_t>::const_iterator dataEnd,
|
||||
[[maybe_unused]] std::vector<SharedFD>::const_iterator fdsBegin,
|
||||
[[maybe_unused]] std::vector<SharedFD>::const_iterator fdsEnd,
|
||||
[[maybe_unused]] ControlSerializer *cs = nullptr)
|
||||
{
|
||||
return deserialize(dataBegin, dataEnd);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* __DOXYGEN__ */
|
||||
|
||||
} /* namespace libcamera */
|
||||
|
||||
Reference in New Issue
Block a user