forked from daniele77/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginmanager.cpp
325 lines (292 loc) · 10.3 KB
/
pluginmanager.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
* CLI - A simple command line interface.
* Copyright (C) 2016-2021 Daniele Pallastrelli
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************/
#ifdef CLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER
#include <cli/standaloneasioscheduler.h>
using MainScheduler = cli::StandaloneAsioScheduler;
#elif defined(CLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER)
// TODO: NB boostasioscheduler.h includes boost asio
// so in Windows it should appear before cli.h and clilocalsession.h that includes rang,
// because both include WinSock.h
// (consider to provide a global header file for the library)
#include <cli/boostasioscheduler.h>
using MainScheduler = cli::BoostAsioScheduler;
#else // i.e. CLI_EXAMPLES_USE_LOOP_SCHEDULER (default is loop scheduler because it does not require external dependencies)
#include <cli/loopscheduler.h>
using MainScheduler = cli::LoopScheduler;
#endif
#include "cli/clilocalsession.h"
#include "cli/cli.h"
#include <vector>
using namespace cli;
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////////
// plugin mechanism
class Plugin
{
public:
Plugin(const string& _name) : name(_name) { cout << "Plugin " << name << " loaded" << endl; }
virtual ~Plugin() { cout << "Plugin " << name << " unloaded" << endl; }
const string& Name() const { return name; }
private:
const string name;
};
using Factory = function<unique_ptr<Plugin>(Menu*)>;
class PluginRegistry
{
public:
static PluginRegistry& Instance() { return instance; }
void Register(const string& name, Factory factory) { plugins.push_back(make_pair(name, factory)); }
void Print(ostream& out) const
{
for (auto& p: plugins)
out << " - " << p.first << endl;
}
unique_ptr<Plugin> Create(const string& name, Menu* menu) const
{
for (auto& p: plugins)
if (p.first == name)
return p.second(menu);
return {};
}
private:
static PluginRegistry instance;
vector<pair<string, Factory>> plugins;
};
PluginRegistry PluginRegistry::instance;
class PluginContainer
{
public:
static PluginContainer& Instance() { return instance; }
void SetMenu(Menu& _menu) { menu = &_menu; }
void Load(const string& plugin)
{
auto p = PluginRegistry::Instance().Create(plugin, menu);
if (p)
plugins.push_back(move(p));
}
void Unload(const string& plugin)
{
plugins.erase(
remove_if(
plugins.begin(),
plugins.end(),
[&](auto& p){ return p->Name() == plugin; }
),
plugins.end()
);
}
void PrintLoaded(ostream& out) const
{
for (auto& p: plugins)
out << " - " << p->Name() << endl;
}
private:
static PluginContainer instance;
vector<unique_ptr<Plugin>> plugins;
Menu* menu;
};
PluginContainer PluginContainer::instance;
class Registration
{
public:
Registration(const string& name, Factory factory)
{
PluginRegistry::Instance().Register(name, factory);
}
};
template <typename T, const char* NAME>
class RegisteredPlugin : public Plugin
{
public:
RegisteredPlugin() : Plugin(NAME)
{
const Registration& dummy = registration;
do { (void)(dummy); } while (0); // silence unused variable warning
}
private:
static unique_ptr<Plugin> Create(Menu* menu) { return make_unique<T>(menu); }
static Registration registration;
};
template <typename T, const char* NAME>
Registration RegisteredPlugin<T, NAME>::registration(NAME, &RegisteredPlugin<T, NAME>::Create);
///////////////////////////////////////////////////////////////////////////////////////////////
// plugins
///////////////////////////// arithmetic.cpp //////////////////////////////////
// Arithmetic plugin
constexpr char ArithmeticName[] = "arithmetic";
class Arithmetic : public RegisteredPlugin<Arithmetic, ArithmeticName>
{
public:
Arithmetic(Menu* menu)
{
auto subMenu = make_unique<Menu>(Name());
subMenu->Insert(
"add", {"first_term", "second_term"},
[](std::ostream& out, int x, int y)
{
out << x << " + " << y << " = " << (x+y) << "\n";
},
"Print the sum of the two numbers" );
subMenu->Insert(
"add",
[](std::ostream& out, int x, int y, int z)
{
out << x << " + " << y << " + " << z << " = " << (x+y+z) << "\n";
},
"Print the sum of the three numbers" );
subMenu->Insert(
"sub", {"subtrahend", "minuend"},
[](std::ostream& out, int x, int y)
{
out << x << " - " << y << " = " << (x-y) << "\n";
},
"Print the result of a subtraction" );
menuHandler = menu->Insert(move(subMenu));
}
~Arithmetic() override
{
menuHandler.Remove();
}
private:
CmdHandler menuHandler;
};
///////////////////////////// strings.cpp //////////////////////////////////
// Strings plugin
constexpr char StringsName[] = "strings";
class Strings : public RegisteredPlugin<Strings, StringsName>
{
public:
Strings(Menu* menu)
{
auto subMenu = make_unique<Menu>(Name());
subMenu->Insert(
"reverse", {"string_to_revert"},
[](std::ostream& out, const string& arg)
{
string copy(arg);
std::reverse(copy.begin(), copy.end());
out << copy << "\n";
},
"Print the reverse string" );
subMenu->Insert(
"upper",
[](std::ostream& out, string arg)
{
std::transform(arg.begin(), arg.end(),arg.begin(), ::toupper);
out << arg << "\n";
},
"Print the string in uppercase" );
subMenu->Insert(
"sort", {"list of strings separated by space"},
[](std::ostream& out, std::vector<std::string> data)
{
std::sort(data.begin(), data.end());
out << "sorted list: ";
std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(out, " "));
out << "\n";
},
"Alphabetically sort a list of words" );
menuHandler = menu->Insert(move(subMenu));
}
~Strings() override
{
menuHandler.Remove();
}
private:
CmdHandler menuHandler;
};
///////////////////////////////////////////////////////////////////////////////////////////////
// Entry point
int main()
{
CmdHandler colorCmd;
CmdHandler nocolorCmd;
// setup cli
auto rootMenu = make_unique< Menu >( "cli" );
PluginContainer::Instance().SetMenu(*rootMenu);
rootMenu->Insert(
"list",
[](std::ostream& out){ PluginRegistry::Instance().Print(out); },
"Print the plugin list" );
rootMenu->Insert(
"loaded",
[](std::ostream& out)
{
PluginContainer::Instance().PrintLoaded(out);
},
"Load the plugin specified" );
rootMenu->Insert(
"load", {"plugin_name"},
[](std::ostream&, const string& plugin)
{
PluginContainer::Instance().Load(plugin);
},
"Load the plugin specified" );
rootMenu->Insert(
"unload", {"plugin_name"},
[](std::ostream&, const string& plugin)
{
PluginContainer::Instance().Unload(plugin);
},
"Unload the plugin specified" );
colorCmd = rootMenu -> Insert(
"color",
[&](std::ostream& out)
{
out << "Colors ON\n";
SetColor();
colorCmd.Disable();
nocolorCmd.Enable();
},
"Enable colors in the cli" );
nocolorCmd = rootMenu -> Insert(
"nocolor",
[&](std::ostream& out)
{
out << "Colors OFF\n";
SetNoColor();
colorCmd.Enable();
nocolorCmd.Disable();
},
"Disable colors in the cli" );
Cli cli( std::move(rootMenu) );
// global exit action
cli.ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } );
MainScheduler scheduler;
CliLocalTerminalSession localSession(cli, scheduler, std::cout, 200);
localSession.ExitAction(
[&scheduler](auto& out) // session exit action
{
out << "Closing App...\n";
scheduler.Stop();
}
);
scheduler.Run();
return 0;
}