Loading...
Searching...
No Matches
CommandBank.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
33// New design (2020)
34
35#ifndef COMMAND_BANK_H_
36#define COMMAND_BANK_H_
37
38#include <iostream>
39
40#include "drain/util/Bank.h"
41#include "drain/util/StringMapper.h"
42#include "Command.h"
43#include "Program.h"
44
45
46namespace drain {
47
48typedef int bank_section_t;
49
51
56class CommandBank : public BankSuper<Command> { // BankSuper<BasicCommand> {
57
58public:
59
60 typedef data_t command_t;
61
63 };
64
66 // Copy sections?
67 };
68
69
71 static
72 std::set<std::string> & trimWords();
73
75 static
76 void deriveCmdName(std::string & name, char prefix=0);
77 //const std::set<std::string> & trim = trimWords());
78
79
80 template <class D, char PREFIX=0>
81 D & addExternal(const D & entry, char alias = 0){
82 std::string key(entry.getName());
83 deriveCmdName(key, PREFIX);
84 //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
85 return BankSuper<Command>::addExternal(entry, key, alias);
86 }
87
88 // Template arg PREFIX is tricky, sometimes need for dynamic
89 template <class D>
90 D & addExternal(char PREFIX, const D & entry, char alias=0){
91 std::string key(entry.getName());
92 deriveCmdName(key, PREFIX);
93 return BankSuper<Command>::addExternal(entry, key, alias);
94 }
95
96 template <class D, char PREFIX=0>
97 inline
98 D & addExternal(const D & entry, const std::string & key, char alias = 0){
99 //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
100 return BankSuper<Command>::addExternal(entry, key, alias);
101 }
102
103
104
106 // void includeFile(const std::string & filename, Program & prog, Program::iterator it) const;
107
109
111
114 // static
115 void readFileTXT(const std::string & filename, Script & script) const;
116
117 void readFile(const std::string & filename, Program & prog) const;
118
120
123 void scriptify(int argc, const char **argv, Script & script) const;
124
126
129 //static
130 void scriptify(const std::string & cmdLine, Script & script) const;
131
133
138 bool scriptify(const std::string & arg, const std::string & argNext, Script & script) const;
139
140
142
145 // void append(const Script & script, Program & prog, Context & context) const ;
146 void append(const Script & script, Program & prog) const ;
147
148protected:
149
150 void append(const Script & script, Context & ctx, Program & prog) const ;
151
152
153 static
154 void tokenize(const std::string & cmdLine, std::list<std::string> & args);
155
156public:
157
158 drain::StringMapper logFileSyntax; // todo:setter
159
161 inline
162 void flushRoutine(Script & script, Context & ctx, Program & prog){
163 append(script, ctx, prog);
164 script.clear();
165 }
166
167
168 void remove(Program & prog) const;
169
171 // void run(const std::string & cmd, const std::string & params, Context & ctx);
172
174
179 void run(const std::string & key, Command & cmd, Context & ctx) const;
180
181 void commitAddedCommands(Context & ctx, Program & prog, Program::iterator it) const;
183 //void run(ScriptTxt & script, drain::Context & context);
184 // void run(Script & script, ClonerBase<Context> & contextSrc);
185
186 // Newish
187 void run(Program & prog, ClonerBase<Context> & contextCloner); // !! May be RackContext!
188
189 // experimental
190 void traverseLoops(Context & ctx, ClonerBase<Context> & contextCloner);
191
192 // experimental
193 void traverseLoops(Context & ctx, ClonerBase<Context> & contextCloner, Loop::loopStack::const_iterator it, Loop::loopStack::const_iterator itEnd);
194
195
197 void help(std::ostream & ostr = std::cout);
198
200 void help(FlagResolver::ivalue_t sectionFilter, std::ostream & ostr = std::cout); // Flagger::ivalue_t sectionFilter
201
203 void help(const std::string & key, std::ostream & ostr = std::cout);
204
206 void info(const std::string & key, const command_t & cmd, std::ostream & ostr = std::cout, bool detailed=true) const ;
207
209
212 const std::string & resolveFull(const key_t & key) const;
213
214 template <class T>
215 void exportStatus(std::map<std::string, T> & statusMap) const;
216
217
219 // drain::Flagger::ivalue_t
220 bank_section_t scriptTriggerFlag;
221
222
223 inline
224 void setScriptTriggerFlag(bank_section_t sectionFlag){ // drain::Flagger::ivalue_t
225 scriptTriggerFlag = sectionFlag;
226 };
227
229
233 std::string defaultCmdKey;
234
236 inline
237 void setDefaultCmdKey(const std::string & s){
238 defaultCmdKey = s;
239 };
240
243
245
248 inline
249 void setNotFoundHandlerCmdKey(const std::string & s){
251 };
252
254 std::string scriptCmd;
255
257 /***
258 * For example: "script"
259 */
260 inline
261 void setScriptCmd(const std::string & s){
262 scriptCmd = s;
263 }
264
266 /***
267 * For example: "exec"
268 inline
269 void setScriptExecCmd(const std::string & s){
270 execScriptCmd = s;
271 }
272 */
273
274
276 /***
277 * For example: "execFile"
278 */
279 inline
280 void setScriptFileCmd(const std::string & s){
281 execFileCmd = s;
282 }
283
285 std::string execFileCmd;
286
287
288 template <class ...TT>
289 void linkRelatedCommands(const TT & ... cmds){
290 std::set<std::string> cmdList;
291 linkRelatedCommandList(cmdList, cmds...);
292 }
293
295 void linkRelatedCommandList(const std::set<std::string> & cmdList);
296
297
298protected:
299
300 template <class ...TT>
301 void linkRelatedCommandList(std::set<std::string> & cmdList, const std::string & cmd, const TT & ... cmds){
302 cmdList.insert(cmd);
303 linkRelatedCommandList(cmdList, cmds...);
304 }
305
306
307 template <class ...TT>
308 void linkRelatedCommandList(std::set<std::string> & cmdList, const char *cmd, const TT & ... cmds){
309 cmdList.insert(cmd);
310 linkRelatedCommandList(cmdList, cmds...);
311 }
312
313
314};
315
316template <class T>
317void CommandBank::exportStatus(std::map<std::string, T> & statusMap) const {
318 for (map_t::const_iterator it = this->begin(); it!=this->end(); ++it){
319 statusMap[it->first] = it->second->getSource().getParameters().getValues();
320 }
321}
322
323
325extern
326CommandBank & getCommandBank();
327
328
329} /* namespace drain */
330
331#endif
A Bank with additional support for brief, single char keys.
Definition Bank.h:315
K key_t
Public key type. (Key type used in the public interface.)
Definition Bank.h:68
Container and execution tools for commands derived from Command.
Definition CommandBank.h:56
void linkRelatedCommandList(const std::set< std::string > &cmdList)
Adds command keys, intelinking all the commands in the list.
Definition CommandBank.cpp:1023
void commitAddedCommands(Context &ctx, Program &prog, Program::iterator it) const
Run a single command.
Definition CommandBank.cpp:499
void scriptify(int argc, const char **argv, Script &script) const
Convert program arguments a script. Like in main(), actual command arguments start from 1.
Definition CommandBank.cpp:183
void append(const Script &script, Program &prog) const
Converts command strings to executable command objects, appending them to a program.
Definition CommandBank.cpp:141
void run(const std::string &key, Command &cmd, Context &ctx) const
Run a single command.
void setNotFoundHandlerCmdKey(const std::string &s)
Set command to be executed when a command - argument with leading hyphen(s) - is not found.
Definition CommandBank.h:249
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:242
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:285
static std::set< std::string > & trimWords()
Words that are moved from class name prior to composing a command name.
Definition CommandBank.cpp:58
void flushRoutine(Script &script, Context &ctx, Program &prog)
Append routine to program, and clear routine.
Definition CommandBank.h:162
bank_section_t scriptTriggerFlag
Member and methods for special commands.
Definition CommandBank.h:220
const std::string & resolveFull(const key_t &key) const
Given an alias or a long key, possibly prefixed by hyphens, return the long key if a command exists.
Definition CommandBank.cpp:1108
void readFile(const std::string &filename, Program &prog) const
Definition CommandBank.cpp:362
std::string defaultCmdKey
Command that is used, if a plain argument is given.
Definition CommandBank.h:233
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:949
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:254
void info(const std::string &key, const command_t &cmd, std::ostream &ostr=std::cout, bool detailed=true) const
Checked key and respective command.
Definition CommandBank.cpp:1054
void readFileTXT(const std::string &filename, Script &script) const
Include commands from file, inserting them before iterator.
Definition CommandBank.cpp:426
void setScriptFileCmd(const std::string &s)
Set command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:280
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:63
void setScriptCmd(const std::string &s)
Set command for storing a routine.
Definition CommandBank.h:261
void setDefaultCmdKey(const std::string &s)
Set command - key, not getName() - that is used if a plain argument is given.
Definition CommandBank.h:237
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition Command.h:53
Definition Context.h:55
Sequence consisting of Command:s retrieved from a CommandBank.
Definition Program.h:52
Definition CommandSequence.h:85
Definition StringMapper.h:114
Definition DataSelector.cpp:1277
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition CommandBank.cpp:52
Definition Cloner.h:46