CommandInstaller.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
50 namespace drain {
51 
53 // in section S of the global command registry.
58 /*
59 template <class C> //, char PREFIX=0, int SECTIONS=1>
60 class CommandWrapper : public C {
61 public:
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.
79 template <char PREFIX=0, class SECTION=GeneralSection>
80 class CommandInstaller { //: public CMD {
81 public:
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 
100 
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 
108  template <class CMD>
109  Command & install(char alias = 0){
110  std::string name = CMD().getName();
111  CommandBank::deriveCmdName(name, PREFIX);
112  return install<CMD>(name, alias);
113  }
114 
115  template <class CMD>
116  Command & installExternal(CMD & cmdExt, const std::string & name, char alias = 0){
117  Command & cmd = cmdBank.addExternal(cmdExt, name,alias);// must give cmdExt, for copying
118  cmd.section |= getSection().index; // keep TRIGGER
119  return cmd;
120  }
121 
122  template <class CMD>
123  Command & installExternal(CMD & cmdExt, char alias = 0){
124  std::string name = cmdExt.getName();
125  CommandBank::deriveCmdName(name, PREFIX);
126  //Command & cmd = bank.addExternal(cmdExt, name,alias); // must give cmdExt, for copying
127  //cmd.section = getSection().index;
128  return installExternal(cmdExt, name, alias);
129  }
130 
132  template <class CMD>
133  static
134  Command & installShared(const std::string & name, char alias = 0){
135  Command & cmd = getCommandBank().add<CMD>(name,alias);
136  cmd.section |= getSection().index; // keep TRIGGER
137  return cmd;
138  }
139 
141  template <class CMD>
142  static
143  Command & installShared(char alias = 0){
144  std::string name = CMD().getName();
145  CommandBank::deriveCmdName(name, PREFIX);
146  return installShared<CMD>(name, alias);
147  }
148 
149 };
150 
151 
153 
158 template <char PREFIX=0, class SECTION=GeneralSection>
159 class CommandModule : protected CommandInstaller<PREFIX, SECTION>{
160 
161 public:
162 
164  //typedef CommandInstaller<PREFIX, SECTION> base_t;
165 
171  CommandModule(CommandBank & bank = getCommandBank()) : CommandInstaller<PREFIX, SECTION>(bank)
172  {
173 
174  };
175 
176  // CommandModule(const std::string & name, CommandBank & bank = getCommandBank()) :
177  // CommandInstaller<PREFIX, SECTION>(bank), name(name)
178 
179  // virtual void initialize() = 0;
180 
182  // CommandInstaller<> installer;
183  // const std::string name;
184 
185 
186 };
187 
188 }
189 
190 
191 
192 /* namespace drain */
193 
194 #endif /* DRAINLET_H_ */
195 
196 // Rack
Container and execution tools for commands derived from Command.
Definition: CommandBank.h:54
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
static Command & installShared(const std::string &name, char alias=0)
Install to shared CommandBank.
Definition: CommandInstaller.h:134
static Command & installShared(char alias=0)
Install to shared CommandBank.
Definition: CommandInstaller.h:143
static char getPrefix()
Return the character used as prefix for the commands in this section.
Definition: CommandInstaller.h:96
Combines command installation and sectioning.
Definition: CommandInstaller.h:159
CommandModule(CommandBank &bank=getCommandBank())
Definition: CommandInstaller.h:171
Definition: CommandSections.h:54
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition: Command.h:54
int section
Definition: Command.h:213
Definition: DataSelector.cpp:1277
CommandBank & getCommandBank()
Global program command registry. Optional utility.
Definition: CommandBank.cpp:51