mimic++ v9.3.0
Loading...
Searching...
No Matches
C++23Backports.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024-2026.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_PRINTING_STATE_CXX23_BACKPORTS_HPP
7#define MIMICPP_PRINTING_STATE_CXX23_BACKPORTS_HPP
8
9#pragma once
10
14
15#ifndef MIMICPP_DETAIL_IS_MODULE
16 #include <concepts>
17 #include <functional>
18 #include <ranges>
19 #include <tuple>
20 #include <utility>
21#endif
22
23namespace mimicpp::printing::detail::state
24{
25 template <std::ranges::forward_range Range>
26 struct cxx23_backport_printer<Range>
27 {
28 template <print_iterator OutIter>
29 static constexpr OutIter print(OutIter out, auto& range)
30 {
31 out = format::format_to(std::move(out), "[");
32 auto iter = std::ranges::begin(range);
33 if (const auto end = std::ranges::end(range);
34 iter != end)
35 {
36 out = mimicpp::print(std::move(out), *iter++);
37
38 for (; iter != end; ++iter)
39 {
40 out = format::format_to(std::move(out), ", ");
41 out = mimicpp::print(std::move(out), *iter);
42 }
43 }
44
45 return format::format_to(std::move(out), "]");
46 }
47 };
48
49 template <std::size_t index, print_iterator OutIter>
50 constexpr OutIter print_tuple_element(OutIter out, auto& tuple)
51 {
52 if constexpr (0u != index)
53 {
54 out = format::format_to(std::move(out), ", ");
55 }
56
57 using std::get;
58 return mimicpp::print(std::move(out), get<index>(tuple));
59 }
60
61 template <typename Tuple, typename IndexSequence>
62 struct has_tuple_element;
63
64 // clang-format off
65 template <typename Tuple, std::size_t index>
66 struct has_tuple_element<Tuple, std::index_sequence<index>>
67 : std::bool_constant<requires {typename std::tuple_element_t<index, Tuple>; }>
68 {
69 };
70
71 // clang-format on
72
73 template <typename Tuple, std::size_t... indices>
74 struct has_tuple_element<Tuple, std::index_sequence<indices...>>
75 : std::conjunction<
76 has_tuple_element<Tuple, std::index_sequence<indices>>...>
77 {
78 };
79
80 template <typename T>
81 concept tuple_like = requires {
82 typename std::tuple_size<T>::type;
83 { std::tuple_size_v<T> } -> std::convertible_to<std::size_t>;
84 requires 0u <= std::tuple_size_v<T>;
85 requires has_tuple_element<T, std::make_index_sequence<std::tuple_size_v<T>>>::value;
86 };
87
88 template <tuple_like T>
89 struct cxx23_backport_printer<T>
90 {
91 template <print_iterator OutIter>
92 static constexpr OutIter print(OutIter out, auto& tuple)
93 {
94 out = format::format_to(std::move(out), "(");
95 std::invoke(
96 [&]<std::size_t... indices>(std::index_sequence<indices...> const /*sequence*/) {
97 (...,
98 (out = state::print_tuple_element<indices>(std::move(out), tuple)));
99 },
100 std::make_index_sequence<std::tuple_size_v<T>>{});
101
102 return format::format_to(std::move(out), ")");
103 }
104 };
105}
106
107#endif
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183