Update ApplicationSettings api.

This commit is contained in:
amass 2024-01-21 13:11:09 +08:00
parent e05d90f1b3
commit 72acf69550
2 changed files with 12 additions and 8 deletions

View File

@ -3,8 +3,7 @@
#include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ini_parser.hpp>
#include <filesystem> #include <filesystem>
ApplicationSettings::ApplicationSettings(boost::asio::io_context &ioContext, const std::string &path) ApplicationSettings::ApplicationSettings(const std::string &path) : m_path(path) {
: m_ioContext(ioContext), m_timer(ioContext), m_path(path) {
if (std::filesystem::exists(std::filesystem::path(path))) { if (std::filesystem::exists(std::filesystem::path(path))) {
try { try {
boost::property_tree::read_ini(path, m_ptree); boost::property_tree::read_ini(path, m_ptree);
@ -12,12 +11,17 @@ ApplicationSettings::ApplicationSettings(boost::asio::io_context &ioContext, con
LOG(error) << e.what(); LOG(error) << e.what();
} }
} }
}
void ApplicationSettings::startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds) {
m_timer = std::make_unique<boost::asio::steady_timer>(ioContext);
m_interval = seconds;
run(); run();
} }
void ApplicationSettings::run() { void ApplicationSettings::run() {
m_timer.expires_after(std::chrono::seconds(1)); m_timer->expires_after(std::chrono::seconds(m_interval));
m_timer.async_wait([this](const boost::system::error_code &error) { m_timer->async_wait([this](const boost::system::error_code &error) {
if (error) { if (error) {
LOG(error) << error.message(); LOG(error) << error.message();
return; return;

View File

@ -1,7 +1,6 @@
#ifndef __APPLICATIONSETTINGS_H__ #ifndef __APPLICATIONSETTINGS_H__
#define __APPLICATIONSETTINGS_H__ #define __APPLICATIONSETTINGS_H__
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
@ -20,7 +19,8 @@ class ApplicationSettings {
#define BUILD_STATUS(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Status, Type, Name, DefaultValue) #define BUILD_STATUS(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Status, Type, Name, DefaultValue)
public: 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: protected:
void run(); void run();
@ -30,9 +30,9 @@ protected:
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
private: private:
boost::asio::io_context &m_ioContext;
boost::asio::steady_timer m_timer;
std::string m_path; std::string m_path;
std::unique_ptr<boost::asio::steady_timer> m_timer;
uint32_t m_interval;
}; };
#endif // __APPLICATIONSETTINGS_H__ #endif // __APPLICATIONSETTINGS_H__