Skip to content

Commit a50a4aa

Browse files
committed
Serde: slightly more robust scalar vs composite detection
Signed-off-by: Alexis Jeandet <[email protected]>
1 parent cf79e34 commit a50a4aa

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

include/cpp_utils/reflexion/reflection.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ consteval auto is_field()
325325
}
326326
else
327327
{
328-
return false;
328+
return std::is_fundamental_v<composite_t> || std::is_enum_v<composite_t>;
329329
}
330330
}
331331

@@ -379,17 +379,20 @@ template <typename field_t>
379379
}
380380

381381
template <typename record_t, typename T, typename... Ts>
382-
[[nodiscard]] constexpr inline bool fields_have_const_size(const record_t& s, T&& field, Ts&&... fields)
382+
[[nodiscard]] constexpr inline bool fields_have_const_size(
383+
const record_t& s, T&& field, Ts&&... fields)
383384
{
384-
return fields_have_const_size(s, std::forward<T>(field)) && fields_have_const_size(s, std::forward<Ts>(fields)...);
385+
return fields_have_const_size(s, std::forward<T>(field))
386+
&& fields_have_const_size(s, std::forward<Ts>(fields)...);
385387
}
386388

387-
SPLIT_FIELDS([[nodiscard]] constexpr bool, composite_have_const_size, fields_have_const_size, const);
389+
SPLIT_FIELDS(
390+
[[nodiscard]] constexpr bool, composite_have_const_size, fields_have_const_size, const);
388391

389392
template <typename composite_t>
390393
consteval std::size_t composite_have_const_size()
391394
{
392-
if constexpr (std::is_fundamental_v<composite_t>)
395+
if constexpr (std::is_fundamental_v<composite_t> || std::is_enum_v<composite_t>)
393396
return true;
394397
if constexpr (is_dyn_size_field_v<composite_t>)
395398
return false;
@@ -436,7 +439,8 @@ SPLIT_FIELDS([[nodiscard]] constexpr std::size_t, composite_size, fields_size, c
436439
template <typename composite_t>
437440
consteval std::size_t composite_size()
438441
{
439-
static_assert(composite_have_const_size<composite_t>(), "Composite type must have a constant size");
442+
static_assert(
443+
composite_have_const_size<composite_t>(), "Composite type must have a constant size");
440444
return composite_size(composite_t {});
441445
}
442446

include/cpp_utils/serde/deserialization.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ constexpr inline std::size_t load_field(const auto& parent_composite, auto& pars
9494
return load_value(parsing_context, offset, field, parent_composite);
9595
}
9696

97+
constexpr inline std::size_t load_field(const auto& parent_composite, auto& parsing_context,
98+
std::size_t offset, types::concepts::enum_type auto& field)
99+
{
100+
using underlying_t = std::underlying_type_t<std::decay_t<decltype(field)>>;
101+
underlying_t& underlying_field = reinterpret_cast<underlying_t&>(field);
102+
return load_value(parsing_context, offset, underlying_field, parent_composite);
103+
}
104+
97105
template <typename field_t>
98106
concept dyn_array_field = dynamic_array_field<field_t> && !dynamic_array_until_eof_field<field_t>;
99107

@@ -121,8 +129,8 @@ constexpr inline std::size_t load_field(const auto& parent_composite, auto& pars
121129
}
122130

123131
template <typename field_t>
124-
concept dy_arr_until_eof_of_const_size
125-
= dynamic_array_until_eof_field<std::decay_t<field_t>> && const_size_field<typename std::decay_t<field_t>::value_type>;
132+
concept dy_arr_until_eof_of_const_size = dynamic_array_until_eof_field<std::decay_t<field_t>>
133+
&& const_size_field<typename std::decay_t<field_t>::value_type>;
126134

127135
constexpr inline std::size_t load_field(const auto& parent_composite, auto& parsing_context,
128136
std::size_t offset, dy_arr_until_eof_of_const_size auto& array_field)
@@ -154,8 +162,8 @@ concept dy_arr_until_eof_of_dyn_size
154162
= dynamic_array_until_eof_field<field_t> && !const_size_field<typename field_t::value_type>;
155163

156164

157-
constexpr inline std::size_t load_field(const auto& parent_composite, auto& parsing_context,
158-
std::size_t offset, dy_arr_until_eof_of_dyn_size auto& array_field)
165+
constexpr inline std::size_t load_field(const auto&, auto& parsing_context, std::size_t offset,
166+
dy_arr_until_eof_of_dyn_size auto& array_field)
159167
{
160168
using array_field_t = std::decay_t<decltype(array_field)>;
161169
using field_t = typename array_field_t::value_type;

include/cpp_utils/types/concepts.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ concept pointer_to_contiguous_memory = requires(T t) {
4545
template <class T>
4646
concept fundamental_type = std::is_fundamental_v<std::decay_t<T>>;
4747

48+
template <class T>
49+
concept enum_type = std::is_enum_v<std::decay_t<T>>;
50+
4851
template <class T>
4952
concept container = requires(T t) {
5053
{ t.size() } -> std::convertible_to<std::size_t>;

0 commit comments

Comments
 (0)