mimic++ v9.2.1
Loading...
Searching...
No Matches
Print.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_PRINT_HPP
7#define MIMICPP_PRINTING_STATE_PRINT_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
16
17#ifndef MIMICPP_DETAIL_IS_MODULE
18 #include <functional>
19 #include <iterator>
20 #include <type_traits>
21 #include <utility>
22#endif
23
24namespace mimicpp::printing::detail::state
25{
26 template <
27 print_iterator OutIter,
28 typename T,
29 printer_for<OutIter, T> Printer = custom::Printer<std::remove_const_t<T>>>
30 constexpr OutIter print(
31 [[maybe_unused]] util::priority_tag<4> const,
32 OutIter out,
33 T& value)
34 {
35 return Printer::print(
36 std::move(out),
37 value);
38 }
39
40 template <
41 print_iterator OutIter,
42 typename T,
43 printer_for<OutIter, T> Printer = state::common_type_printer<std::remove_const_t<T>>>
44 constexpr OutIter print(
45 [[maybe_unused]] util::priority_tag<3> const,
46 OutIter out,
47 T& value)
48 {
49 return Printer::print(
50 std::move(out),
51 value);
52 }
53
54 template <
55 print_iterator OutIter,
56 typename T,
57 printer_for<OutIter, T> Printer = state::cxx23_backport_printer<std::remove_const_t<T>>>
58 constexpr OutIter print(
59 [[maybe_unused]] util::priority_tag<2> const,
60 OutIter out,
61 T& value)
62 {
63 return Printer::print(
64 std::move(out),
65 value);
66 }
67
68 template <print_iterator OutIter, format::detail::formattable<CharT> T>
69 constexpr OutIter print(
70 [[maybe_unused]] util::priority_tag<1> const,
71 OutIter out,
72 T& value)
73 {
74 return format::format_to(
75 std::move(out),
76 "{}",
77 value);
78 }
79
80 template <print_iterator OutIter>
81 constexpr OutIter print(
82 [[maybe_unused]] util::priority_tag<0> const,
83 OutIter out,
84 auto&)
85 {
86 return format::format_to(
87 std::move(out),
88 "{{?}}");
89 }
90
91 inline constexpr util::priority_tag<4> maxStatePrinterTag{};
92}
93
95{
96 class PrintFn
97 {
98 public:
99 template <print_iterator OutIter>
100 constexpr OutIter operator()(OutIter out, auto&& value) const
101 {
102 static_assert(
103 requires {
104 {
105 detail::state::print(detail::state::maxStatePrinterTag, out, value)
106 } -> std::convertible_to<OutIter>; },
107 "The given type is not printable. ");
108
109 return detail::state::print(
110 detail::state::maxStatePrinterTag,
111 std::move(out),
112 value);
113 }
114
115 [[nodiscard]]
116 StringT operator()(auto&& value) const
117 {
118 StringStreamT stream{};
119 std::invoke(*this, std::ostreambuf_iterator{stream}, value);
120
121 return std::move(stream).str();
122 }
123 };
124}
125
127{
132
179
183 [[maybe_unused]] inline constexpr printing::PrintFn print{};
184
188}
189
190#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition Print.hpp:97
constexpr OutIter operator()(OutIter out, auto &&value) const
Definition Print.hpp:100
StringT operator()(auto &&value) const
Definition Print.hpp:116
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183
Definition Fwd.hpp:31
Definition Call.hpp:24
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Format.hpp:35
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391