6#ifndef MIMICPP_PRINTING_TYPE_TEMPLATED_HPP
7#define MIMICPP_PRINTING_TYPE_TEMPLATED_HPP
18#ifndef MIMICPP_DETAIL_IS_MODULE
23 #include <memory_resource>
24 #include <type_traits>
28namespace mimicpp::printing::type::detail
30#ifdef MIMICPP_CONFIG_EXPERIMENTAL_PRETTY_TYPES
32 template <
typename T, pr
int_iterator OutIter>
33 constexpr OutIter pretty_template_name(OutIter out)
36 auto const iter = std::ranges::find(name,
'<');
37 MIMICPP_ASSERT(iter != name.cend(),
"Given name is not a template.");
38 name.erase(iter, name.end());
43 template <
typename NameGenerator>
44 struct basic_template_type_printer
46 template <pr
int_iterator OutIter>
47 static constexpr OutIter
print(OutIter out)
49 return std::invoke(NameGenerator{}, std::move(out));
53 template <
typename Type,
template <
typename...>
typename Template,
typename LeadingArgList>
54 struct is_default_arg_for
55 :
public std::false_type
59 template <
typename Type,
template <
typename...>
typename Template,
typename... LeadingArgs>
60 requires requires {
typename Template<LeadingArgs...>; }
61 struct is_default_arg_for<Type, Template, util::type_list<LeadingArgs...>>
62 :
public std::is_same<
63 Template<LeadingArgs...>,
64 Template<LeadingArgs..., Type>>
68 template <
typename Type,
template <
typename...>
typename Template,
typename LeadingArgList>
69 concept default_arg_for = is_default_arg_for<Type, Template, LeadingArgList>::value;
71 template <
typename List>
72 struct type_list_pop_back_if_can
73 :
public util::type_list_pop_back<List>
78 struct type_list_pop_back_if_can<util::type_list<>>
80 using type = util::type_list<>;
85 template <
typename...>
typename Template,
86 typename PopBack = type_list_pop_back_if_can<ArgList>>
87 struct drop_default_args_for_impl
92 template <
typename ArgList,
template <
typename...>
typename Template,
typename PopBack>
93 requires requires {
typename PopBack::popped; }
94 && default_arg_for<typename PopBack::popped, Template, typename PopBack::type>
95 struct drop_default_args_for_impl<ArgList, Template, PopBack>
96 :
public drop_default_args_for_impl<typename PopBack::type, Template>
100 template <
template <
typename...>
typename Template,
typename ArgList>
101 struct drop_default_args_for
102 :
public drop_default_args_for_impl<ArgList, Template>
106 template <
template <
typename...>
typename Template,
typename ArgList>
107 struct template_type_name_generator_fn
109 template <pr
int_iterator OutIter>
110 constexpr OutIter operator()(OutIter out)
const
112 using MinimalArgList =
typename drop_default_args_for<Template, ArgList>::type;
113 using Type = util::type_list_populate_t<Template, MinimalArgList>;
115 out = pretty_template_name<Type>(std::move(out));
116 out = format::format_to(std::move(out),
"<");
117 out = print_separated(std::move(out),
", ", MinimalArgList{});
118 out = format::format_to(std::move(out),
">");
124 template <
template <
typename...>
typename Template,
typename... Ts>
125 struct template_type_printer<Template<Ts...>>
126 :
public basic_template_type_printer<
127 template_type_name_generator_fn<Template, util::type_list<Ts...>>>
131 template <
typename Allocator>
132 struct is_pmr_allocator
133 :
public std::false_type
137 template <
typename T>
138 struct is_pmr_allocator<std::pmr::polymorphic_allocator<T>>
139 :
public std::true_type
143 template <
template <
typename...>
typename Template,
typename... Ts>
144 concept std_pmr_container =
146 typename Template<Ts...>::allocator_type;
147 typename Template<Ts...>::value_type;
149 && is_pmr_allocator<
typename Template<Ts...>::allocator_type>::value
150 && default_arg_for<std::allocator<
typename Template<Ts...>::value_type>, Template,
util::type_list_pop_back_t<util::type_list<Ts...>>>;
154 template <
template <
typename...>
typename Template,
typename ArgList>
155 struct potential_pmr_container_type_name_generator_fn
157 template <pr
int_iterator OutIter>
158 OutIter operator()(OutIter out)
const
160 StringT const name = std::invoke(
164 template_type_name_generator_fn<
166 util::type_list_pop_back_t<ArgList>>{},
167 std::ostreambuf_iterator{stream});
168 return std::move(stream).str();
172 if (name.starts_with(stdPrefix))
174 out = format::format_to(std::move(out),
"std::pmr::");
175 out = std::ranges::copy(
176 name.cbegin() + stdPrefix.size(),
186 template_type_name_generator_fn<Template, ArgList>{},
194 template <
template <
typename...>
typename Template,
typename... Ts>
195 requires std_pmr_container<Template, Ts...>
196 struct template_type_printer<Template<Ts...>>
197 :
public basic_template_type_printer<
198 potential_pmr_container_type_name_generator_fn<Template, util::type_list<Ts...>>>
202 template <
template <
typename,
auto...>
typename Template,
typename T,
auto n>
203 struct array_like_name_generator
205 template <pr
int_iterator OutIter>
206 constexpr OutIter operator()(OutIter out)
const
208 out = pretty_template_name<Template<T, n>>(std::move(out));
209 out = format::format_to(std::move(out),
"<");
212 if constexpr (
constexpr bool isDefaultSize =
requires {
requires std::same_as<Template<T>, Template<T, n>>; };
215 out = format::format_to(std::move(out),
", {}", n);
218 out = format::format_to(std::move(out),
">");
227 template <
template <
typename,
auto...>
typename Template,
typename T,
auto n>
228 struct template_type_printer<Template<T, n>>
229 :
public basic_template_type_printer<
230 array_like_name_generator<Template, T, n>>
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:51
constexpr TypeMatcher< T > type
Matcher, which can be used as a last resort to disambiguate similar overloads.
Definition GeneralMatchers.hpp:297
constexpr printing::PrintFn print
Functional object, converting the given object to its textual representation.
Definition Print.hpp:183
StringT type_name()
Returns the (potentially demangled) name.
Definition PrintType.hpp:201
MIMICPP_DETAIL_CONSTEXPR_STRING OutIter prettify_type(OutIter out, StringT name)
Prettifies a demangled name.
Definition PrintType.hpp:207
constexpr printing::PrintTypeFn< T > print_type
Functional object, converting the given type to its textual representation.
Definition PrintType.hpp:478
typename type_list_pop_back< TypeList >::type type_list_pop_back_t
Definition TypeList.hpp:86
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Format.hpp:35
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:392
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391