-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_function.h
More file actions
296 lines (261 loc) · 8.52 KB
/
text_function.h
File metadata and controls
296 lines (261 loc) · 8.52 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "text_function_detail.h"
#include <experimental/tuple>
#include <map>
#include <memory>
#include <sstream>
#include <iostream>
#include <regex>
#include <vector>
#include <type_traits>
namespace txtfn {
template <class Last>
void type(int& i, std::vector<std::string>& args) {
args.emplace_back(detail::demangle<Last>());
++i;
}
template <class First, class Second, class ...Rest>
void type(int& i, std::vector<std::string>& args) {
args.emplace_back(detail::demangle<First>());
++i;
type<Second, Rest...>(i, args);
}
// handle empty case
template <class...Args>
struct GetType {
static void get(std::vector<std::string>& args) {
int i = 0;
type<Args...>(i, args);
}
};
template <>
struct GetType<> {
static void get(std::vector<std::string>&) {
}
};
class TextFunctionBase {
public:
virtual ~TextFunctionBase() { }
virtual void call(const std::vector<std::string>& args, std::string& out) const = 0;
virtual const std::string& return_type() const = 0;
virtual const std::vector<std::string>& arg_types() const = 0;
virtual const std::string& help(const std::string& name) const =0;
};
template<class F, class Sig>
class TextFunction;
template<class F, class Ret, class... Args>
class TextFunction<F, Ret (Args...)> : public TextFunctionBase {
public:
TextFunction(F&& f)
: callable_(f) {
}
void call(const std::vector<std::string>& args, std::string& out) const override {
call_impl(args, out, std::is_void<Ret>());
}
private:
const std::string& return_type() const override {
return detail::demangle<Ret>();
}
const std::vector<std::string>& arg_types() const override {
static std::vector<std::string> args = [] {
std::vector<std::string> args;
GetType<Args...>::get(args);
return args;
}();
return args;
}
const std::string& help(const std::string& name) const {
static std::string help = [&] {
constexpr const char* GREEN = "\x1b[32m";
constexpr const char* RESET = "\x1b[0m";
std::ostringstream ss;
ss << return_type() << " " << GREEN << name << RESET;
auto it = arg_types().begin();
auto end = arg_types().end();
if (it != end) {
ss << " [" << *it++ << "]";
}
for (; it != end; ++it) {
ss << " [" << *it << "]";
}
return ss.str();
}();
return help;
}
template <class T>
std::decay_t<T> get(size_t& i, const std::vector<std::string>& arg) const {
std::decay_t<T> t;
std::stringstream a(arg[i]);
if (!(a >> t)) {
throw std::invalid_argument("Unable to convert arg: '" + arg[i] + "' to " + detail::demangle<T>());
}
++i;
return t;
}
void validate_args(const std::vector<std::string>& args) const {
if (args.size() != sizeof...(Args)) {
throw std::runtime_error("Wrong number of args: " + std::to_string(args.size())
+ " != " + std::to_string(sizeof...(Args)));
}
}
void call_impl(const std::vector<std::string>& args, std::string&, std::true_type) const {
validate_args(args);
size_t index = 0;
(void)index; // Remove unused warning when zero arg function
std::experimental::apply(callable_, std::tuple<Args...>{get<Args>(index, args)...});
}
void call_impl(const std::vector<std::string>& args, std::string& out, std::false_type) const{
validate_args(args);
std::stringstream conv;
size_t index = 0;
(void)index; // Remove unused warning when zero arg function
conv << std::experimental::apply(callable_, std::tuple<Args...>{get<Args>(index, args)...});
if(!conv) {
throw std::runtime_error("Failed to convert return type " + detail::demangle<Ret>() + " to string");
}
out = conv.str();
}
F callable_;
};
// Handle lambdas
template <typename S, typename F>
class TextFunction : public TextFunction<F, decltype(&F::operator())> { };
class TextFunctionHelp {
public:
TextFunctionHelp(const std::string& name) : name_(name) { }
TextFunctionHelp& arg(const std::string& name, const std::string& description) {
args_.emplace_back(Arg{name, description});
return *this;
}
TextFunctionHelp& description(const std::string& description) {
description_ = description;
return *this;
}
const std::string& name() const { return name_; }
const std::string& description() const { return description_; }
struct Arg {
std::string name;
std::string description;
};
const std::vector<Arg>& args() const { return args_; }
private:
std::string name_;
std::string description_{"No description"};
std::vector<Arg> args_;
};
struct Lookup {
const std::string& name;
size_t num_args;
};
template <class F>
std::unique_ptr<TextFunctionBase> create_text_function(F&& f) {
return std::make_unique<TextFunction<F, detail::get_signature<F>>>(std::forward<F>(f));
}
class TextFunctionLibrary {
public:
TextFunctionLibrary() {
register_help();
register_search();
}
void register_search() {
auto search = create_text_function([&] (const std::string& str) {
std::regex r(str);
std::ostringstream ss;
for (auto& fn : funcs_) {
auto data = detailed_help(fn.first.name());
if (std::regex_search(data, r)) {
ss << data << "\n";
}
}
if (ss.str().empty()) {
ss << "Nothing found for pattern: " << str;
}
return ss.str();
});
add(std::move(search),
TextFunctionHelp("search")
.arg("search_regex", "Regex is matched against names, descriptions, and arguments")
.description("Returns list of functions matching regex"));
}
std::string detailed_help(const std::string& name) {
std::ostringstream ss;
for (auto& fn : funcs_) {
if (fn.first.name() == name) {
ss << fn.second->help(fn.first.name()) << "\n Description: ";
ss << fn.first.description() << "\n Arguments:\n";
for (auto& it : fn.first.args()) {
ss << " " << it.name << ": " << it.description << "\n";
}
}
}
if (ss.str().empty()) {
ss << name << " not found";
}
return ss.str();
}
void register_help() {
auto help = create_text_function([&] {
std::ostringstream ss;
for (auto& fn : funcs_) {
ss << fn.second->help(fn.first.name()) << " -- " << fn.first.description() << "\n";
}
return ss.str();
});
add(std::move(help),
TextFunctionHelp("help")
.description("Returns list of functions"));
auto detailed_help = create_text_function([&] (const std::string& name) {
return this->detailed_help(name);
});
add(std::move(detailed_help),
TextFunctionHelp("help")
.arg("func_name", "function name to retrieve detailed help for")
.description("Returns detailed help for matching function"));
}
void add(std::unique_ptr<TextFunctionBase> fn, TextFunctionHelp help) {
if (help.args().size() != fn->arg_types().size()) {
throw std::runtime_error("Help has different number of arguments: "
+ std::to_string(help.args().size()) + " != " + std::to_string(fn->arg_types().size()));
}
if (funcs_.find(help) != funcs_.end()) {
throw std::runtime_error(help.name() + " already registered.");
}
funcs_[help] = std::move(fn);
}
void add(const std::string& name, std::unique_ptr<TextFunctionBase> fn) {
TextFunctionHelp h(name);
for (auto& arg : fn->arg_types()) {
h.arg(arg, arg);
}
h.description(fn->return_type());
if (funcs_.find(h) != funcs_.end()) {
throw std::runtime_error(name + " already registered.");
}
funcs_[h] = std::move(fn);
}
// Return false if function can not be found
bool call(const std::string& name, const std::vector<std::string>& args, std::string& out) {
const Lookup l{name, args.size()};
auto it = funcs_.find(l);
if (it == funcs_.end()) {
return false;
}
it->second->call(args, out);
return true;
}
private:
std::map<TextFunctionHelp, std::unique_ptr<TextFunctionBase>, std::less<>> funcs_;
};
inline bool operator<(const TextFunctionHelp& lhs, const Lookup& rhs) {
const auto lhs_size = lhs.args().size();
return std::tie(lhs.name(), lhs_size) < std::tie(rhs.name, rhs.num_args);
}
inline bool operator<(const TextFunctionHelp& lhs, const TextFunctionHelp& rhs) {
const auto lhs_size = lhs.args().size();
const auto rhs_size = rhs.args().size();
return std::tie(lhs.name(), lhs_size) < std::tie(rhs.name(), rhs_size);
}
inline bool operator<(const Lookup& lhs, const TextFunctionHelp& rhs) {
const auto rhs_size = rhs.args().size();
return std::tie(lhs.name, lhs.num_args) < std::tie(rhs.name(), rhs_size);
}
}