From 72acf695504fc8ca9de39ad67018c29796674c21 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sun, 21 Jan 2024 13:11:09 +0800 Subject: [PATCH] Update ApplicationSettings api. --- Universal/ApplicationSettings.cpp | 12 ++++++++---- Universal/ApplicationSettings.h | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Universal/ApplicationSettings.cpp b/Universal/ApplicationSettings.cpp index baf6cda..86cdaaa 100644 --- a/Universal/ApplicationSettings.cpp +++ b/Universal/ApplicationSettings.cpp @@ -3,8 +3,7 @@ #include #include -ApplicationSettings::ApplicationSettings(boost::asio::io_context &ioContext, const std::string &path) - : m_ioContext(ioContext), m_timer(ioContext), m_path(path) { +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); @@ -12,12 +11,17 @@ ApplicationSettings::ApplicationSettings(boost::asio::io_context &ioContext, con 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(1)); - m_timer.async_wait([this](const boost::system::error_code &error) { + 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; diff --git a/Universal/ApplicationSettings.h b/Universal/ApplicationSettings.h index a4b67ee..851cb11 100644 --- a/Universal/ApplicationSettings.h +++ b/Universal/ApplicationSettings.h @@ -1,7 +1,6 @@ #ifndef __APPLICATIONSETTINGS_H__ #define __APPLICATIONSETTINGS_H__ -#include #include #include @@ -20,7 +19,8 @@ class ApplicationSettings { #define BUILD_STATUS(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Status, Type, Name, DefaultValue) public: - ApplicationSettings(boost::asio::io_context &ioContext, const std::string &path); + ApplicationSettings(const std::string &path); + void startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds); protected: void run(); @@ -30,9 +30,9 @@ protected: mutable std::mutex m_mutex; private: - boost::asio::io_context &m_ioContext; - boost::asio::steady_timer m_timer; std::string m_path; + std::unique_ptr m_timer; + uint32_t m_interval; }; #endif // __APPLICATIONSETTINGS_H__ \ No newline at end of file