/
main.cpp
220 lines (183 loc) · 5.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <memory>
#include <span>
#include <array>
#include <sstream>
#include <functional>
#include <optional>
template <typename... Ts>
struct type_list
{
template <typename... Us>
constexpr auto operator+(type_list<Us...>) const noexcept
{
return type_list<Ts..., Us...>{};
}
template <typename... Us>
constexpr bool operator==(type_list<Us...>) const noexcept
{
return std::is_same_v<type_list<Ts...>, type_list<Us...>>;
}
};
struct stateful_type_list
{
private:
template <size_t N>
struct getter
{
friend consteval auto flag(getter);
};
template <typename T, size_t N>
struct setter
{
friend consteval auto flag(getter<N>)
{
return type_list<T>{};
}
static constexpr size_t value = N;
};
public:
template <typename T, size_t N = 0>
consteval static size_t try_push()
{
if constexpr (requires { flag(getter<N>{}); })
{
return try_push<T, N + 1>();
}
else
{
return setter<T, N>::value;
}
}
template <typename Unique, size_t N = 0, auto = []{}>
consteval static auto get()
{
if constexpr (requires { flag(getter<N>{}); })
{
return flag(getter<N>{}) + get<Unique, N + 1>();
}
else
{
return type_list<>{};
}
}
};
class Printer
{
enum class Function
{
Print,
PrintToStream,
PrintToString,
};
template <Function F, typename... Ts>
struct vtable_func
{
private:
template <typename Derived, typename R, typename... Args>
static void run_impl(R(Derived::* func)(Args...), Printer* printer, void* argsTuplePtr, void* ret)
{
const auto bound = std::bind_front(func, static_cast<Derived*>(printer));
auto& argsTuple = *static_cast<std::tuple<Args&&...>*>(argsTuplePtr);
if constexpr (std::is_same_v<R, void>)
{
std::apply(bound, std::move(argsTuple));
}
else
{
*static_cast<std::optional<R>*>(ret) = std::apply(bound, std::move(argsTuple));
}
}
public:
template <typename Derived>
static void run(Printer* printer, void* argsTuplePtr, void* ret)
{
if constexpr (F == Function::Print)
{
run_impl(&Derived::template print<Ts...>, printer, argsTuplePtr, ret);
}
else if constexpr (F == Function::PrintToStream)
{
run_impl(&Derived::template print_to_stream<Ts...>, printer, argsTuplePtr, ret);
}
else if constexpr (F == Function::PrintToString)
{
run_impl(&Derived::template print_to_string<Ts...>, printer, argsTuplePtr, ret);
}
}
};
template <typename Derived, typename... Funcs>
static auto create_vtable(type_list<Funcs...>)
{
static constinit std::array vtable { Funcs::template run<Derived>... };
return std::span{ vtable };
}
const std::span<void(*)(Printer*, void*, void*)> m_vtable;
protected:
template <typename Derived>
Printer(Derived*) :
m_vtable{ create_vtable<Derived>(stateful_type_list::get<Derived>()) }
{}
public:
template <typename... Args,
size_t Index = stateful_type_list::try_push<vtable_func<Function::Print, Args...>>()>
void print(Args&&... args)
{
auto argsTuple = std::forward_as_tuple(std::forward<Args>(args)...);
m_vtable[Index](this, &argsTuple, nullptr);
}
template <typename... Args,
size_t Index = stateful_type_list::try_push<vtable_func<Function::PrintToStream, Args...>>()>
void print_to_stream(std::ostream& stream, Args&&... args)
{
auto argsTuple = std::forward_as_tuple(stream, std::forward<Args>(args)...);
m_vtable[Index](this, &argsTuple, nullptr);
}
template <typename... Args,
size_t Index = stateful_type_list::try_push<vtable_func<Function::PrintToString, Args...>>()>
std::string print_to_string(Args&&... args)
{
auto argsTuple = std::forward_as_tuple(std::forward<Args>(args)...);
std::optional<std::string> ret;
m_vtable[Index](this, &argsTuple, &ret);
return std::move(*ret);
}
};
struct PrinterImpl : Printer
{
public:
PrinterImpl() :
Printer{ this }
{}
template <typename... Args>
void print(Args&&... args)
{
((std::cout << args << '\n'), ...);
}
template <typename... Args>
void print_to_stream(std::ostream& stream, Args&&... args)
{
((stream << args << '\n'), ...);
}
template <typename... Args>
std::string print_to_string(Args&&... args)
{
std::stringstream stream;
((stream << args << '\n'), ...);
return stream.str();
}
};
std::unique_ptr<Printer> make_printer();
int main()
{
auto p = make_printer();
double d = 2.5;
const std::string s = "Hello, world!";
const auto str = p->print_to_string(5, d, s);
std::cout << str;
return 0;
}
std::unique_ptr<Printer> make_printer()
{
return std::make_unique<PrinterImpl>();
}