repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
layout.h
Go to the documentation of this file.
1#pragma once
4#include <librepr/name/type.h>
5#include <librepr/visit.h>
6#include <type_traits>
7
8namespace librepr {
11private:
12 bool separate = false;
13
14 void print_separator() {
15 if (separate) {
16 result << ", ";
17 } else {
18 separate = true;
19 }
20 }
21
22 template <typename T>
23 void print_name() {
24 if constexpr (std::is_pointer_v<T> && std::same_as<std::remove_const_t<std::remove_pointer_t<T>>, char>) {
25 result << "str";
26 } else {
28 }
29 }
30
31 template <category::can_descend T>
32 void descend(T info) {
33 separate = false;
34 info.visit(*this);
35 }
36
37public:
38
39
40 template <typename T>
41 void operator()(T info) {
42 using type = typename T::type;
43 print_separator();
44
45 if constexpr (requires { typename type::value_type; }) {
46 // iterable container
47
48 result << '[';
49 if constexpr (requires {
50 typename type::key_type;
51 typename type::mapped_type;
52 }) {
53 // associative container
54 separate = false;
56 result << " -> ";
57 separate = false;
59 } else {
61 }
62
63 result << ']';
64 } else {
66 }
67 }
68
69 template <category::has_members T>
70 void operator()(T info) {
71 print_separator();
72 result << '{';
73 descend(info);
74 result << '}';
75 }
76 template <category::has_alternatives T>
77 void operator()(T info) {
78 print_separator();
79 T::alternatives::for_each([this, first=true]<typename U>() mutable {
80 if (!first) {
81 result << " | ";
82 }
83 first = false;
84 separate = false;
85 librepr::visit<U>(*this);
86 });
87 }
88
89 template <category::has_enumerator_names T>
90 void operator()(T info) {
91 print_separator();
92 for (auto const& name : info.enumerator_names) {
93 if (&*info.enumerator_names.begin() != &name) {
94 result << " | ";
95 }
96 result << name;
97 }
98 }
99
100 template <category::has_extent T>
101 requires requires { typename T::element_type; }
102 void operator()(T info) {
104 result << '[' << std::to_string(info.extent) << ']';
105 }
106};
107} // namespace librepr
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
Definition layout.h:9
void operator()(T info)
Definition layout.h:102
detail::StringBuffer result
Definition layout.h:10
void operator()(T info)
Definition layout.h:41
Wrapper around std::string.
Definition buffer.h:14
T to_string(T... args)