repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
repr.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <string>
5#include <string_view>
6#include <type_traits>
7
10
14#include <librepr/repr/all.h>
15#include <librepr/name/type.h>
16#include <librepr/literal.h>
17#include <librepr/options.h>
18
19namespace librepr {
21private:
22 bool separate = false;
23 std::size_t level = 0;
24 Options options = {};
25
26 void print_indent() {
27 if (options.indent != 0) {
28 result << "\n";
29 result << std::string(options.indent * level, ' ');
30 }
31 }
32
33 void print_separator() {
34 if (separate) {
35 result << ", ";
36 } else {
37 separate = true;
38 }
39 print_indent();
40 }
41
42public:
43 explicit ReprVisitor(Options const& options_) : options(options_) {}
45
46 void nesting(bool increase) {
47 if (increase) {
48 separate = false;
49 result << '{';
50 ++level;
51 }
52 else {
53 --level;
54 print_indent();
55 result << '}';
56 separate = true;
57 }
58 }
59
61 print_separator();
62 result << '.'<< name << '=';
63 separate = false;
64 }
65
66 template <typename T>
67 void type() {
68 print_separator();
69 if constexpr (!librepr::is_literal_v<T>) {
70 if (options.should_print_type(level)) {
72 }
73 }
74 }
75
76 template <typename T>
77 void value(T const& obj) {
78 type<T>();
80 }
81
82 template <typename T>
84 void value(T const& obj) {
85 if constexpr (!librepr::is_literal_v<T>) {
86 type<T>();
87 nesting(true);
88 }
89
90 result << obj.repr();
91
92 if constexpr (!librepr::is_literal_v<T>) {
93 nesting(false);
94 }
95 }
96
97 // special case string literals
98 void value(char const* obj) {
99 // TODO template this for wide string literals
100 // don't print a type, only print the separator if needed
101 print_separator();
102 result << REPR_FORMAT("\"{}\"", obj);
103 }
104
105 template <typename T>
106 requires std::is_pointer_v<T>
107 void value(T const& obj) {
108 // don't print a type, only print the separator if needed
109 print_separator();
110 using underlying_type = std::remove_pointer_t<T>;
111
112 if constexpr (!std::is_same_v<underlying_type, void> && !std::is_pointer_v<underlying_type>) {
113 // try to reflect whatever the pointer is pointing to
114 // disabled for void* and pointer-to-pointer T
115
116 if (obj) { // don't attempt to dereference nullptr
118 nesting(true);
120 nesting(false);
121 return;
122 }
123 }
124 result << '(' << librepr::get_name<T>() << ')' << librepr::repr(static_cast<const void*>(obj));
125 }
126
127
128 template <typename T>
131 if constexpr (category::has_name<T>){
132 member_name(info.name());
133 }
134 value(info.value());
135 }
136
137 template <typename T>
140 if constexpr (category::has_name<T>){
141 member_name(info.name());
142 }
143
145 nesting(true);
146 info.visit(*this);
147 nesting(false);
148 }
149};
150
151//static_assert(Visitor::Hierarchical<ReprVisitor>, "Formatter isn't a valid hierarchical visitor.");
152
153} // namespace librepr
#define REPR_FORMAT(...)
Definition repr.h:20
void nesting(bool increase)
Definition repr.h:46
void type()
Definition repr.h:67
void value(T const &obj)
Definition repr.h:84
void operator()(T info)
Definition repr.h:139
void value(char const *obj)
Definition repr.h:98
void value(T const &obj)
Definition repr.h:77
detail::StringBuffer result
Definition repr.h:44
ReprVisitor(Options const &options_)
Definition repr.h:43
void member_name(std::string_view name)
Definition repr.h:60
void operator()(T info)
Definition repr.h:130
void value(T const &obj)
Definition repr.h:107
Definition category.h:43
Definition category.h:20
Definition category.h:40
Definition concepts.h:16
Definition ctvi/ctvi.h:9
std::string repr(T const &obj)
Definition repr/enum.h:17
std::string code_for()
Definition repr:39
constexpr auto member_name
Definition member.h:126
Definition options.h:7
std::size_t indent
Definition options.h:13
bool should_print_type(std::size_t level) const
Definition options.h:16
static void visit(V &&visitor, T &obj)
Definition reflection/reflect.h:20
Wrapper around std::string.
Definition buffer.h:14