+class COMMAND_FUNCTOR
+{
+ public:
+ virtual ~COMMAND_FUNCTOR() {}
+ virtual bool operator()(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options) = 0;
+ virtual COMMAND_FUNCTOR * Clone() = 0;
+};
+
+class COMMAND
+{
+ public:
+ COMMAND(COMMAND_FUNCTOR * funPtr,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options)
+ : m_funPtr(funPtr->Clone()),
+ m_arg(arg),
+ m_options(options)
+ {}
+ COMMAND(const COMMAND & rhs)
+ : m_funPtr(rhs.m_funPtr->Clone()),
+ m_arg(rhs.m_arg),
+ m_options(rhs.m_options)
+ {}
+ ~COMMAND()
+ {
+ delete m_funPtr;
+ }
+ bool Execute(const SGCONF::CONFIG & config) const
+ {
+ return (*m_funPtr)(config, m_arg, m_options);
+ }
+
+ private:
+ COMMAND_FUNCTOR * m_funPtr;
+ std::string m_arg;
+ std::map<std::string, std::string> m_options;
+};
+
+class COMMANDS
+{
+ public:
+ void Add(COMMAND_FUNCTOR * funPtr,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
+ bool Execute(const SGCONF::CONFIG & config) const
+ {
+ std::list<COMMAND>::const_iterator it(m_commands.begin());
+ bool res = true;
+ while (it != m_commands.end() && res)
+ {
+ res = res && it->Execute(config);
+ ++it;
+ }
+ return res;
+ }
+ private:
+ std::list<COMMAND> m_commands;
+};
+
+class COMMAND_ACTION : public ACTION
+{
+ public:
+ COMMAND_ACTION(COMMANDS & commands,
+ const std::string & paramDescription,
+ bool needArgument,
+ const OPTION_BLOCK& suboptions,
+ COMMAND_FUNCTOR* funPtr)
+ : m_commands(commands),
+ m_description(paramDescription),
+ m_argument(needArgument ? "1" : ""), // Hack
+ m_suboptions(suboptions),
+ m_funPtr(funPtr)
+ {}
+ COMMAND_ACTION(COMMANDS & commands,
+ const std::string & paramDescription,
+ bool needArgument,
+ COMMAND_FUNCTOR* funPtr)
+ : m_commands(commands),
+ m_description(paramDescription),
+ m_argument(needArgument ? "1" : ""), // Hack
+ m_funPtr(funPtr)
+ {}
+ COMMAND_ACTION(const COMMAND_ACTION& rhs)
+ : m_commands(rhs.m_commands),
+ m_description(rhs.m_description),
+ m_argument(rhs.m_argument),
+ m_suboptions(rhs.m_suboptions),
+ m_params(rhs.m_params),
+ m_funPtr(rhs.m_funPtr->Clone())
+ {
+ }
+
+ ~COMMAND_ACTION()
+ {
+ delete m_funPtr;
+ }
+
+ virtual ACTION * Clone() const { return new COMMAND_ACTION(*this); }
+
+ virtual std::string ParamDescription() const { return m_description; }
+ virtual std::string DefaultDescription() const { return ""; }
+ virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
+ virtual PARSER_STATE Parse(int argc, char ** argv)
+ {
+ PARSER_STATE state(false, argc, argv);
+ if (!m_argument.empty())
+ {
+ if (argc == 0 ||
+ argv == NULL ||
+ *argv == NULL)
+ throw ERROR("Missing argument.");
+ m_argument = *argv;
+ --state.argc;
+ ++state.argv;
+ }
+ m_suboptions.Parse(state.argc, state.argv);
+ m_commands.Add(m_funPtr, m_argument, m_params);
+ return state;
+ }
+
+ private:
+ COMMANDS & m_commands;
+ std::string m_description;
+ std::string m_argument;
+ OPTION_BLOCK m_suboptions;
+ std::map<std::string, std::string> m_params;
+ COMMAND_FUNCTOR* m_funPtr;
+};
+