Kylin/Universal/ApplicationSettings.h

38 lines
1.8 KiB
C
Raw Normal View History

2024-01-06 00:06:35 +08:00
#ifndef __APPLICATIONSETTINGS_H__
#define __APPLICATIONSETTINGS_H__
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/property_tree/ptree.hpp>
class ApplicationSettings {
#define BUILD_SETTING_FIELD(Category, Type, Name, DefaultValue) \
inline void set##Name(const Type &value) { \
std::lock_guard locker(m_mutex); \
m_ptree.put(#Category "." #Name, value); \
m_needSave = true; \
} \
inline Type get##Name() const { \
std::lock_guard locker(m_mutex); \
return m_ptree.get<Type>(#Category "." #Name, DefaultValue); \
}
#define BUILD_STATUS(Type, Name, DefaultValue) BUILD_SETTING_FIELD(Status, Type, Name, DefaultValue)
public:
ApplicationSettings(boost::asio::io_context &ioContext, const std::string &path);
protected:
void run();
boost::property_tree::ptree m_ptree;
bool m_needSave = false;
mutable std::mutex m_mutex;
private:
boost::asio::io_context &m_ioContext;
boost::asio::steady_timer m_timer;
std::string m_path;
};
#endif // __APPLICATIONSETTINGS_H__