update ApplicationSettings.

This commit is contained in:
amass 2024-01-21 18:08:16 +08:00
parent 72acf69550
commit e81337c3aa
3 changed files with 56 additions and 44 deletions

View File

@ -1,40 +0,0 @@
#include "ApplicationSettings.h"
#include "BoostLog.h"
#include <boost/property_tree/ini_parser.hpp>
#include <filesystem>
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<boost::asio::steady_timer>(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();
});
}

View File

@ -1,9 +1,15 @@
#ifndef __APPLICATIONSETTINGS_H__ #ifndef __APPLICATIONSETTINGS_H__
#define __APPLICATIONSETTINGS_H__ #define __APPLICATIONSETTINGS_H__
#include "BoostLog.h"
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <filesystem>
template <typename Child>
class ApplicationSettings { class ApplicationSettings {
#define BUILD_SETTING_FIELD(Category, Type, Name, DefaultValue) \ #define BUILD_SETTING_FIELD(Category, Type, Name, DefaultValue) \
inline void set##Name(const Type &value) { \ 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_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: public:
ApplicationSettings(const std::string &path); ApplicationSettings(const std::string &path) : m_path(path) {
void startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds); 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<Child *>(this)->initializeFileds();
save();
}
}
void startCheckInterval(boost::asio::io_context &ioContext, uint32_t seconds) {
m_timer = std::make_unique<boost::asio::steady_timer>(ioContext);
m_interval = seconds;
run();
}
protected: 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; boost::property_tree::ptree m_ptree;
bool m_needSave = false; bool m_needSave = false;

View File

@ -1,7 +1,7 @@
find_package(Boost REQUIRED COMPONENTS log log_setup program_options) find_package(Boost REQUIRED COMPONENTS log log_setup program_options)
add_library(Universal add_library(Universal
ApplicationSettings.h ApplicationSettings.cpp ApplicationSettings.h
BoostLog.h BoostLog.inl BoostLog.cpp BoostLog.h BoostLog.inl BoostLog.cpp
BufferUtility.h BufferUtility.cpp BufferUtility.h BufferUtility.cpp
DateTime.h DateTime.cpp DateTime.h DateTime.cpp