From: Maksym Mamontov Date: Wed, 20 Jul 2022 20:39:09 +0000 (+0300) Subject: More std::jthread X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/479b8853c2ab18c98926a9369a03888021e9b986?ds=inline;hp=a500fb72810060e52d87ad2c2e4691531f0bcc5a More std::jthread --- diff --git a/projects/sgconf/action.h b/projects/sgconf/action.h index 940ea7f6..bd3555ed 100644 --- a/projects/sgconf/action.h +++ b/projects/sgconf/action.h @@ -18,8 +18,7 @@ * Author : Maxim Mamontov */ -#ifndef __STG_SGCONF_ACTION_H__ -#define __STG_SGCONF_ACTION_H__ +#pragma once #include #include @@ -37,21 +36,17 @@ class ACTION public: virtual ~ACTION() {} - virtual ACTION * Clone() const = 0; virtual std::string ParamDescription() const = 0; virtual std::string DefaultDescription() const = 0; virtual OPTION_BLOCK & Suboptions() = 0; virtual PARSER_STATE Parse(int argc, char ** argv, void * data = NULL) = 0; virtual void ParseValue(const std::string &) {} - class ERROR : public std::runtime_error + struct ERROR : std::runtime_error { - public: - ERROR(const std::string & message) - : std::runtime_error(message.c_str()) {} + explicit ERROR(const std::string & message) + : std::runtime_error(message.c_str()) {} }; }; } // namespace SGCONF - -#endif diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 3181a105..f23e566d 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -40,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: @@ -60,9 +58,9 @@ 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 @@ -77,7 +75,7 @@ class PARAM_ACTION : public ACTION m_description(paramDescription), m_hasDefault(true) {} - PARAM_ACTION(STG::Optional & param) + explicit PARAM_ACTION(STG::Optional & param) : m_param(param), m_hasDefault(false) {} @@ -88,13 +86,11 @@ class PARAM_ACTION : public ACTION 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: STG::Optional & m_param; @@ -176,26 +172,26 @@ return PARSER_STATE(false, --argc, ++argv); template inline -PARAM_ACTION * MakeParamAction(STG::Optional & param, - const T & defaultValue, - const std::string & paramDescription) +std::unique_ptr MakeParamAction(STG::Optional & param, + const T & defaultValue, + const std::string & paramDescription) { -return new PARAM_ACTION(param, defaultValue, paramDescription); +return std::make_unique>(param, defaultValue, paramDescription); } template inline -PARAM_ACTION * MakeParamAction(STG::Optional & param) +std::unique_ptr MakeParamAction(STG::Optional & param) { -return new PARAM_ACTION(param); +return std::make_unique>(param); } template inline -PARAM_ACTION * MakeParamAction(STG::Optional & param, - const std::string & paramDescription) +std::unique_ptr MakeParamAction(STG::Optional & param, + const std::string & paramDescription) { -return new PARAM_ACTION(param, paramDescription); +return std::make_unique>(param, paramDescription); } class KV_ACTION : public ACTION @@ -207,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; @@ -234,10 +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 diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h index f27715ca..7fc22bca 100644 --- a/projects/sgconf/api_action.h +++ b/projects/sgconf/api_action.h @@ -90,12 +90,10 @@ class API_ACTION : public ACTION m_funPtr(funPtr) {} - virtual ACTION * Clone() const { return new API_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: COMMANDS & m_commands; @@ -107,35 +105,35 @@ class API_ACTION : public ACTION }; inline -ACTION * MakeAPIAction(COMMANDS & commands, +std::unique_ptr MakeAPIAction(COMMANDS & commands, const std::string & paramDescription, const std::vector & params, API_FUNCTION funPtr) { -return new API_ACTION(commands, paramDescription, true, params, funPtr); +return std::make_unique(commands, paramDescription, true, params, funPtr); } inline -ACTION * MakeAPIAction(COMMANDS & commands, +std::unique_ptr MakeAPIAction(COMMANDS & commands, const std::vector & params, API_FUNCTION funPtr) { -return new API_ACTION(commands, "", false, params, funPtr); +return std::make_unique(commands, "", false, params, funPtr); } inline -ACTION * MakeAPIAction(COMMANDS & commands, +std::unique_ptr MakeAPIAction(COMMANDS & commands, const std::string & paramDescription, API_FUNCTION funPtr) { -return new API_ACTION(commands, paramDescription, true, funPtr); +return std::make_unique(commands, paramDescription, true, funPtr); } inline -ACTION * MakeAPIAction(COMMANDS & commands, +std::unique_ptr MakeAPIAction(COMMANDS & commands, API_FUNCTION funPtr) { -return new API_ACTION(commands, "", false, funPtr); +return std::make_unique(commands, "", false, funPtr); } } diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 97908195..3413dbd9 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -73,7 +73,7 @@ template class FUNC1_ADAPTER : public std::unary_function { public: - FUNC1_ADAPTER(R (*func)(A)) : m_func(func) {} + explicit FUNC1_ADAPTER(R (*func)(A)) : m_func(func) {} const R operator()(A arg) const { return (m_func)(arg); } private: R (*m_func)(A); @@ -160,12 +160,10 @@ class CONFIG_ACTION : public ACTION m_description(paramDescription) {} - virtual ACTION * Clone() const { return new CONFIG_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: SGCONF::CONFIG & m_config; @@ -227,11 +225,10 @@ else } } -inline -CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config, - const std::string & paramDescription) +std::unique_ptr MakeParamAction(SGCONF::CONFIG & config, + const std::string & paramDescription) { -return new CONFIG_ACTION(config, paramDescription); +return std::make_unique(config, paramDescription); } } // namespace SGCONF diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index e431bcb0..06378d1c 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -39,26 +39,26 @@ namespace template void ReadConfigFile(const std::string & filePath, void (C::* callback)(const std::string&, const std::string&), C * obj) { -std::ifstream stream(filePath.c_str()); -std::string line; -size_t num = 0; -while (std::getline(stream, line)) + std::ifstream stream(filePath.c_str()); + std::string line; + size_t num = 0; + while (std::getline(stream, line)) { - ++num; - line = Trim(line); - std::string::size_type pos = line.find_first_of('#'); - if (pos != std::string::npos) - line = line.substr(0, pos); - if (line.empty()) - continue; - pos = line.find_first_of('='); - if (pos == std::string::npos) + ++num; + line = Trim(line); + std::string::size_type pos = line.find_first_of('#'); + if (pos != std::string::npos) + line = line.substr(0, pos); + if (line.empty()) + continue; + pos = line.find_first_of('='); + if (pos == std::string::npos) { - std::ostringstream error; - error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'."; - throw std::runtime_error(error.str().c_str()); + std::ostringstream error; + error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'."; + throw std::runtime_error(error.str().c_str()); } - (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1))); + (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1))); } } @@ -72,184 +72,157 @@ using SGCONF::PARSER_STATE; OPTION::OPTION(const std::string & shortName, const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) : m_shortName(shortName), m_longName(longName), - m_action(action), + m_action(std::move(action)), m_description(description) { } OPTION::OPTION(const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) : m_longName(longName), - m_action(action), + m_action(std::move(action)), m_description(description) { } -OPTION::OPTION(const OPTION & rhs) - : m_shortName(rhs.m_shortName), - m_longName(rhs.m_longName), - m_action(rhs.m_action->Clone()), - m_description(rhs.m_description) -{ -} - -OPTION::~OPTION() -{ -delete m_action; -} - -OPTION & OPTION::operator=(const OPTION & rhs) -{ -m_shortName = rhs.m_shortName; -m_longName = rhs.m_longName; -m_action = rhs.m_action->Clone(); -m_description = rhs.m_description; -return *this; -} - void OPTION::Help(size_t level) const { -if (!m_action) - throw ERROR("Option is not defined."); -std::string indent(level, '\t'); -std::cout << indent; -if (!m_shortName.empty()) - std::cout << "-" << m_shortName << ", "; -std::cout << "--" << m_longName << " " << m_action->ParamDescription() - << "\t" << m_description << m_action->DefaultDescription() << "\n"; -m_action->Suboptions().Help(level); + if (!m_action) + throw ERROR("Option is not defined."); + std::string indent(level, '\t'); + std::cout << indent; + if (!m_shortName.empty()) + std::cout << "-" << m_shortName << ", "; + std::cout << "--" << m_longName << " " << m_action->ParamDescription() + << "\t" << m_description << m_action->DefaultDescription() << "\n"; + m_action->Suboptions().Help(level); } bool OPTION::Check(const char * arg) const { -if (arg == NULL) - return false; + if (arg == NULL) + return false; -if (*arg++ != '-') - return false; + if (*arg++ != '-') + return false; -if (*arg == '-') -{ - return m_longName == arg + 1; -} + if (*arg == '-') + return m_longName == arg + 1; -return m_shortName == arg; + return m_shortName == arg; } PARSER_STATE OPTION::Parse(int argc, char ** argv, void * data) { -if (!m_action) - throw ERROR("Option is not defined."); -try + if (!m_action) + throw ERROR("Option is not defined."); + try { - return m_action->Parse(argc, argv, data); + return m_action->Parse(argc, argv, data); } -catch (const ACTION::ERROR & ex) + catch (const ACTION::ERROR & ex) { - if (m_longName.empty()) - throw ERROR("-" + m_shortName + ": " + ex.what()); - else - throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what()) - : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); + if (m_longName.empty()) + throw ERROR("-" + m_shortName + ": " + ex.what()); + else + throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what()) + : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); } } void OPTION::ParseValue(const std::string & value) { -if (!m_action) - throw ERROR("Option is not defined."); -try + if (!m_action) + throw ERROR("Option is not defined."); + try { - return m_action->ParseValue(value); + return m_action->ParseValue(value); } -catch (const ACTION::ERROR & ex) + catch (const ACTION::ERROR & ex) { - throw ERROR(m_longName + ": " + ex.what()); + throw ERROR(m_longName + ": " + ex.what()); } } OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName, const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) { -m_options.push_back(OPTION(shortName, longName, action, description)); -return *this; + m_options.emplace_back(shortName, longName, std::move(action), description); + return *this; } OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) { -m_options.push_back(OPTION(longName, action, description)); -return *this; + m_options.emplace_back(longName, std::move(action), description); + return *this; } void OPTION_BLOCK::Help(size_t level) const { -if (m_options.empty()) - return; -if (!m_description.empty()) - std::cout << m_description << ":\n"; -std::for_each(m_options.begin(), - m_options.end(), - [&level](const auto& opt){ opt.Help(level + 1); }); + if (m_options.empty()) + return; + if (!m_description.empty()) + std::cout << m_description << ":\n"; + for (const auto& option : m_options) + option.Help(level + 1); } PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv, void * data) { -PARSER_STATE state(false, argc, argv); -if (state.argc == 0) - return state; -while (state.argc > 0 && !state.stop) + PARSER_STATE state(false, argc, argv); + if (state.argc == 0) + return state; + while (state.argc > 0 && !state.stop) { - const auto it = std::find_if(m_options.begin(), m_options.end(), [&state](const auto& opt){ return opt.Check(*state.argv); }); - if (it != m_options.end()) - state = it->Parse(--state.argc, ++state.argv, data); - else - break; + const auto it = std::find_if(m_options.begin(), m_options.end(), [&state](const auto& opt){ return opt.Check(*state.argv); }); + if (it != m_options.end()) + state = it->Parse(--state.argc, ++state.argv, data); + else + break; } -return state; + return state; } void OPTION_BLOCK::ParseFile(const std::string & filePath) { -if (access(filePath.c_str(), R_OK)) - throw ERROR("File '" + filePath + "' does not exists."); -ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this); + if (access(filePath.c_str(), R_OK)) + throw ERROR("File '" + filePath + "' does not exists."); + ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this); } void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value) { -for (std::vector