Skip to content

Commit 90593dd

Browse files
authored
Expose re-usable tuple concepts in json_auto.h (#1777)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 8bc5554 commit 90593dd

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/core/json/include/sourcemeta/core/json_auto.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ concept json_auto_map_like =
7575
!json_auto_has_method<T> &&
7676
std::is_same_v<typename T::key_type, JSON::String>;
7777

78+
/// @ingroup json
79+
template <typename T>
80+
concept json_auto_tuple_mono =
81+
std::tuple_size_v<std::remove_cvref_t<std::tuple<T>>> == 1;
82+
83+
// We have to do this mess because MSVC seems confuses `std::pair`
84+
// of 2 elements with this overload
85+
/// @ingroup json
86+
template <typename T>
87+
concept json_auto_tuple_poly =
88+
requires { typename std::tuple_size<std::remove_cvref_t<T>>::type; } &&
89+
(std::tuple_size_v<std::remove_cvref_t<T>> >= 2) &&
90+
(!std::is_base_of_v<
91+
std::pair<std::tuple_element_t<0, std::remove_cvref_t<T>>,
92+
std::tuple_element_t<1, std::remove_cvref_t<T>>>,
93+
std::remove_cvref_t<T>>);
94+
7895
/// @ingroup json
7996
/// If the value has a `.to_json()` method, always prefer that
8097
template <typename T>
@@ -216,27 +233,16 @@ auto to_json(const std::pair<L, R> &value) -> JSON {
216233

217234
// Handle 1-element tuples
218235
/// @ingroup json
219-
template <typename T>
220-
requires(std::tuple_size_v<std::remove_cvref_t<std::tuple<T>>> == 1)
236+
template <json_auto_tuple_mono T>
221237
auto to_json(const std::tuple<T> &value) -> JSON {
222238
auto tuple = JSON::make_array();
223239
std::apply([&](const T &element) { tuple.push_back(to_json(element)); },
224240
value);
225241
return tuple;
226242
}
227243

228-
// We have to do this mess because MSVC seems confuses `std::pair`
229-
// of 2 elements with this overload
230244
/// @ingroup json
231-
template <typename TupleT>
232-
requires(requires {
233-
typename std::tuple_size<std::remove_cvref_t<TupleT>>::type;
234-
} && (std::tuple_size_v<std::remove_cvref_t<TupleT>> >= 2) &&
235-
(!std::is_base_of_v<
236-
std::pair<std::tuple_element_t<0, std::remove_cvref_t<TupleT>>,
237-
std::tuple_element_t<1, std::remove_cvref_t<TupleT>>>,
238-
std::remove_cvref_t<TupleT>>))
239-
auto to_json(const TupleT &value) -> JSON {
245+
template <json_auto_tuple_poly T> auto to_json(const T &value) -> JSON {
240246
auto tuple = JSON::make_array();
241247
std::apply(
242248
[&tuple](const auto &...elements) {

0 commit comments

Comments
 (0)