2024-02-09 09:25:26 +08:00
|
|
|
#ifndef LUNA_CONFIG_HELPER
|
|
|
|
#define LUNA_CONFIG_HELPER
|
2024-07-21 19:28:47 +08:00
|
|
|
#include <nlohmann/json.hpp>
|
2024-03-29 18:07:15 +08:00
|
|
|
|
2024-07-21 19:28:47 +08:00
|
|
|
class confighelper
|
|
|
|
{
|
2024-02-09 09:25:26 +08:00
|
|
|
std::wstring configpath;
|
2024-07-21 19:28:47 +08:00
|
|
|
|
2024-02-09 09:25:26 +08:00
|
|
|
public:
|
2024-03-29 18:07:15 +08:00
|
|
|
nlohmann::json configs;
|
2024-02-09 09:25:26 +08:00
|
|
|
confighelper();
|
|
|
|
~confighelper();
|
2024-07-21 19:28:47 +08:00
|
|
|
template <class T>
|
|
|
|
T get(const std::string &key, T default1)
|
|
|
|
{
|
|
|
|
if (configs.find(key) == configs.end())
|
|
|
|
return default1;
|
2024-02-09 09:25:26 +08:00
|
|
|
return configs[key];
|
|
|
|
}
|
2024-07-21 19:28:47 +08:00
|
|
|
template <class T>
|
|
|
|
void set(const std::string &key, T v)
|
|
|
|
{
|
|
|
|
configs[key] = v;
|
2024-02-09 09:25:26 +08:00
|
|
|
}
|
|
|
|
};
|
2024-07-21 19:28:47 +08:00
|
|
|
template <typename T>
|
|
|
|
T safequeryjson(const nlohmann::json &js, const std::string &key, const T &defaultv)
|
|
|
|
{
|
|
|
|
if (js.find(key) == js.end())
|
|
|
|
{
|
2024-03-29 18:07:15 +08:00
|
|
|
return defaultv;
|
|
|
|
}
|
|
|
|
return js[key];
|
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
#endif
|