diff --git a/Universal/ApplicationSettings.cpp b/Universal/ApplicationSettings.cpp deleted file mode 100644 index 86cdaaa..0000000 --- a/Universal/ApplicationSettings.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "ApplicationSettings.h" -#include "BoostLog.h" -#include -#include - -ApplicationSettings::ApplicationSettings(const std::string &path) : m_path(path) { - if (std::filesystem::exists(std::filesystem::path(path))) { - try { - boost::property_tree::read_ini(path, m_ptree); - } catch (const boost::property_tree::ini_parser_error &e) { - LOG(error) << e.what(); - } - } -} - -void ApplicationSettings::startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds) { - m_timer = std::make_unique(ioContext); - m_interval = seconds; - run(); -} - -void ApplicationSettings::run() { - m_timer->expires_after(std::chrono::seconds(m_interval)); - m_timer->async_wait([this](const boost::system::error_code &error) { - if (error) { - LOG(error) << error.message(); - return; - } - if (m_needSave) { - try { - std::lock_guard locker(m_mutex); - boost::property_tree::write_ini(m_path, m_ptree); - } catch (const boost::property_tree::ini_parser_error &e) { - LOG(error) << e.what(); - } - m_needSave = false; - } - run(); - }); -} diff --git a/Universal/ApplicationSettings.h b/Universal/ApplicationSettings.h index 851cb11..789c7c0 100644 --- a/Universal/ApplicationSettings.h +++ b/Universal/ApplicationSettings.h @@ -1,9 +1,15 @@ #ifndef __APPLICATIONSETTINGS_H__ #define __APPLICATIONSETTINGS_H__ +#include "BoostLog.h" #include +#include +#include +#include #include +#include +template class ApplicationSettings { #define BUILD_SETTING_FIELD(Category, Type, Name, DefaultValue) \ inline void set##Name(const Type &value) { \ @@ -17,13 +23,59 @@ class ApplicationSettings { } #define BUILD_STATUS(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Status, Type, Name, DefaultValue) +#define BUILD_SETTING(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Settings, Type, Name, DefaultValue) +#define INITIALIZE_FIELD(Name) set##Name(get##Name()); + +#define MACRO(r, data, elem) INITIALIZE_FIELD(elem) + +#define INITIALIZE_FIELDS(...) \ + inline void initializeFileds() final { \ + BOOST_PP_SEQ_FOR_EACH(MACRO, _, BOOST_PP_TUPLE_TO_SEQ((__VA_ARGS__))) \ + } public: - ApplicationSettings(const std::string &path); - void startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds); + ApplicationSettings(const std::string &path) : m_path(path) { + if (std::filesystem::exists(path)) { + try { + boost::property_tree::read_ini(path, m_ptree); + } catch (const boost::property_tree::ini_parser_error &e) { + LOG(error) << e.what(); + } + } else { + static_cast(this)->initializeFileds(); + save(); + } + } + void startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds) { + m_timer = std::make_unique(ioContext); + m_interval = seconds; + run(); + } protected: - void run(); + void save() { + if (!m_needSave) return; + try { + std::lock_guard locker(m_mutex); + boost::property_tree::write_ini(m_path, m_ptree); + m_needSave = false; + } catch (const boost::property_tree::ini_parser_error &e) { + LOG(error) << e.what(); + } + } + void run() { + m_timer->expires_after(std::chrono::seconds(m_interval)); + m_timer->async_wait([this](const boost::system::error_code &error) { + if (error) { + LOG(error) << error.message(); + return; + } + save(); + run(); + }); + } + virtual void initializeFileds() { + } boost::property_tree::ptree m_ptree; bool m_needSave = false; diff --git a/Universal/CMakeLists.txt b/Universal/CMakeLists.txt index c733c52..3940a8b 100644 --- a/Universal/CMakeLists.txt +++ b/Universal/CMakeLists.txt @@ -1,7 +1,7 @@ find_package(Boost REQUIRED COMPONENTS log log_setup program_options) add_library(Universal - ApplicationSettings.h ApplicationSettings.cpp + ApplicationSettings.h BoostLog.h BoostLog.inl BoostLog.cpp BufferUtility.h BufferUtility.cpp DateTime.h DateTime.cpp