X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/0c109e9e61ae3624f30621b5fd328f354bfa6524..a5cb4cf37e5dfa9bb9ce5c5e4ccf8d5978d3576f:/projects/sgconf/actions.h diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 739fbf14..3181a105 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -18,49 +18,58 @@ * Author : Maxim Mamontov */ -#ifndef __STG_SGCONF_ACTIONS_H__ -#define __STG_SGCONF_ACTIONS_H__ +#pragma once #include "action.h" #include "options.h" #include "parser_state.h" #include "stg/common.h" -#include "stg/resetable.h" +#include "stg/optional.h" #include +#include + namespace SGCONF { typedef void (* FUNC0)(); +template class FUNC0_ACTION : public ACTION { public: - FUNC0_ACTION(FUNC0 func) : m_func(func) {} + FUNC0_ACTION(const F & func) : m_func(func) {} + + virtual ACTION * Clone() const { return new FUNC0_ACTION(*this); } virtual std::string ParamDescription() const { return ""; } virtual std::string DefaultDescription() const { return ""; } virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) + { + m_func(); + return PARSER_STATE(true, argc, argv); + } private: - FUNC0 m_func; + F m_func; OPTION_BLOCK m_suboptions; }; +template inline -FUNC0_ACTION * MakeFunc0Action(FUNC0 func) +FUNC0_ACTION * MakeFunc0Action(F func) { -return new FUNC0_ACTION(func); +return new FUNC0_ACTION(func); } template -class PARAM_ACTION: public ACTION +class PARAM_ACTION : public ACTION { public: - PARAM_ACTION(RESETABLE & param, + PARAM_ACTION(STG::Optional & param, const T & defaultValue, const std::string & paramDescription) : m_param(param), @@ -68,20 +77,27 @@ class PARAM_ACTION: public ACTION m_description(paramDescription), m_hasDefault(true) {} - PARAM_ACTION(RESETABLE & param, + PARAM_ACTION(STG::Optional & param) + : m_param(param), + m_hasDefault(false) + {} + PARAM_ACTION(STG::Optional & param, const std::string & paramDescription) : m_param(param), m_description(paramDescription), m_hasDefault(false) {} + virtual ACTION * Clone() const { return new PARAM_ACTION(*this); } + virtual std::string ParamDescription() const { return m_description; } virtual std::string DefaultDescription() const; virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/); + virtual void ParseValue(const std::string & value); private: - RESETABLE & m_param; + STG::Optional & m_param; T m_defaltValue; std::string m_description; bool m_hasDefault; @@ -92,7 +108,7 @@ template inline std::string PARAM_ACTION::DefaultDescription() const { -return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')" +return m_hasDefault ? " (default: '" + std::to_string(m_defaltValue) + "')" : ""; } @@ -106,8 +122,12 @@ return m_hasDefault ? " (default: '" + m_defaltValue + "')" template inline -PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) { +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); T value; if (str2x(*argv, value)) throw ERROR(std::string("Bad argument: '") + *argv + "'"); @@ -117,15 +137,46 @@ return PARSER_STATE(false, --argc, ++argv); template <> inline -PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) +{ +m_param = true; +return PARSER_STATE(false, argc, argv); +} + +template +inline +void PARAM_ACTION::ParseValue(const std::string & stringValue) +{ +if (stringValue.empty()) + throw ERROR("Missing value."); +T value; +if (str2x(stringValue, value)) + throw ERROR(std::string("Bad value: '") + stringValue + "'"); +m_param = value; +} + +template <> +inline +void PARAM_ACTION::ParseValue(const std::string & stringValue) { +m_param = stringValue; +} + +template <> +inline +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); m_param = *argv; return PARSER_STATE(false, --argc, ++argv); } template inline -PARAM_ACTION * MakeParamAction(RESETABLE & param, +PARAM_ACTION * MakeParamAction(STG::Optional & param, const T & defaultValue, const std::string & paramDescription) { @@ -134,12 +185,59 @@ return new PARAM_ACTION(param, defaultValue, paramDescription); template inline -PARAM_ACTION * MakeParamAction(RESETABLE & param, +PARAM_ACTION * MakeParamAction(STG::Optional & param) +{ +return new PARAM_ACTION(param); +} + +template +inline +PARAM_ACTION * MakeParamAction(STG::Optional & param, const std::string & paramDescription) { return new PARAM_ACTION(param, paramDescription); } -} // namespace SGCONF +class KV_ACTION : public ACTION +{ + public: + KV_ACTION(const std::string & name, + const std::string & paramDescription) + : m_name(name), + m_description(paramDescription) + {} + + virtual ACTION * Clone() const { return new KV_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, void * data); + + private: + std::string m_name; + std::string m_description; + OPTION_BLOCK m_suboptions; +}; -#endif +inline +PARSER_STATE KV_ACTION::Parse(int argc, char ** argv, void * data) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); +assert(data != NULL && "Expecting container pointer."); +std::map & kvs = *static_cast*>(data); +kvs[m_name] = *argv; +return PARSER_STATE(false, --argc, ++argv); +} + +inline +KV_ACTION * MakeKVAction(const std::string & name, + const std::string & paramDescription) +{ +return new KV_ACTION(name, paramDescription); +} + +} // namespace SGCONF