40 #include <drain/Log.h>
41 #include "drain/util/BeanLike.h"
42 #include "drain/util/Flags.h"
43 #include "drain/util/ReferenceMap.h"
44 #include "drain/util/VariableMap.h"
64 Command(
const Command & cmd) : section(cmd.section), lastParameters(cmd.lastParameters){
75 const std::string & getName()
const = 0;
78 const std::string & getDescription()
const = 0;
90 void setParameters(
const std::string & args)
final;
94 void setParameters(
const T & args){
97 std::stringstream sstr;
99 for (
const auto & entry: args){
100 parameters[entry.first] = entry.second;
106 sstr << entry.first <<
'=' << entry.second;
108 lastParameters = sstr.str();
119 getParameters()[key] = value;
120 std::stringstream sstr;
121 sstr << key <<
'=' << value;
122 lastParameters = sstr.str();
130 bool hasParameters()
const {
132 Logger mout(__FILE__, __FUNCTION__);
137 const ReferenceMap::const_iterator it = params.begin();
138 if (it == params.end())
141 return it->second.getType() !=
typeid(void);
147 static const std::string empty;
157 void run(
const std::string & params =
"") final {
158 setParameters(params);
190 const std::string & getLastParameters()
const {
191 return lastParameters;
197 ReferenceMap & getParameters() = 0;
200 std::string lastParameters;
205 std::ostream & operator<<(std::ostream & ostr,
const Command &cmd){
206 ostr << cmd.getName();
207 if (cmd.hasParameters()){
208 ostr <<
' ' << cmd.getParameters();
269 template <
class B,
class B2=B>
278 BeanCommand(
const std::string & name,
const std::string & description) : bean(name, description){
297 const bean_t & getBean()
const {
310 const std::string & getName()
const final {
311 return getBean().getName();
315 const std::string & getDescription()
const final {
316 return getBean().getDescription();
321 return getBean().getParameters();
328 return getBean().getParameters();
389 BasicCommand(
const std::string & name,
const std::string & description);
408 template <
class T = std::
string>
415 SimpleCommand(
const std::string & name,
const std::string & description,
416 const std::string & key=
"value",
const T & initValue = T(),
const std::string & unit =
"") :
BasicCommand(name, description) {
419 getParameters().link(key, value = initValue, unit);
422 Context & ctx = getContext<Context>();
424 mout.
warn(
"Empty value key in command ", name,
" (", description,
")");
433 const std::string & key, std::initializer_list<S> l ,
const std::string & unit =
"") :
BasicCommand(name, description) {
436 getParameters().link(key, value);
439 Context & ctx = getContext<Context>();
441 mout.
warn(
"Empty value key in command ", name,
" (", description,
")");
448 getParameters().
copyStruct(cmd.getParameters(), cmd, *
this);
Simple implementation of Command: adds name , description and parameters .
Definition: Command.h:385
Retrieves bean dynamically for each call.
Definition: Command.h:270
Definition: Command.h:359
Base class for commands: typically actions taking parameters but also plain variable assignments and ...
Definition: Command.h:55
int cmd_section_type
Definition: Command.h:173
virtual void run(const std::string ¶ms="") final
Convenience. Sets parameters and executes the command.
Definition: Command.h:157
virtual void exec() const
Run the command with current parameter values.
Definition: Command.h:173
void setParameter(const std::string &key, const T &value)
Set a value to a single parameter.
Definition: Command.h:118
virtual const std::string & getType() const
Description of result, comparable to a return type of a function. ?
Definition: Command.h:146
virtual void update()
Optional method for preparing command to execution.
Definition: Command.h:168
Definition: Context.h:252
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:428
Definition: ReferenceMap.h:207
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition: ReferenceMap.h:399
A single-parameter command.
Definition: Command.h:409
SimpleCommand(const std::string &name, const std::string &description, const std::string &key, std::initializer_list< S > l, const std::string &unit="")
Constuctor designer for SimpleCommand<Unituple<> > .
Definition: Command.h:432
char separator
Default character used for splitting input and output. See setValues.
Definition: SmartMap.h:85
Definition: DataSelector.cpp:1277
Definition: Sprinter.h:137