mimic++ v9.2.1
Loading...
Searching...
No Matches
C++23Backports.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
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 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 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 return mimicpp::print(std::move(out), std::get<index>(tuple));
58 }
59
60 template <typename T>
61 concept tuple_like = requires {
62 typename std::tuple_size<T>::type;
63 { std::tuple_size_v<T> } -> std::convertible_to<std::size_t>;
64 requires 0u <= std::tuple_size_v<T>;
65 };
66
67 template <tuple_like T>
68 struct cxx23_backport_printer<T>
69 {
70 template <print_iterator OutIter>
71 static OutIter print(OutIter out, auto& tuple)
72 {
73 out = format::format_to(std::move(out), "(");
74 std::invoke(
75 [&]<std::size_t... indices>([[maybe_unused]] const std::index_sequence<indices...>) {
76 (...,
77 (out = state::print_tuple_element<indices>(std::move(out), tuple)));
78 },
79 std::make_index_sequence<std::tuple_size_v<T>>{});
80
81 return format::format_to(std::move(out), ")");
82 }
83 };
84}
85
86#endif
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183