4#include "generators/temporal_types.h"
26#if HEGEL_HAS_REFLECTION
31namespace hegel::internal {
33 template <
typename T> std::string cpp_type_name();
35 void print_value(hegel::Printer& printer,
const T& value);
40 template <
typename T>
constexpr std::string_view compiler_type_name() {
42 std::string_view p = __PRETTY_FUNCTION__;
43 auto start = p.find(
"T = ");
44 auto end = p.rfind(
']');
45 if (start == std::string_view::npos || end == std::string_view::npos) {
49 return p.substr(start, end - start);
50#elif defined(__GNUC__)
51 std::string_view p = __PRETTY_FUNCTION__;
52 auto start = p.find(
"T = ");
53 if (start == std::string_view::npos) {
57 auto end = p.find(
';', start);
58 if (end == std::string_view::npos) {
61 if (end == std::string_view::npos) {
64 return p.substr(start, end - start);
65#elif defined(_MSC_VER)
66 std::string_view p = __FUNCSIG__;
67 constexpr std::string_view marker =
"compiler_type_name<";
68 auto start = p.find(marker);
69 auto end = p.rfind(
">(");
70 if (start == std::string_view::npos || end == std::string_view::npos) {
73 start += marker.size();
74 return p.substr(start, end - start);
80 namespace repr_detail {
82 template <
typename T>
struct is_vector : std::false_type {};
84 struct is_vector<std::vector<T>> : std::true_type {};
86 template <
typename T>
struct is_set : std::false_type {};
87 template <
typename T>
struct is_set<std::set<T>> : std::true_type {};
89 template <
typename T>
struct is_map : std::false_type {};
90 template <
typename K,
typename V>
91 struct is_map<std::map<K, V>> : std::true_type {};
93 template <
typename T>
struct is_optional : std::false_type {};
95 struct is_optional<std::
optional<T>> : std::true_type {};
97 template <
typename T>
struct is_pair : std::false_type {};
98 template <
typename A,
typename B>
99 struct is_pair<std::pair<A, B>> : std::true_type {};
101 template <
typename T>
struct is_tuple : std::false_type {};
102 template <
typename... Ts>
103 struct is_tuple<std::tuple<Ts...>> : std::true_type {};
105 template <
typename T>
struct is_variant : std::false_type {};
106 template <
typename... Ts>
107 struct is_variant<std::
variant<Ts...>> : std::true_type {};
109 template <
typename T>
struct is_std_array : std::false_type {};
110 template <
typename E, std::
size_t N>
111 struct is_std_array<std::array<E, N>> : std::true_type {};
113 template <
typename T>
struct array_traits;
114 template <
typename E, std::
size_t N>
115 struct array_traits<std::array<E, N>> {
116 using element_type = E;
117 static constexpr std::size_t size = N;
123 template <
typename T>
124 using is_temporal_value =
125 hegel::generators::temporal::detail::is_value<T>;
127 template <
typename T>
struct is_chrono_duration : std::false_type {};
128 template <
typename Rep,
typename Period>
129 struct is_chrono_duration<std::chrono::duration<Rep, Period>>
132 template <
typename T>
133 using is_system_time_point =
134 hegel::generators::temporal::detail::is_system_time_point<T>;
138 template <
typename T>
139 using is_instant_time_point =
140 hegel::generators::temporal::detail::holds_instants<T>;
142 template <
typename T,
typename =
void>
143 struct prints_itself : std::false_type {};
144 template <
typename T>
145 struct prints_itself<
146 T, std::void_t<decltype(std::declval<const T&>().print_to(
147 std::declval<hegel::Printer&>()))>> : std::true_type {};
149 template <
typename T,
typename =
void>
150 struct has_ostream_operator : std::false_type {};
151 template <
typename T>
152 struct has_ostream_operator<
153 T, std::void_t<decltype(std::declval<std::ostream&>()
154 << std::declval<const T&>())>>
157#if HEGEL_HAS_REFLECTION
158 template <typename T, typename = void>
159 struct is_reflectable_impl : std::false_type {};
160 template <typename T>
161 struct is_reflectable_impl<
162 T, std::void_t<decltype(rfl::to_view(std::declval<T&>()))>>
167 template <typename T>
168 struct is_reflectable
169 : std::conjunction<std::is_aggregate<T>, is_reflectable_impl<T>> {};
172 template <typename... Ts> std::string type_name_list() {
173 if constexpr (sizeof...(Ts) == 0) {
176 std::string names[] = {cpp_type_name<Ts>()...};
178 for (std::size_t i = 0; i < sizeof...(Ts); ++i) {
188 template <typename T> struct template_args;
189 template <typename... Ts> struct template_args<std::tuple<Ts...>> {
190 static std::string names() { return type_name_list<Ts...>(); }
192 template <typename... Ts> struct template_args<std::variant<Ts...>> {
193 static std::string names() { return type_name_list<Ts...>(); }
200 inline void append_escaped(std::string& out, unsigned char c,
230 if (c >= 0x20 && c < 0x7f) {
231 out += static_cast<char>(c);
235 std::snprintf(buf, sizeof(buf), "\\%03o", c);
241 inline std::string quoted(const std::string& text) {
242 std::string out = "\"";
243 for (char c : text) {
244 auto byte = static_cast<unsigned char>(c);
248 append_escaped(out, byte, true);
258 template <typename T> std::string cpp_type_name() {
259 if constexpr (std::is_same_v<T, bool>) {
261 } else if constexpr (std::is_same_v<T, char>) {
263 } else if constexpr (std::is_same_v<T, signed char>) {
264 return "signed char";
265 } else if constexpr (std::is_same_v<T, unsigned char>) {
266 return "unsigned char";
267 } else if constexpr (std::is_same_v<T, short>) {
269 } else if constexpr (std::is_same_v<T, unsigned short>) {
270 return "unsigned short";
271 } else if constexpr (std::is_same_v<T, int>) {
273 } else if constexpr (std::is_same_v<T, unsigned int>) {
274 return "unsigned int";
275 } else if constexpr (std::is_same_v<T, long>) {
277 } else if constexpr (std::is_same_v<T, unsigned long>) {
278 return "unsigned long";
279 } else if constexpr (std::is_same_v<T, long long>) {
281 } else if constexpr (std::is_same_v<T, unsigned long long>) {
282 return "unsigned long long";
283 } else if constexpr (std::is_same_v<T, float>) {
285 } else if constexpr (std::is_same_v<T, double>) {
287 } else if constexpr (std::is_same_v<T, long double>) {
288 return "long double";
289 } else if constexpr (std::is_same_v<T, std::string>) {
290 return "std::string";
291 } else if constexpr (std::is_same_v<T, std::monostate>) {
292 return "std::monostate";
293 } else if constexpr (repr_detail::is_vector<T>::value) {
294 return "std::vector<" + cpp_type_name<typename T::value_type>() +
296 } else if constexpr (repr_detail::is_set<T>::value) {
297 return "std::set<" + cpp_type_name<typename T::value_type>() + ">";
298 } else if constexpr (repr_detail::is_map<T>::value) {
299 return "std::map<" + cpp_type_name<typename T::key_type>() + ", " +
300 cpp_type_name<typename T::mapped_type>() + ">";
301 } else if constexpr (repr_detail::is_optional<T>::value) {
302 return "std::optional<" + cpp_type_name<typename T::value_type>() +
304 } else if constexpr (repr_detail::is_pair<T>::value) {
305 return "std::pair<" + cpp_type_name<typename T::first_type>() +
306 ", " + cpp_type_name<typename T::second_type>() + ">";
307 } else if constexpr (repr_detail::is_tuple<T>::value) {
308 return "std::tuple<" + repr_detail::template_args<T>::names() + ">";
309 } else if constexpr (repr_detail::is_variant<T>::value) {
310 return "std::variant<" + repr_detail::template_args<T>::names() +
312 } else if constexpr (repr_detail::is_std_array<T>::value) {
313 using traits = repr_detail::array_traits<T>;
314 return "std::array<" +
315 cpp_type_name<typename traits::element_type>() + ", " +
316 std::to_string(traits::size) + ">";
317 } else if constexpr (std::is_same_v<T, std::chrono::nanoseconds>) {
318 return "std::chrono::nanoseconds";
319 } else if constexpr (std::is_same_v<T, std::chrono::microseconds>) {
320 return "std::chrono::microseconds";
321 } else if constexpr (std::is_same_v<T, std::chrono::milliseconds>) {
322 return "std::chrono::milliseconds";
323 } else if constexpr (std::is_same_v<T, std::chrono::seconds>) {
324 return "std::chrono::seconds";
325 } else if constexpr (std::is_same_v<T, std::chrono::minutes>) {
326 return "std::chrono::minutes";
327 } else if constexpr (std::is_same_v<T, std::chrono::hours>) {
328 return "std::chrono::hours";
329 } else if constexpr (repr_detail::is_system_time_point<T>::value) {
330 return "std::chrono::sys_time<" +
331 cpp_type_name<typename T::duration>() + ">";
332 } else if constexpr (repr_detail::is_chrono_duration<T>::value) {
333 using period = typename T::period;
334 return "std::chrono::duration<" + cpp_type_name<typename T::rep>() +
335 ", std::ratio<" + std::to_string(period::num) + ", " +
336 std::to_string(period::den) + ">>";
338 return std::string(compiler_type_name<T>());
342 namespace repr_detail {
344 template <typename Items>
345 void print_sequence(hegel::Printer& printer, const std::string& open,
346 const Items& items) {
347 printer.seq(open, "}", items,
348 [](hegel::Printer& p, const auto& item) {
349 print_value(p, item);
353 template <typename Tuple>
354 void print_tuple(hegel::Printer& printer, const std::string& open,
355 const Tuple& value) {
356 printer.begin_block(open);
359 [&](const auto&... elems) {
360 ((first ? void(first = false) : printer.separator(),
361 print_value(printer, elems)),
365 printer.end_block("}");
369 template <typename T> std::string integer_digits(T value) {
370 using U = std::make_unsigned_t<T>;
371 auto magnitude = static_cast<U>(value);
372 bool negative = false;
373 if constexpr (std::is_signed_v<T>) {
374 negative = value < 0;
376 magnitude = static_cast<U>(U{0} - magnitude);
379 std::string reversed;
381 reversed.push_back(static_cast<char>('0' + magnitude % 10));
383 } while (magnitude != 0);
385 reversed.push_back('-');
387 return std::string(reversed.rbegin(), reversed.rend());
391 template <typename T> std::string scalar_text(const T& value) {
392 if constexpr (std::is_same_v<T, bool>) {
393 return value ? "true" : "false";
394 } else if constexpr (std::is_same_v<T, char>) {
395 std::string out = "'";
396 append_escaped(out, static_cast<unsigned char>(value),
400 } else if constexpr (std::is_same_v<T, signed char> ||
401 std::is_same_v<T, unsigned char>) {
402 return "static_cast<" + cpp_type_name<T>() + ">(" +
403 std::to_string(static_cast<int>(value)) + ")";
404 } else if constexpr (std::is_same_v<T, int>) {
405 return std::to_string(value);
406 } else if constexpr (std::is_same_v<T, unsigned int>) {
407 return std::to_string(value) + "u";
408 } else if constexpr (std::is_same_v<T, long>) {
409 return std::to_string(value) + "l";
410 } else if constexpr (std::is_same_v<T, unsigned long>) {
411 return std::to_string(value) + "ul";
412 } else if constexpr (std::is_same_v<T, long long>) {
413 return std::to_string(value) + "ll";
414 } else if constexpr (std::is_same_v<T, unsigned long long>) {
415 return std::to_string(value) + "ull";
416 } else if constexpr (std::is_integral_v<T>) {
418 return "static_cast<" + cpp_type_name<T>() + ">(" +
419 integer_digits(value) + ")";
420 } else if constexpr (std::is_enum_v<T>) {
421 using underlying = std::underlying_type_t<T>;
422 return "static_cast<" + cpp_type_name<T>() + ">(" +
423 scalar_text(static_cast<underlying>(value)) + ")";
424 } else if constexpr (std::is_floating_point_v<T>) {
425 if (std::isnan(value)) {
426 return "std::numeric_limits<" + cpp_type_name<T>() +
429 if (std::isinf(value)) {
430 std::string inf = "std::numeric_limits<" +
431 cpp_type_name<T>() + ">::infinity()";
432 return value < 0 ? "-" + inf : inf;
435 constexpr int digits = std::numeric_limits<T>::max_digits10;
436 if constexpr (std::is_same_v<T, long double>) {
437 std::snprintf(buf, sizeof(buf), "%.*Lg", digits, value);
439 std::snprintf(buf, sizeof(buf), "%.*g", digits,
440 static_cast<double>(value));
442 std::string out = buf;
445 if (out.find('.') == std::string::npos &&
446 out.find('e') == std::string::npos &&
447 out.find('E') == std::string::npos) {
450 if constexpr (std::is_same_v<T, float>) {
452 } else if constexpr (std::is_same_v<T, long double>) {
456 } else if constexpr (std::is_same_v<T, std::string>) {
457 bool has_nul = value.find('\0') != std::string::npos;
458 std::string out = "std::string(\"";
459 for (char c : value) {
460 append_escaped(out, static_cast<unsigned char>(c),
466 out += ", " + std::to_string(value.size());
471 static_assert(std::is_same_v<T, std::monostate>);
472 return "std::monostate{}";
476 template <typename T>
477 inline constexpr bool is_scalar_v =
478 std::is_arithmetic_v<T> || std::is_enum_v<T> ||
479 std::is_same_v<T, std::string> || std::is_same_v<T, std::monostate>;
486 template <typename T>
487 void print_value(hegel::Printer& printer, const T& value) {
488 if constexpr (repr_detail::is_scalar_v<T>) {
489 printer.text(repr_detail::scalar_text(value));
490 } else if constexpr (repr_detail::prints_itself<T>::value) {
491 value.print_to(printer);
492 } else if constexpr (repr_detail::is_pair<T>::value ||
493 repr_detail::is_tuple<T>::value) {
494 repr_detail::print_tuple(printer, cpp_type_name<T>() + "{", value);
495 } else if constexpr (repr_detail::is_optional<T>::value) {
496 std::string name = cpp_type_name<T>();
497 if (!value.has_value()) {
498 printer.text(name + "{}");
501 printer.begin_block(name + "{");
502 print_value(printer, *value);
503 printer.end_block("}");
504 } else if constexpr (repr_detail::is_variant<T>::value) {
507 printer.begin_block(cpp_type_name<T>() + "{");
508 printer.text("std::in_place_index<" +
509 std::to_string(value.index()) + ">");
511 std::visit([&printer](const auto& v) { print_value(printer, v); },
513 printer.end_block("}");
514 } else if constexpr (repr_detail::is_vector<T>::value ||
515 repr_detail::is_set<T>::value ||
516 repr_detail::is_std_array<T>::value) {
517 repr_detail::print_sequence(printer, cpp_type_name<T>() + "{",
519 } else if constexpr (repr_detail::is_map<T>::value) {
520 printer.seq(cpp_type_name<T>() + "{", "}", value,
521 [](hegel::Printer& p, const auto& entry) {
523 print_value(p, entry.first);
525 print_value(p, entry.second);
528 } else if constexpr (repr_detail::is_temporal_value<T>::value) {
529 printer.text(cpp_type_name<T>() + "{" +
530 repr_detail::quoted(to_string(value)) + "}");
531 } else if constexpr (repr_detail::is_chrono_duration<T>::value) {
532 printer.text(cpp_type_name<T>() + "{");
533 print_value(printer, value.count());
535 } else if constexpr (repr_detail::is_instant_time_point<T>::value) {
536 std::optional<hegel::generators::temporal::Instant> instant =
537 hegel::generators::temporal::Instant::from_chrono(value);
538 if (instant.has_value()) {
539 printer.text(cpp_type_name<T>() + "{" +
540 repr_detail::quoted(to_string(*instant)) + "}");
543 printer.text(cpp_type_name<T>() + "{");
544 print_value(printer, value.time_since_epoch());
547#if HEGEL_HAS_REFLECTION
548 else if constexpr (repr_detail::is_reflectable<T>::value) {
549 printer.begin_block(cpp_type_name<T>() + "{");
551 auto view = rfl::to_view(const_cast<T&>(value));
552 view.apply([&printer, &first](const auto& field) {
557 printer.text("." + std::string(field.name()) + " = ");
558 print_value(printer, *field.value());
560 printer.end_block("}");
563 else if constexpr (repr_detail::has_ostream_operator<T>::value) {
564 std::ostringstream os;
566 printer.reflow(os.str());
568 printer.text("<unprintable " +
569 std::string(compiler_type_name<T>()) + ">");
575 template <typename T>
576 std::string render(const T& value, std::size_t width) {
577 hegel::Document document(width);
578 print_value(document.printer(), value);
579 return std::move(document).finish();
583 template <typename T> std::string repr(const T& value) {
584 return render(value, unbounded_width);
587 namespace repr_detail {
589 template <typename T, typename = void>
590 struct is_representable : std::false_type {};
592 template <typename T>
593 struct is_representable<
594 T, std::enable_if_t<is_scalar_v<T> || prints_itself<T>::value>>
597 template <typename A, typename B>
598 struct is_representable<
600 std::enable_if_t<!prints_itself<std::pair<A, B>>::value>>
601 : std::conjunction<is_representable<A>, is_representable<B>> {};
603 template <typename... Ts>
604 struct is_representable<
606 std::enable_if_t<!prints_itself<std::tuple<Ts...>>::value>>
607 : std::conjunction<is_representable<Ts>...> {};
609 template <typename... Ts>
610 struct is_representable<
612 std::enable_if_t<!prints_itself<std::variant<Ts...>>::value>>
613 : std::conjunction<is_representable<Ts>...> {};
615 template <typename T>
616 struct is_representable<
618 std::enable_if_t<!prints_itself<std::optional<T>>::value>>
619 : is_representable<T> {};
621 template <typename T>
622 struct is_representable<
624 std::enable_if_t<!prints_itself<std::vector<T>>::value>>
625 : is_representable<T> {};
627 template <typename T>
628 struct is_representable<
629 std::set<T>, std::enable_if_t<!prints_itself<std::set<T>>::value>>
630 : is_representable<T> {};
632 template <typename T, std::size_t N>
633 struct is_representable<
635 std::enable_if_t<!prints_itself<std::array<T, N>>::value>>
636 : is_representable<T> {};
638 template <typename K, typename V>
639 struct is_representable<
641 std::enable_if_t<!prints_itself<std::map<K, V>>::value>>
642 : std::conjunction<is_representable<K>, is_representable<V>> {};
644 template <typename Rep, typename Period>
645 struct is_representable<std::chrono::duration<Rep, Period>,
646 std::enable_if_t<!prints_itself<
647 std::chrono::duration<Rep, Period>>::value>>
648 : is_representable<Rep> {};
650 template <typename T>
651 struct is_representable<
652 T, std::enable_if_t<is_instant_time_point<T>::value &&
653 !prints_itself<T>::value>> : std::true_type {};
655 template <typename T>
656 inline constexpr bool is_std_compound_v =
657 is_pair<T>::value || is_tuple<T>::value || is_variant<T>::value ||
658 is_optional<T>::value || is_vector<T>::value || is_set<T>::value ||
659 is_std_array<T>::value || is_map<T>::value;
661 template <typename T>
662 struct is_representable<
663 T, std::enable_if_t<!is_scalar_v<T> && !prints_itself<T>::value &&
664 !is_std_compound_v<T> &&
665 !is_chrono_duration<T>::value &&
666 !is_instant_time_point<T>::value &&
667 (has_ostream_operator<T>::value
668#if HEGEL_HAS_REFLECTION
669 || is_reflectable<T>::value
671 )>> : std::true_type {
700 template <typename T>
701 inline constexpr bool is_representable_v =
702 internal::repr_detail::is_representable<T>::value;
704#if defined(__cpp_concepts) && __cpp_concepts >= 201907L
709 template <typename T>
710 concept Representable = is_representable_v<T>;
713 template <typename T> void Printer::repr(const T& value) {
714 if (should_print()) {
715 internal::print_value(*this, value);
Generator< std::variant< Ts... > > variant(Generator< Ts >... gens)
Generate std::variant from heterogeneous generators.
Definition combinators.h:306
Generator< std::optional< T > > optional(Generator< T > gen)
Generate optional values (present or absent).
Definition combinators.h:341