Loading...
Searching...
No Matches
CommandInstaller.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_CMD_INSTALLER_H_
33#define DRAIN_CMD_INSTALLER_H_
34
35#include <drain/Log.h>
36#include <map>
37#include <set>
38//#include "drain/util/Debug.h"
39//#include "drain/util/ReferenceMap.h"
40
41#include "drain/util/ReferenceMap.h"
42
43//#include "Command.h"
44//#include "CommandRegistry.h"
45
46#include "CommandBank.h"
47#include "CommandSections.h"
48
49
50namespace drain {
51
53// in section S of the global command registry.
58/*
59template <class C> //, char PREFIX=0, int SECTIONS=1>
60class CommandWrapper : public C {
61public:
62
64 CommandWrapper(const std::string & name, char alias = 0){
65 getCommandBank().add<C>(name,alias); //.section = S;
66 }
67
68};
69*/
70
71
72
74// in section S of the global command registry.
79template <char PREFIX=0, class SECTION=GeneralSection>
80class CommandInstaller { //: public CMD {
81public:
82
83 CommandBank & cmdBank;
84
85 CommandInstaller(CommandBank & bank = getCommandBank()) : cmdBank(bank){
86 getSection();
87 };
88
89 inline static
90 const CommandSection & getSection(){
91 return drain::Static::get<SECTION>();
92 };
93
95 static inline
96 char getPrefix(){
97 return PREFIX;
98 }
99
101 template <class CMD>
102 Command & install(const std::string & name, char alias = 0){
103 Command & cmd = cmdBank.add<CMD>(name,alias);
104 cmd.section |= getSection().index; // keep TRIGGER
105 return cmd;
106 }
107
109 template <class CMD>
110 Command & install(char alias = 0){
111 std::string name = CMD().getName();
112 CommandBank::deriveCmdName(name, PREFIX);
113 return install<CMD>(name, alias);
114 }
115
117
121 template <class CMD>
122 Command & install(CMD & cmdExt, const std::string & name, char alias = 0){
123 Command & cmd = cmdBank.addExternal(cmdExt, name,alias);// must give cmdExt, for copying
124 cmd.section |= getSection().index; // keep TRIGGER
125 return cmd;
126 }
127
129
133 template <class CMD>
134 Command & install(CMD & cmdExt, char alias = 0){
135 std::string name = cmdExt.getName();
136 CommandBank::deriveCmdName(name, PREFIX);
137 return install(cmdExt, name, alias);
138 }
139
140 // Deprecated method name - use plain install()
141 /*
142 template <class CMD>
143 Command & installExternal(CMD & cmdExt, const std::string & name, char alias = 0){
144 Command & cmd = cmdBank.addExternal(cmdExt, name,alias);// must give cmdExt, for copying
145 cmd.section |= getSection().index; // keep TRIGGER
146 return cmd;
147 }
148
149 // Deprecated method name - use plain install()
150 template <class CMD>
151 Command & installExternal(CMD & cmdExt, char alias = 0){
152 std::string name = cmdExt.getName();
153 CommandBank::deriveCmdName(name, PREFIX);
154 return installExternal(cmdExt, name, alias);
155 }
156 */
157
159 template <class CMD>
160 static
161 Command & installShared(const std::string & name, char alias = 0){
162 Command & cmd = getCommandBank().add<CMD>(name,alias);
163 cmd.section |= getSection().index; // keep TRIGGER
164 return cmd;
165 }
166
168 template <class CMD>
169 static
170 Command & installShared(char alias = 0){
171 std::string name = CMD().getName();
172 CommandBank::deriveCmdName(name, PREFIX);
173 return installShared<CMD>(name, alias);
174 }
175
176 /*
177 template <class ...CC>
178 static
179 //void linkRelatedCommands(const std::string & cmd, const CC & ... cmds){
180 void linkRelatedCommands(const Command & cmd, const CC & ... cmds){
181 std::string name = cmd.getName();
182 CommandBank::deriveCmdName(name, PREFIX);
183
184 // relatedCommands.insert(&cmd);
185 // linkRelatedCommands(cmds...);
186 linkRelatedCommands(name, cmds...);
187 // return *this;
188 };
189
190
191 template <class ...CC>
192 static // inline
193 void linkRelatedCommands(const std::string & name, const Command & cmd, const CC & ... cmds){
194 //cmd.linkRelatedCommands(name);
195 cmd.relatedCommands.insert(name);
196 linkRelatedCommands(name, cmds...);
197 }
198
199 static inline
200 void linkRelatedCommands(const std::string & name, const Command & cmd){
201 cmd.relatedCommands.insert(name);
202 // cmd.linkRelatedCommands(name);
203 }
204
205 // mutable
206 // std::set<const Command *> relatedCommands;
207
208 typedef std::set<const Command *> CmdSet;
209
210 template <class ...TT>
211 inline
212 void createCmdSet(CmdSet & set,const TT & ... cmds){
213 set.insert(cmds);
214 }
215 */
216
217
218};
219
220
222
227template <char PREFIX=0, class SECTION=GeneralSection>
228class CommandModule : protected CommandInstaller<PREFIX, SECTION>{
229
230public:
231
233 //typedef CommandInstaller<PREFIX, SECTION> base_t;
234
240 CommandModule(CommandBank & bank = getCommandBank()) : CommandInstaller<PREFIX, SECTION>(bank)
241 {
242
243 };
244
248 template <class CMD, class CMD_NEW = CMD>
249 Command & installDeprecating(const std::string & name, char alias = 0){
250 Command & cmd = this->cmdBank.template add<CMD>(name,alias);
251 cmd.section |= this->getSection().index; // keep TRIGGER
252
253 cmd.section |= drain::Static::get<drain::DeprecatingSection>().index;
254 //cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
255
256
257 std::string nameNew = CMD_NEW().getName();
258 CommandBank::deriveCmdName(nameNew, PREFIX);
259 cmd.relatedCommands.insert(nameNew);
260
261 return cmd;
262 }
263
267 template <class CMD, class CMD_NEW = CMD>
268 Command & installDeprecating(char alias = 0){
269 std::string name = CMD().getName();
270 CommandBank::deriveCmdName(name, PREFIX);
271
272 return installDeprecating<CMD,CMD_NEW>(name, alias);
273
274 /*
275 // this->cmdBank.linkRelatedCommandList(cmdList);
276 Command & cmd = this->template install<CMD>(name, alias);
277
278 std::string nameNew = CMD_NEW().getName();
279 CommandBank::deriveCmdName(nameNew, PREFIX);
280
281 cmd.relatedCommands.insert(nameNew);
282 return cmd;
283 // return install<CMD>(name, alias);
284 */
285 }
286
287
288
292 template <class ...TT>
293 inline
294 void linkRelatedCommands(TT & ... cmds){
295 std::set<std::string> cmdList;
296 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
297 }
298
299 template <class ...TT>
300 inline
301 void linkRelatedCommandList(std::set<std::string> & cmdList, Command & cmd, TT & ... cmds){
302 std::string name = cmd.getName();
303 CommandBank::deriveCmdName(name, PREFIX);
304 cmdList.insert(name);
305 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
306 }
307
308 template <class ...TT>
309 inline
310 void linkRelatedCommandList(std::set<std::string> & cmdList, const std::string & cmdName, TT & ... cmds){
311 cmdList.insert(cmdName);
312 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
313 }
314
315
316 template <class ...TT>
317 inline
318 void linkRelatedCommandList(std::set<std::string> & cmdList){
319 this->cmdBank.linkRelatedCommandList(cmdList); // infinite loop?
320 }
321
322
323
324};
325
326}
327
328
330#define DRAIN_CMD_INSTALL(prefix, cmd) drain::Command & cmd = install<prefix##cmd>
331
332
333/* namespace drain */
334
335#endif /* DRAINLET_H_ */
336
337// Rack
Container and execution tools for commands derived from Command.
Definition CommandBank.h:56
static void deriveCmdName(std::string &name, char prefix=0)
Given a command class name like MyFileReadCommand, derives a respective command line option ("myFileR...
Definition CommandBank.cpp:62
Creates an instance of command class C.
Definition CommandInstaller.h:80
Command & install(char alias=0)
Install external command.
Definition CommandInstaller.h:110
Command & install(CMD &cmdExt, char alias=0)
Install external command.
Definition CommandInstaller.h:134
static char getPrefix()
Return the character used as prefix for the commands in this section.
Definition CommandInstaller.h:96
static Command & installShared(const std::string &name, char alias=0)
Install to shared (global) CommandBank.
Definition CommandInstaller.h:161
static Command & installShared(char alias=0)
Install to shared (global) CommandBank.
Definition CommandInstaller.h:170
Command & install(CMD &cmdExt, const std::string &name, char alias=0)
Install external command.
Definition CommandInstaller.h:122
Command & install(const std::string &name, char alias=0)
Install command initialized and stored by command bank.
Definition CommandInstaller.h:102
Combines command installation and sectioning.
Definition CommandInstaller.h:228
Command & installDeprecating(const std::string &name, char alias=0)
Definition CommandInstaller.h:249
CommandModule(CommandBank &bank=getCommandBank())
Definition CommandInstaller.h:240
void linkRelatedCommands(TT &... cmds)
Definition CommandInstaller.h:294
Command & installDeprecating(char alias=0)
Definition CommandInstaller.h:268
Definition CommandSections.h:54
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition Command.h:55
std::set< std::string > relatedCommands
Add related command(s), to appear in help after "See-also:" phrase.
Definition Command.h:223
virtual const std::string & getName() const =0
Returns the class name of this command, like "CmdVerbose".
Definition DataSelector.cpp:1277
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition CommandBank.cpp:51