X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/df2bb45e41303b5132feb6b264caaa01c31b8bb5..078ab6852ff1674f99d03fd697018a96bdb12d81:/projects/sgconf/actions.h diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 739fbf14..08dc177e 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -30,34 +30,44 @@ #include <string> +#include <cassert> + namespace SGCONF { typedef void (* FUNC0)(); +template <typename F> 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<F>(*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 <typename F> inline -FUNC0_ACTION * MakeFunc0Action(FUNC0 func) +FUNC0_ACTION<F> * MakeFunc0Action(F func) { -return new FUNC0_ACTION(func); +return new FUNC0_ACTION<F>(func); } template <typename T> -class PARAM_ACTION: public ACTION +class PARAM_ACTION : public ACTION { public: PARAM_ACTION(RESETABLE<T> & param, @@ -68,6 +78,10 @@ class PARAM_ACTION: public ACTION m_description(paramDescription), m_hasDefault(true) {} + PARAM_ACTION(RESETABLE<T> & param) + : m_param(param), + m_hasDefault(false) + {} PARAM_ACTION(RESETABLE<T> & param, const std::string & paramDescription) : m_param(param), @@ -75,10 +89,13 @@ class PARAM_ACTION: public ACTION m_hasDefault(false) {} + virtual ACTION * Clone() const { return new PARAM_ACTION<T>(*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<T> & m_param; @@ -106,8 +123,12 @@ return m_hasDefault ? " (default: '" + m_defaltValue + "')" template <typename T> inline -PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION<T>::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,8 +138,39 @@ return PARSER_STATE(false, --argc, ++argv); template <> inline -PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION<bool>::Parse(int argc, char ** argv, void * /*data*/) +{ +m_param = true; +return PARSER_STATE(false, argc, argv); +} + +template <typename T> +inline +void PARAM_ACTION<T>::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<std::string>::ParseValue(const std::string & stringValue) { +m_param = stringValue; +} + +template <> +inline +PARSER_STATE PARAM_ACTION<std::string>::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); } @@ -132,6 +184,13 @@ PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param, return new PARAM_ACTION<T>(param, defaultValue, paramDescription); } +template <typename T> +inline +PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param) +{ +return new PARAM_ACTION<T>(param); +} + template <typename T> inline PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param, @@ -140,6 +199,48 @@ PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param, return new PARAM_ACTION<T>(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<std::string, std::string> & kvs = *static_cast<std::map<std::string, std::string>*>(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