Skip to content

Commit 79c24ad

Browse files
Merge pull request #20 from ChemiCalChems/reduce-boilerplate
Reduce boilerplate 2, electric boogaloo, and extras
2 parents ea3a56c + 28c602e commit 79c24ad

8 files changed

+2007
-5432
lines changed

include/rfl/internal/bind_to_tuple.hpp

+1,940
Large diffs are not rendered by default.

include/rfl/internal/move_to_field_tuple.hpp

+5-35
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,24 @@
55
#include <type_traits>
66

77
#include "rfl/field_names_t.hpp"
8-
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
8+
#include "rfl/internal/bind_to_tuple.hpp"
99
#include "rfl/internal/has_fields.hpp"
10-
#include "rfl/internal/is_flatten_field.hpp"
1110
#include "rfl/internal/is_named_tuple.hpp"
12-
#include "rfl/internal/lit_name.hpp"
13-
#include "rfl/internal/move_to_field_tuple_impl.hpp"
11+
#include "rfl/internal/wrap_in_fields.hpp"
1412

1513
namespace rfl {
1614
namespace internal {
1715

18-
template <class FieldNames, int j = 0, class... Fields>
19-
auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
20-
constexpr auto size = std::tuple_size_v<std::decay_t<decltype(_tuple)>>;
21-
constexpr auto i = sizeof...(_fields);
22-
if constexpr (i == size) {
23-
return std::make_tuple(std::move(_fields)...);
24-
25-
} else {
26-
auto value = std::move(std::get<i>(_tuple));
27-
using Type = std::decay_t<decltype(value)>;
28-
if constexpr (is_flatten_field_v<Type>) {
29-
// The problem here is that the FieldNames are already flattened, but this
30-
// is not, so we need to determine how many field names to skip.
31-
constexpr auto n_skip = std::tuple_size_v<
32-
std::decay_t<flattened_ptr_tuple_t<typename Type::Type>>>;
33-
return wrap_in_fields<FieldNames, j + n_skip>(
34-
std::move(_tuple), std::move(_fields)..., std::move(value));
35-
} else {
36-
const auto name_literal = FieldNames::template name_of<j>();
37-
auto new_field =
38-
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
39-
std::move(value));
40-
return wrap_in_fields<FieldNames, j + 1>(
41-
std::move(_tuple), std::move(_fields)..., std::move(new_field));
42-
}
43-
}
44-
}
45-
4616
template <class OriginalStruct>
4717
auto move_to_field_tuple(OriginalStruct&& _t) {
4818
using T = std::decay_t<OriginalStruct>;
49-
if constexpr (internal::is_named_tuple_v<T>) {
19+
if constexpr (is_named_tuple_v<T>) {
5020
return _t.fields();
5121
} else if constexpr (has_fields<T>()) {
52-
return move_to_field_tuple_impl(std::move(_t));
22+
return bind_to_tuple(_t, [](auto& x) { return std::move(x); });
5323
} else {
5424
using FieldNames = field_names_t<T>;
55-
auto tup = move_to_field_tuple_impl(std::move(_t));
25+
auto tup = bind_to_tuple(_t, [](auto& x) { return std::move(x); });
5626
return wrap_in_fields<FieldNames>(std::move(tup));
5727
}
5828
}

include/rfl/internal/move_to_field_tuple_impl.hpp

-1,980
This file was deleted.

include/rfl/internal/ptr_field_tuple_t.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
#include <tuple>
66
#include <type_traits>
77

8-
#include "rfl/internal/is_named_tuple.hpp"
98
#include "rfl/internal/to_ptr_field_tuple.hpp"
109

1110
namespace rfl {
1211
namespace internal {
1312

1413
template <class T>
15-
using ptr_field_tuple_t =
16-
typename std::invoke_result<decltype(to_ptr_field_tuple<T>), T>::type;
14+
using ptr_field_tuple_t = decltype(to_ptr_field_tuple(std::declval<T&>()));
1715

1816
} // namespace internal
1917
} // namespace rfl

include/rfl/internal/ptr_tuple_t.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ namespace rfl {
1010
namespace internal {
1111

1212
template <class T>
13-
using ptr_tuple_t =
14-
typename std::invoke_result<decltype(to_ptr_tuple<T>), T>::type;
13+
using ptr_tuple_t = decltype(to_ptr_tuple(std::declval<T&>()));
1514

1615
} // namespace internal
1716
} // namespace rfl

include/rfl/internal/to_ptr_field_tuple.hpp

+10-2,307
Large diffs are not rendered by default.

include/rfl/internal/to_ptr_tuple.hpp

+7-1,105
Large diffs are not rendered by default.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef RFL_INTERNAL_WRAP_IN_FIELDS_HPP_
2+
#define RFL_INTERNAL_WRAP_IN_FIELDS_HPP_
3+
4+
#include <tuple>
5+
#include <type_traits>
6+
7+
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
8+
#include "rfl/internal/is_flatten_field.hpp"
9+
#include "rfl/internal/lit_name.hpp"
10+
11+
namespace rfl {
12+
namespace internal {
13+
14+
template <class FieldNames, int j = 0, class... Fields>
15+
auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
16+
constexpr auto size = std::tuple_size_v<std::decay_t<decltype(_tuple)>>;
17+
constexpr auto i = sizeof...(_fields);
18+
if constexpr (i == size) {
19+
return std::make_tuple(std::move(_fields)...);
20+
} else {
21+
auto value = std::move(std::get<i>(_tuple));
22+
using Type = std::decay_t<decltype(value)>;
23+
if constexpr (is_flatten_field_v<Type>) {
24+
// The problem here is that the FieldNames are already flattened, but this
25+
// is not, so we need to determine how many field names to skip.
26+
constexpr auto n_skip = std::tuple_size_v<
27+
std::decay_t<flattened_ptr_tuple_t<typename Type::Type>>>;
28+
return wrap_in_fields<FieldNames, j + n_skip>(
29+
std::move(_tuple), std::move(_fields)..., std::move(value));
30+
} else {
31+
const auto name_literal = FieldNames::template name_of<j>();
32+
auto new_field =
33+
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
34+
std::move(value));
35+
return wrap_in_fields<FieldNames, j + 1>(
36+
std::move(_tuple), std::move(_fields)..., std::move(new_field));
37+
}
38+
}
39+
}
40+
41+
} // namespace internal
42+
} // namespace rfl
43+
#endif

0 commit comments

Comments
 (0)