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
52
53
54
56// in section S of the global command registry.
61template <char PREFIX=0, class SECTION=GeneralSection>
63public:
64
65 CommandBank & cmdBank;
66
67 CommandInstaller(CommandBank & bank = getCommandBank()) : cmdBank(bank){
68 getSection();
69 };
70
71 inline static
72 const CommandSection & getSection(){
73 return drain::Static::get<SECTION>();
74 };
75
77 static inline
78 char getPrefix(){
79 return PREFIX;
80 }
81
83 template <class CMD>
84 Command & install(const std::string & name, char alias = 0){
85 Command & cmd = cmdBank.add<CMD>(name,alias);
86 cmd.section |= getSection().index; // keep TRIGGER
87 return cmd;
88 }
89
91 template <class CMD>
92 Command & install(char alias = 0){
93 std::string name = CMD().getName();
94 CommandBank::deriveCmdName(name, PREFIX);
95 return install<CMD>(name, alias);
96 }
97
99
103 template <class CMD>
104 Command & install(CMD & cmdExt, const std::string & name, char alias = 0){
105 Command & cmd = cmdBank.addExternal(cmdExt, name,alias);// must give cmdExt, for copying
106 cmd.section |= getSection().index; // keep TRIGGER
107 return cmd;
108 }
109
111
115 template <class CMD>
116 Command & install(CMD & cmdExt, char alias = 0){
117 std::string name = cmdExt.getName();
118 CommandBank::deriveCmdName(name, PREFIX);
119 return install(cmdExt, name, alias);
120 }
121
122
124 template <class CMD>
125 static
126 Command & installShared(const std::string & name, char alias = 0){
127 Command & cmd = getCommandBank().add<CMD>(name,alias);
128 cmd.section |= getSection().index; // keep TRIGGER
129 return cmd;
130 }
131
133 template <class CMD>
134 static
135 Command & installShared(char alias = 0){
136 std::string name = CMD().getName();
137 CommandBank::deriveCmdName(name, PREFIX);
138 return installShared<CMD>(name, alias);
139 }
140
141
142
143};
144
145
147
152template <char PREFIX=0, class SECTION=GeneralSection>
153class CommandModule : protected CommandInstaller<PREFIX, SECTION>{
154
155public:
156
158 //typedef CommandInstaller<PREFIX, SECTION> base_t;
159
165 CommandModule(CommandBank & bank = getCommandBank()) : CommandInstaller<PREFIX, SECTION>(bank)
166 {
167
168 };
169
171
174 template <class CMD, class CMD_NEW = CMD>
175 Command & installDeprecating(const std::string & name, char alias = 0){
176 Command & cmd = this->cmdBank.template add<CMD>(name,alias);
177 cmd.section |= this->getSection().index; // keep TRIGGER
178
179 cmd.section |= drain::Static::get<drain::DeprecatingSection>().index;
180 //cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
181
182
183 std::string nameNew = CMD_NEW().getName();
184 CommandBank::deriveCmdName(nameNew, PREFIX);
185 cmd.linkRelated(nameNew);
186
187 return cmd;
188 }
189
190
192
195 template <class CMD, class CMD_NEW = CMD>
196 Command & installDeprecating(char alias = 0){
197 std::string name = CMD().getName();
198 CommandBank::deriveCmdName(name, PREFIX);
199 return installDeprecating<CMD,CMD_NEW>(name, alias);
200 }
201
202
203
207 template <class ...TT>
208 inline
209 void linkRelatedCommands(TT & ... cmds){
210 std::set<std::string> cmdList;
211 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
212 }
213
214 template <class ...TT>
215 inline
216 void linkRelatedCommandList(std::set<std::string> & cmdList, Command & cmd, TT & ... cmds){
217 std::string name = cmd.getName();
218 CommandBank::deriveCmdName(name, PREFIX);
219 cmdList.insert(name);
220 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
221 }
222
223 template <class ...TT>
224 inline
225 void linkRelatedCommandList(std::set<std::string> & cmdList, const std::string & cmdName, TT & ... cmds){
226 cmdList.insert(cmdName);
227 linkRelatedCommandList(cmdList, cmds...); // infinite loop?
228 }
229
230
231 template <class ...TT>
232 inline
233 void linkRelatedCommandList(std::set<std::string> & cmdList){
234 this->cmdBank.linkRelatedCommandList(cmdList); // infinite loop?
235 }
236
240 inline
241 // void linkRelatedSection(Command & cmd, const std::string & section){
242 void linkRelatedSection(Command & cmd, const CommandSection & section){
243 std::string name = cmd.getName();
244 CommandBank::deriveCmdName(name, PREFIX);
245 this->cmdBank.get(name).linkRelated(section); // cast to str
246 }
247
248
249};
250
251} // drain::
252
253
255#define DRAIN_CMD_INSTALL(prefix, cmd) drain::Command & cmd = install<prefix##cmd>
256
257
258#endif
259
260
const T & get(const K &key) const
Returns the base instance.
Definition Bank.h:160
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:887
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, deriving command name from the class name and prefixing if de...
Definition CommandInstaller.h:62
Command & install(char alias=0)
Install external command.
Definition CommandInstaller.h:92
Command & install(CMD &cmdExt, char alias=0)
Install external command.
Definition CommandInstaller.h:116
static char getPrefix()
Return the character used as prefix for the commands in this section.
Definition CommandInstaller.h:78
static Command & installShared(const std::string &name, char alias=0)
Install to shared (global) CommandBank.
Definition CommandInstaller.h:126
static Command & installShared(char alias=0)
Install to shared (global) CommandBank.
Definition CommandInstaller.h:135
Command & install(CMD &cmdExt, const std::string &name, char alias=0)
Install external command.
Definition CommandInstaller.h:104
Command & install(const std::string &name, char alias=0)
Install command initialized and stored by command bank.
Definition CommandInstaller.h:84
Combines command installation and sectioning.
Definition CommandInstaller.h:153
Command & installDeprecating(const std::string &name, char alias=0)
Mark deprecating commands by installing them with this.
Definition CommandInstaller.h:175
CommandModule(CommandBank &bank=getCommandBank())
Definition CommandInstaller.h:165
void linkRelatedSection(Command &cmd, const CommandSection &section)
Definition CommandInstaller.h:242
void linkRelatedCommands(TT &... cmds)
Definition CommandInstaller.h:209
Command & installDeprecating(char alias=0)
Mark deprecating commands by installing them with this.
Definition CommandInstaller.h:196
Definition CommandSections.h:54
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition Command.h:55
void linkRelated(const std::string &cmdKey) const
Add related command(s), to appear in help after "See-also:" phrase.
Definition Command.h:221
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