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
49/*
50class ExtendedContext {
51
52public:
53
54 Program addedProgram;
55
56};
57*/
58
59typedef int bank_section_t;
60
62
67class CommandBank : public BankSuper<Command> { // BankSuper<BasicCommand> {
68
69public:
70
71 typedef data_t command_t;
72
74 };
75
76 // TODO: CHECK: base BankSuper<Command> (which holds the registered commands themselves, and
77 // has its own working copy constructor) is explicitly default-constructed (empty) here, same
78 // as before, per the pre-existing "Copy sections?" doubt below - a copied CommandBank would
79 // have no registered commands/sections. CommandBank is used as a singleton via
80 // getCommandBank() everywhere in this codebase, so this is currently dormant, but confirm.
82 // Copy sections?
83 };
84
85
87 static
88 std::set<std::string> & trimWords();
89
91 static
92 void deriveCmdName(std::string & name, char prefix=0);
93
95
98 template <class D, char PREFIX=0>
99 D & addExternal(const D & entry, char alias = 0){
100 std::string key(entry.getName());
101 deriveCmdName(key, PREFIX);
102 //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
103 return BankSuper<Command>::addExternal(entry, key, alias);
104 }
105
106 // Template arg PREFIX is tricky, sometimes need for dynamic
108
111 template <class D>
112 D & addExternal(char PREFIX, const D & entry, char alias=0){
113 std::string key(entry.getName());
114 deriveCmdName(key, PREFIX);
115 return BankSuper<Command>::addExternal(entry, key, alias);
116 }
117
119
122 template <class D, char PREFIX=0>
123 inline
124 D & addExternal(const D & entry, const std::string & key, char alias = 0){
125 //return BankSuper<Command>::template addExternal<D>(entry, key, alias);
126 return BankSuper<Command>::addExternal(entry, key, alias);
127 }
128
129 // NEW 2026
130 /* Add a command
131 * An instance (entry) is needed for bean ref command, for example.
132 template <class D>
133 D & addExternal(char PREFIX, char alias=0){
134 static const D entry;
135 std::string key = entry.getName();
136 deriveCmdName(key, PREFIX);
137 return BankSuper<Command>::addExternal(entry, key, alias);
138 }
139 */
140
142 // void includeFile(const std::string & filename, Program & prog, Program::iterator it) const;
143
145
147
150 // static
151 void readFileTXT(const std::string & filename, Script & script) const;
152
153 void readFile(const std::string & filename, Program & prog) const;
154
156
159 void scriptify(int argc, const char **argv, Script & script) const;
160
162
165 //static
166 void scriptify(const std::string & cmdLine, Script & script) const;
167
169
174 bool scriptify(const std::string & arg, const std::string & argNext, Script & script) const;
175
176
178
181 // void append(const Script & script, Program & prog, Context & context) const ;
182 void append(const Script & script, Program & prog) const ;
183
184protected:
185
186 void append(const Script & script, Context & ctx, Program & prog) const ;
187
188
189 static
190 void tokenize(const std::string & cmdLine, std::list<std::string> & args);
191
192public:
193
194 drain::StringMapper logFileSyntax; // todo:setter
195
197 inline
198 void flushRoutine(Script & script, Context & ctx, Program & prog){
199 append(script, ctx, prog);
200 script.clear();
201 }
202
203
204 void remove(Program & prog) const;
205
207 // void run(const std::string & cmd, const std::string & params, Context & ctx);
208
210
215 void run(const std::string & key, Command & cmd, Context & ctx) const;
216
217 void commitAddedCommands(Context & ctx, Program & prog, Program::iterator it) const;
219 //void run(ScriptTxt & script, drain::Context & context);
220 // void run(Script & script, ClonerBase<Context> & contextSrc);
221
222 // Newish
223 void run(Program & prog, ClonerBase<Context> & contextCloner); // !! May be RackContext!
224
225 // experimental
226 void traverseLoops(Context & ctx, ClonerBase<Context> & contextCloner);
227
228 // experimental
229 void traverseLoops(Context & ctx, ClonerBase<Context> & contextCloner, Loop::loopStack::const_iterator it, Loop::loopStack::const_iterator itEnd);
230
231
233 void help(std::ostream & ostr = std::cout);
234
236 void help(FlagResolver::ivalue_t sectionFilter, std::ostream & ostr = std::cout); // Flagger::ivalue_t sectionFilter
237
239 void help(const std::string & key, std::ostream & ostr = std::cout);
240
242 void info(const std::string & key, const command_t & cmd, std::ostream & ostr = std::cout, bool detailed=true) const ;
243
245
248 const std::string & resolveFull(const key_t & key) const;
249
250 template <class T>
251 void exportStatus(std::map<std::string, T> & statusMap) const;
252
253
255 // drain::Flagger::ivalue_t
257
258
259 inline
260 void setScriptTriggerFlag(bank_section_t sectionFlag){ // drain::Flagger::ivalue_t
261 scriptTriggerFlag = sectionFlag;
262 };
263
265
269 std::string defaultCmdKey;
270
272 inline
273 void setDefaultCmdKey(const std::string & s){
274 defaultCmdKey = s;
275 };
276
279
281
284 inline
285 void setNotFoundHandlerCmdKey(const std::string & s){
287 };
288
290 std::string scriptCmd;
291
293 /***
294 * For example: "script"
295 */
296 inline
297 void setScriptCmd(const std::string & s){
298 scriptCmd = s;
299 }
300
302 /***
303 * For example: "exec"
304 inline
305 void setScriptExecCmd(const std::string & s){
306 execScriptCmd = s;
307 }
308 */
309
310
312 /***
313 * For example: "execFile"
314 */
315 inline
316 void setScriptFileCmd(const std::string & s){
317 execFileCmd = s;
318 }
319
321 std::string execFileCmd;
322
323
324 template <class ...TT>
325 void linkRelatedCommands(const TT & ... cmds){
326 std::set<std::string> cmdList;
327 linkRelatedCommandList(cmdList, cmds...);
328 }
329
331 void linkRelatedCommandList(const std::set<std::string> & cmdList);
332
333
334protected:
335
336 template <class ...TT>
337 void linkRelatedCommandList(std::set<std::string> & cmdList, const std::string & cmd, const TT & ... cmds){
338 cmdList.insert(cmd);
339 linkRelatedCommandList(cmdList, cmds...);
340 }
341
342
343 template <class ...TT>
344 void linkRelatedCommandList(std::set<std::string> & cmdList, const char *cmd, const TT & ... cmds){
345 cmdList.insert(cmd);
346 linkRelatedCommandList(cmdList, cmds...);
347 }
348
349
350};
351
352template <class T>
353void CommandBank::exportStatus(std::map<std::string, T> & statusMap) const {
354 for (map_t::const_iterator it = this->begin(); it!=this->end(); ++it){
355 statusMap[it->first] = it->second->getSource().getParameters().getValues();
356 }
357}
358
359
361extern
362CommandBank & getCommandBank();
363
364
365} /* namespace drain */
366
367#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:67
D & addExternal(char PREFIX, const D &entry, char alias=0)
Add a command.
Definition CommandBank.h:112
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:285
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:278
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:321
static std::set< std::string > & trimWords()
Words that should be removed from class name when automatically 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:198
D & addExternal(const D &entry, char alias=0)
Add a command.
Definition CommandBank.h:99
bank_section_t scriptTriggerFlag
Member and methods for special commands.
Definition CommandBank.h:256
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
D & addExternal(const D &entry, const std::string &key, char alias=0)
Add a command.
Definition CommandBank.h:124
std::string defaultCmdKey
Command that is used, if a plain argument is given.
Definition CommandBank.h:269
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:290
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:316
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:297
void setDefaultCmdKey(const std::string &s)
Set command - key, not getName() - that is used if a plain argument is given.
Definition CommandBank.h:273
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 Data.h:1473
int bank_section_t
Class to be used as one base class.
Definition CommandBank.h:59
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition CommandBank.cpp:52
Definition Cloner.h:46