X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/d084d9ae9f7bcd7f7d1926e7eeae921dbad49273..a91e9542b384905187890f161d4da5396996fcfd:/projects/sgconf/actions.h?ds=sidebyside diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 07bcfb8b..a078e6b2 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -18,17 +18,16 @@ * 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 +#include #include @@ -41,17 +40,15 @@ template class FUNC0_ACTION : public ACTION { public: - FUNC0_ACTION(const F & func) : m_func(func) {} + explicit 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, void * /*data*/) + std::string ParamDescription() const override { return ""; } + std::string DefaultDescription() const override { return ""; } + OPTION_BLOCK & Suboptions() override { return m_suboptions; } + PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override { - m_func(); - return PARSER_STATE(true, argc, argv); + m_func(); + return PARSER_STATE(true, argc, argv); } private: @@ -61,16 +58,16 @@ class FUNC0_ACTION : public ACTION template inline -FUNC0_ACTION * MakeFunc0Action(F func) +std::unique_ptr MakeFunc0Action(F func) { -return new FUNC0_ACTION(func); +return std::make_unique>(func); } template class PARAM_ACTION : public ACTION { public: - PARAM_ACTION(RESETABLE & param, + PARAM_ACTION(std::optional & param, const T & defaultValue, const std::string & paramDescription) : m_param(param), @@ -78,23 +75,25 @@ class PARAM_ACTION : public ACTION m_description(paramDescription), m_hasDefault(true) {} - PARAM_ACTION(RESETABLE & param, + explicit PARAM_ACTION(std::optional & param) + : m_param(param), + m_hasDefault(false) + {} + PARAM_ACTION(std::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, void * /*data*/); - virtual void ParseValue(const std::string & value); + std::string ParamDescription() const override { return m_description; } + std::string DefaultDescription() const override; + OPTION_BLOCK & Suboptions() override { return m_suboptions; } + PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override; + void ParseValue(const std::string & value) override; private: - RESETABLE & m_param; + std::optional & m_param; T m_defaltValue; std::string m_description; bool m_hasDefault; @@ -105,7 +104,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) + "')" : ""; } @@ -132,6 +131,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) @@ -165,19 +172,26 @@ return PARSER_STATE(false, --argc, ++argv); template inline -PARAM_ACTION * MakeParamAction(RESETABLE & param, - const T & defaultValue, - const std::string & paramDescription) +std::unique_ptr MakeParamAction(std::optional & param, + const T & defaultValue, + const std::string & paramDescription) +{ +return std::make_unique>(param, defaultValue, paramDescription); +} + +template +inline +std::unique_ptr MakeParamAction(std::optional & param) { -return new PARAM_ACTION(param, defaultValue, paramDescription); +return std::make_unique>(param); } template inline -PARAM_ACTION * MakeParamAction(RESETABLE & param, - const std::string & paramDescription) +std::unique_ptr MakeParamAction(std::optional & param, + const std::string & paramDescription) { -return new PARAM_ACTION(param, paramDescription); +return std::make_unique>(param, paramDescription); } class KV_ACTION : public ACTION @@ -189,12 +203,10 @@ class KV_ACTION : public ACTION 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); + std::string ParamDescription() const override { return m_description; } + std::string DefaultDescription() const override { return ""; } + OPTION_BLOCK & Suboptions() override { return m_suboptions; } + PARSER_STATE Parse(int argc, char ** argv, void * data) override; private: std::string m_name; @@ -216,12 +228,10 @@ return PARSER_STATE(false, --argc, ++argv); } inline -KV_ACTION * MakeKVAction(const std::string & name, - const std::string & paramDescription) +std::unique_ptr MakeKVAction(const std::string & name, + const std::string & paramDescription) { -return new KV_ACTION(name, paramDescription); +return std::make_unique(name, paramDescription); } } // namespace SGCONF - -#endif