X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/289d396c8d0684e23c2233291164febfec3947bf..dd6144fb73127c6716b973544e48dbbd0bb29a69:/projects/sgconf/actions.h diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 33a11347..08dc177e 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -30,6 +30,8 @@ #include +#include + namespace SGCONF { @@ -46,7 +48,7 @@ class FUNC0_ACTION : public ACTION 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); @@ -76,6 +78,10 @@ class PARAM_ACTION : public ACTION m_description(paramDescription), m_hasDefault(true) {} + PARAM_ACTION(RESETABLE & param) + : m_param(param), + m_hasDefault(false) + {} PARAM_ACTION(RESETABLE & param, const std::string & paramDescription) : m_param(param), @@ -88,7 +94,7 @@ class PARAM_ACTION : public ACTION 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: @@ -117,7 +123,7 @@ 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 || @@ -130,6 +136,14 @@ m_param = value; return PARSER_STATE(false, --argc, ++argv); } +template <> +inline +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) @@ -151,7 +165,7 @@ m_param = stringValue; 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 || @@ -170,6 +184,13 @@ PARAM_ACTION * MakeParamAction(RESETABLE & param, return new PARAM_ACTION(param, defaultValue, paramDescription); } +template +inline +PARAM_ACTION * MakeParamAction(RESETABLE & param) +{ +return new PARAM_ACTION(param); +} + template inline PARAM_ACTION * MakeParamAction(RESETABLE & param, @@ -178,6 +199,48 @@ PARAM_ACTION * MakeParamAction(RESETABLE & param, return new PARAM_ACTION(param, paramDescription); } +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; +}; + +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 #endif