repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
python.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <stack>
6#include <string>
7#include <string_view>
8#include <type_traits>
9#include <librepr/name/type.h>
16
17namespace librepr {
18namespace detail {
20 for (auto at = text.find(needle, 0); at != text.npos; at = text.find(needle, at)) {
21 text.replace(at, needle.length(), replacement);
22 }
23}
24} // namespace detail
25
31
32 [[nodiscard]] auto get_indent() const { return std::string(indent_amount * indent_level, ' '); }
33
35 auto needle = std::string_view{"::"};
36 for (auto at = namespaces_.find(needle, 0); at != namespaces_.npos; at = namespaces_.find(needle, at)) {
37 result << get_indent() << "class " << namespaces_.substr(0, at) << ":\n";
38 namespaces_.remove_prefix(at + 2);
40 }
41 result << get_indent() << "class " << namespaces_ << ":\n";
43 }
44
47 detail::replace_all(ret, "::", ".");
48 return ret;
49 }
50
51 template <typename T>
52 void print_type() {
53 if constexpr (std::is_fundamental_v<T>) {
54 if constexpr (std::is_floating_point_v<T>) {
55 result << "float";
56 } else if constexpr (std::same_as<T, bool>) {
57 result << "bool";
58 } else if constexpr (std::same_as<T, char>) {
59 result << "str";
60 } else if constexpr (std::is_integral_v<T>) {
61 result << "int";
62 } else {
63 // todo handle all fundamentals
65 }
66 } else if constexpr (std::same_as<T, const char*>) {
67 result << "str";
68 } else if constexpr (requires { typename Reflect<T>::element_type; }) {
69 if constexpr (requires { typename T::mapped_type; }) {
70 result << "dict[";
72 result << ", ";
74 result << "]";
75 } else {
76 result << "list[";
78 result << "]";
79 }
80 } else if constexpr (is_variant<T>) {
82 if constexpr (Index != 0) {
83 result << " | ";
84 }
86 });
87 } else {
89 }
90 }
91
92 template <typename T>
94 auto operator()(T info) {
95 auto full_name = info.type_name();
96 auto name = std::string_view{full_name};
98 bool out_of_line = false;
100
101 if (!namespaces.empty()) {
102 if (name.starts_with(namespaces.top())) {
103 name.remove_prefix(namespaces.top().size() + 2);
104 } else {
105 // namespace mismatch -> defined out of line
107 indent_level = 0;
108 out_of_line = true;
109 if (auto pos = name.rfind("::"); pos != std::string_view::npos) {
110 namespace_ = name.substr(0, pos);
111 name = name.substr(pos + 2);
113 }
114 }
115 }
116
117 result << get_indent() << "@dataclass\n";
118 result << get_indent() << "class " << name << ":\n";
119
120 namespaces.push(full_name);
121 indent_level++;
122 if constexpr (T::members::size == 0) {
123 result << get_indent() << "...\n";
124 } else {
125 info.visit(*this);
126 }
127 indent_level--;
128 namespaces.pop();
129
130 if (out_of_line) {
131 indent_level = last_indent; // roll back indent level
132 result.set_cursor(); // roll back cursor
133 }
134
135 result << get_indent();
136 if constexpr (category::has_name<T>) {
137 result << info.name() << ": ";
138 if (!namespace_.empty()) {
140 }
141 result << name << '\n';
142 }
143 }
144
145 template <typename T>
146 auto operator()(T info) {
147 result << get_indent() << info.name() << ": ";
149 result << '\n';
150 }
151};
152} // namespace librepr
Definition category.h:26
Definition category.h:20
Definition variant.h:42
void replace_all(std::string &text, std::string_view needle, std::string_view replacement)
Definition python.h:19
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
Definition members.h:68
Definition python.h:26
void print_type()
Definition python.h:52
std::size_t indent_level
Definition python.h:27
detail::StringBuffer result
Definition python.h:30
std::stack< std::string > namespaces
Definition python.h:29
auto operator()(T info)
Definition python.h:146
std::size_t indent_amount
Definition python.h:28
auto get_indent() const
Definition python.h:32
auto operator()(T info)
Definition python.h:94
void print_namespaces(std::string_view namespaces_)
Definition python.h:34
static auto fix_namespace(std::string_view namespace_)
Definition python.h:45
Definition reflection/reflect.h:16
Wrapper around std::string.
Definition buffer.h:14
void set_cursor(std::size_t index=std::string::npos)
Definition buffer.h:77