X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/46432ca916d65b4801a11f7b8196201bea6d2d2d..ebd170a764ab9660adee464588cda1801c7986b4:/projects/sgconf/actions.h diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index c88de14d..a078e6b2 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -18,17 +18,18 @@ * Author : Maxim Mamontov <faust@stargazer.dp.ua> */ -#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 <string> +#include <optional> + +#include <cassert> namespace SGCONF { @@ -39,17 +40,15 @@ template <typename F> class FUNC0_ACTION : public ACTION { public: - FUNC0_ACTION(const F & func) : m_func(func) {} - - virtual ACTION * Clone() const { return new FUNC0_ACTION<F>(*this); } + explicit FUNC0_ACTION(const F & func) : m_func(func) {} - 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) + 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: @@ -59,16 +58,16 @@ class FUNC0_ACTION : public ACTION template <typename F> inline -FUNC0_ACTION<F> * MakeFunc0Action(F func) +std::unique_ptr<ACTION> MakeFunc0Action(F func) { -return new FUNC0_ACTION<F>(func); +return std::make_unique<FUNC0_ACTION<F>>(func); } template <typename T> class PARAM_ACTION : public ACTION { public: - PARAM_ACTION(RESETABLE<T> & param, + PARAM_ACTION(std::optional<T> & param, const T & defaultValue, const std::string & paramDescription) : m_param(param), @@ -76,22 +75,25 @@ class PARAM_ACTION : public ACTION m_description(paramDescription), m_hasDefault(true) {} - PARAM_ACTION(RESETABLE<T> & param, + explicit PARAM_ACTION(std::optional<T> & param) + : m_param(param), + m_hasDefault(false) + {} + PARAM_ACTION(std::optional<T> & param, const std::string & paramDescription) : m_param(param), m_description(paramDescription), 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); + 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<T> & m_param; + std::optional<T> & m_param; T m_defaltValue; std::string m_description; bool m_hasDefault; @@ -102,7 +104,7 @@ template <typename T> inline std::string PARAM_ACTION<T>::DefaultDescription() const { -return m_hasDefault ? " (default: '" + x2str(m_defaltValue) + "')" +return m_hasDefault ? " (default: '" + std::to_string(m_defaltValue) + "')" : ""; } @@ -116,7 +118,7 @@ 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 || @@ -131,7 +133,34 @@ 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 || @@ -143,21 +172,66 @@ return PARSER_STATE(false, --argc, ++argv); template <typename T> inline -PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param, - const T & defaultValue, - const std::string & paramDescription) +std::unique_ptr<ACTION> MakeParamAction(std::optional<T> & param, + const T & defaultValue, + const std::string & paramDescription) { -return new PARAM_ACTION<T>(param, defaultValue, paramDescription); +return std::make_unique<PARAM_ACTION<T>>(param, defaultValue, paramDescription); } template <typename T> inline -PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param, - const std::string & paramDescription) +std::unique_ptr<ACTION> MakeParamAction(std::optional<T> & param) { -return new PARAM_ACTION<T>(param, paramDescription); +return std::make_unique<PARAM_ACTION<T>>(param); } -} // namespace SGCONF +template <typename T> +inline +std::unique_ptr<ACTION> MakeParamAction(std::optional<T> & param, + const std::string & paramDescription) +{ +return std::make_unique<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) + {} + + 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; + 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 +std::unique_ptr<ACTION> MakeKVAction(const std::string & name, + const std::string & paramDescription) +{ +return std::make_unique<KV_ACTION>(name, paramDescription); +} -#endif +} // namespace SGCONF