mirror of
https://github.com/PixlOne/logiops.git
synced 2025-07-14 13:22:33 +08:00
Inline config functions
This commit is contained in:
parent
605ccdc07d
commit
a23f0ea725
@ -45,12 +45,12 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct group_io<T> {
|
struct group_io<T> {
|
||||||
static void get(
|
static inline void get(
|
||||||
[[maybe_unused]] const libconfig::Setting& s,
|
[[maybe_unused]] const libconfig::Setting& s,
|
||||||
[[maybe_unused]] T* t,
|
[[maybe_unused]] T* t,
|
||||||
[[maybe_unused]] const std::vector<std::string>& names,
|
[[maybe_unused]] const std::vector<std::string>& names,
|
||||||
[[maybe_unused]] const std::size_t index) { }
|
[[maybe_unused]] const std::size_t index) { }
|
||||||
static void set(
|
static inline void set(
|
||||||
[[maybe_unused]] libconfig::Setting& s,
|
[[maybe_unused]] libconfig::Setting& s,
|
||||||
[[maybe_unused]] const T* t,
|
[[maybe_unused]] const T* t,
|
||||||
[[maybe_unused]] const std::vector<std::string>& names,
|
[[maybe_unused]] const std::vector<std::string>& names,
|
||||||
@ -59,7 +59,7 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T, typename A, typename... M>
|
template <typename T, typename A, typename... M>
|
||||||
struct group_io<T, A, M...> {
|
struct group_io<T, A, M...> {
|
||||||
static void get(
|
static inline void get(
|
||||||
const libconfig::Setting& s, T* t,
|
const libconfig::Setting& s, T* t,
|
||||||
const std::vector<std::string>& names,
|
const std::vector<std::string>& names,
|
||||||
const std::size_t index, A T::* arg, M T::*... rest) {
|
const std::size_t index, A T::* arg, M T::*... rest) {
|
||||||
@ -77,7 +77,7 @@ namespace logid::config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(
|
static inline void set(
|
||||||
libconfig::Setting& s, const T* t,
|
libconfig::Setting& s, const T* t,
|
||||||
const std::vector<std::string>& names,
|
const std::vector<std::string>& names,
|
||||||
const std::size_t index, A T::* arg, M T::*... rest) {
|
const std::size_t index, A T::* arg, M T::*... rest) {
|
||||||
@ -102,7 +102,7 @@ namespace logid::config {
|
|||||||
friend struct signed_group;
|
friend struct signed_group;
|
||||||
protected:
|
protected:
|
||||||
template <typename T, typename... M>
|
template <typename T, typename... M>
|
||||||
group(const std::array<std::string, sizeof...(M)>& names,
|
explicit group(const std::array<std::string, sizeof...(M)>& names,
|
||||||
M T::*... args) :
|
M T::*... args) :
|
||||||
_names (names.begin(), names.end()),
|
_names (names.begin(), names.end()),
|
||||||
_getter ([args...](const libconfig::Setting& s, group* g,
|
_getter ([args...](const libconfig::Setting& s, group* g,
|
||||||
@ -147,12 +147,12 @@ namespace logid::config {
|
|||||||
namespace {
|
namespace {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct normalize_signature {
|
struct normalize_signature {
|
||||||
static const T& make(const T& ret) { return ret; }
|
static inline const T& make(const T& ret) { return ret; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct normalize_signature<std::string> {
|
struct normalize_signature<std::string> {
|
||||||
static std::string make(const std::string& data) {
|
static inline std::string make(const std::string& data) {
|
||||||
std::string ret = data;
|
std::string ret = data;
|
||||||
std::transform(ret.begin(), ret.end(),
|
std::transform(ret.begin(), ret.end(),
|
||||||
ret.begin(), ::tolower);
|
ret.begin(), ::tolower);
|
||||||
|
@ -39,20 +39,20 @@ namespace logid::config {
|
|||||||
struct config_io {
|
struct config_io {
|
||||||
static_assert(std::is_base_of<group, T>::value);
|
static_assert(std::is_base_of<group, T>::value);
|
||||||
|
|
||||||
static T get(const libconfig::Setting& parent,
|
static inline T get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
T t {};
|
T t {};
|
||||||
t._load(parent.lookup(name));
|
t._load(parent.lookup(name));
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static T get(const libconfig::Setting& setting) {
|
static inline T get(const libconfig::Setting& setting) {
|
||||||
T t {};
|
T t {};
|
||||||
t._load(setting);
|
t._load(setting);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const T& t) {
|
const T& t) {
|
||||||
if(!parent.exists(name)) {
|
if(!parent.exists(name)) {
|
||||||
@ -65,11 +65,11 @@ namespace logid::config {
|
|||||||
t._save(parent.lookup(name));
|
t._save(parent.lookup(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting, const T& t) {
|
static inline void set(libconfig::Setting& setting, const T& t) {
|
||||||
t._save(setting);
|
t._save(setting);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list, const T& t) {
|
static inline void append(libconfig::Setting& list, const T& t) {
|
||||||
auto& x = list.add(libconfig::Setting::TypeGroup);
|
auto& x = list.add(libconfig::Setting::TypeGroup);
|
||||||
set(x, t);
|
set(x, t);
|
||||||
}
|
}
|
||||||
@ -80,16 +80,16 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T, libconfig::Setting::Type TypeEnum>
|
template <typename T, libconfig::Setting::Type TypeEnum>
|
||||||
struct primitive_io {
|
struct primitive_io {
|
||||||
static T get(const libconfig::Setting& parent,
|
static inline T get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
return parent.lookup(name);
|
return parent.lookup(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static T get(const libconfig::Setting& setting) {
|
static inline T get(const libconfig::Setting& setting) {
|
||||||
return setting;
|
return setting;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const T& t) {
|
const T& t) {
|
||||||
if(!parent.exists(name)) {
|
if(!parent.exists(name)) {
|
||||||
@ -101,11 +101,11 @@ namespace logid::config {
|
|||||||
set(parent.lookup(name), t);
|
set(parent.lookup(name), t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting, const T& t) {
|
static inline void set(libconfig::Setting& setting, const T& t) {
|
||||||
setting = t;
|
setting = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list, const T& t) {
|
static inline void append(libconfig::Setting& list, const T& t) {
|
||||||
auto& x = list.add(TypeEnum);
|
auto& x = list.add(TypeEnum);
|
||||||
set(x, t);
|
set(x, t);
|
||||||
}
|
}
|
||||||
@ -113,28 +113,28 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T, typename O, libconfig::Setting::Type TypeEnum>
|
template <typename T, typename O, libconfig::Setting::Type TypeEnum>
|
||||||
struct reinterpret_io {
|
struct reinterpret_io {
|
||||||
static T get(const libconfig::Setting& parent,
|
static inline T get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
return static_cast<T>(primitive_io<O, TypeEnum>::get(parent, name));
|
return static_cast<T>(primitive_io<O, TypeEnum>::get(parent, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static T get(const libconfig::Setting& setting) {
|
static inline T get(const libconfig::Setting& setting) {
|
||||||
return static_cast<T>(primitive_io<O, TypeEnum>::get(setting));
|
return static_cast<T>(primitive_io<O, TypeEnum>::get(setting));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const T& t) {
|
const T& t) {
|
||||||
primitive_io<O, TypeEnum>::set(parent, name,
|
primitive_io<O, TypeEnum>::set(parent, name,
|
||||||
static_cast<O>(t));
|
static_cast<O>(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting, const T& t) {
|
static inline void set(libconfig::Setting& setting, const T& t) {
|
||||||
primitive_io<O, TypeEnum>::set(setting,
|
primitive_io<O, TypeEnum>::set(setting,
|
||||||
static_cast<O>(t));
|
static_cast<O>(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list, const T& t) {
|
static inline void append(libconfig::Setting& list, const T& t) {
|
||||||
primitive_io<O, TypeEnum>::append(list,
|
primitive_io<O, TypeEnum>::append(list,
|
||||||
static_cast<O>(t));
|
static_cast<O>(t));
|
||||||
}
|
}
|
||||||
@ -188,17 +188,13 @@ namespace logid::config {
|
|||||||
struct config_io<std::variant<T...>> {
|
struct config_io<std::variant<T...>> {
|
||||||
private:
|
private:
|
||||||
template <typename Singleton>
|
template <typename Singleton>
|
||||||
static std::variant<T...> try_each(
|
static inline std::variant<T...> try_each(
|
||||||
const libconfig::Setting& setting) {
|
const libconfig::Setting& setting) {
|
||||||
try {
|
|
||||||
return config_io<Singleton>::get(setting);
|
return config_io<Singleton>::get(setting);
|
||||||
} catch(libconfig::SettingTypeException& e) {
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename First, typename Next, typename... Rest>
|
template <typename First, typename Next, typename... Rest>
|
||||||
static std::variant<T...> try_each(
|
static inline std::variant<T...> try_each(
|
||||||
const libconfig::Setting& setting) {
|
const libconfig::Setting& setting) {
|
||||||
try {
|
try {
|
||||||
return config_io<First>::get(setting);
|
return config_io<First>::get(setting);
|
||||||
@ -207,23 +203,25 @@ namespace logid::config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
static std::variant<T...> get(const libconfig::Setting& setting) {
|
static inline std::variant<T...> get(
|
||||||
|
const libconfig::Setting& setting) {
|
||||||
return try_each<T...>(setting);
|
return try_each<T...>(setting);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::variant<T...> get(const libconfig::Setting& parent,
|
static inline std::variant<T...> get(
|
||||||
|
const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
return get(parent.lookup(name));
|
return get(parent.lookup(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting,
|
static inline void set(libconfig::Setting& setting,
|
||||||
const std::variant<T...>& t) {
|
const std::variant<T...>& t) {
|
||||||
std::visit([&setting](auto&& arg){
|
std::visit([&setting](auto&& arg){
|
||||||
config::set(setting, arg);
|
config::set(setting, arg);
|
||||||
}, t);
|
}, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::variant<T...>& t) {
|
const std::variant<T...>& t) {
|
||||||
std::visit([&parent, &name](auto&& arg){
|
std::visit([&parent, &name](auto&& arg){
|
||||||
@ -231,7 +229,7 @@ namespace logid::config {
|
|||||||
}, t);
|
}, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list,
|
static inline void append(libconfig::Setting& list,
|
||||||
const std::variant<T...>& t) {
|
const std::variant<T...>& t) {
|
||||||
std::visit([&list](auto&& arg){
|
std::visit([&list](auto&& arg){
|
||||||
config::append(list, arg);
|
config::append(list, arg);
|
||||||
@ -241,7 +239,7 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct config_io<std::list<T>> {
|
struct config_io<std::list<T>> {
|
||||||
static std::list<T> get(const libconfig::Setting& setting) {
|
static inline std::list<T> get(const libconfig::Setting& setting) {
|
||||||
const auto size = setting.getLength();
|
const auto size = setting.getLength();
|
||||||
std::list<T> t {};
|
std::list<T> t {};
|
||||||
for(int i = 0; i < size; ++i) {
|
for(int i = 0; i < size; ++i) {
|
||||||
@ -252,12 +250,12 @@ namespace logid::config {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::list<T> get(const libconfig::Setting& parent,
|
static inline std::list<T> get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
return get(parent.lookup(name));
|
return get(parent.lookup(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting,
|
static inline void set(libconfig::Setting& setting,
|
||||||
const std::list<T>& t) {
|
const std::list<T>& t) {
|
||||||
while(setting.getLength() != 0)
|
while(setting.getLength() != 0)
|
||||||
setting.remove((int)0);
|
setting.remove((int)0);
|
||||||
@ -266,7 +264,7 @@ namespace logid::config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::list<T>& t) {
|
const std::list<T>& t) {
|
||||||
if (!parent.exists(name)) {
|
if (!parent.exists(name)) {
|
||||||
@ -278,7 +276,7 @@ namespace logid::config {
|
|||||||
set(parent.lookup(name), t);
|
set(parent.lookup(name), t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list,
|
static inline void append(libconfig::Setting& list,
|
||||||
const std::list<T>& t) {
|
const std::list<T>& t) {
|
||||||
auto& s = list.add(libconfig::Setting::TypeList);
|
auto& s = list.add(libconfig::Setting::TypeList);
|
||||||
set(s, t);
|
set(s, t);
|
||||||
@ -287,7 +285,7 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct config_io<std::set<T>> {
|
struct config_io<std::set<T>> {
|
||||||
static std::set<T> get(const libconfig::Setting& setting) {
|
static inline std::set<T> get(const libconfig::Setting& setting) {
|
||||||
const auto size = setting.getLength();
|
const auto size = setting.getLength();
|
||||||
std::set<T> t;
|
std::set<T> t;
|
||||||
for(int i = 0; i < size; ++i) {
|
for(int i = 0; i < size; ++i) {
|
||||||
@ -298,12 +296,12 @@ namespace logid::config {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::set<T> get(const libconfig::Setting& parent,
|
static inline std::set<T> get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
return get(parent.lookup(name));
|
return get(parent.lookup(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting,
|
static inline void set(libconfig::Setting& setting,
|
||||||
const std::set<T>& t) {
|
const std::set<T>& t) {
|
||||||
while(setting.getLength() != 0)
|
while(setting.getLength() != 0)
|
||||||
setting.remove((int)0);
|
setting.remove((int)0);
|
||||||
@ -313,7 +311,7 @@ namespace logid::config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::set<T>& t) {
|
const std::set<T>& t) {
|
||||||
if (!parent.exists(name)) {
|
if (!parent.exists(name)) {
|
||||||
@ -325,7 +323,7 @@ namespace logid::config {
|
|||||||
set(parent.lookup(name), t);
|
set(parent.lookup(name), t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list,
|
static inline void append(libconfig::Setting& list,
|
||||||
const std::set<T>& t) {
|
const std::set<T>& t) {
|
||||||
auto& s = list.add(libconfig::Setting::TypeList);
|
auto& s = list.add(libconfig::Setting::TypeList);
|
||||||
set(s, t);
|
set(s, t);
|
||||||
@ -335,7 +333,7 @@ namespace logid::config {
|
|||||||
template <typename K, typename V, string_literal KeyName,
|
template <typename K, typename V, string_literal KeyName,
|
||||||
typename Cmp, typename Alloc>
|
typename Cmp, typename Alloc>
|
||||||
struct config_io<map<K, V, KeyName, Cmp, Alloc>> {
|
struct config_io<map<K, V, KeyName, Cmp, Alloc>> {
|
||||||
static map<K, V, KeyName, Cmp, Alloc> get(
|
static inline map<K, V, KeyName, Cmp, Alloc> get(
|
||||||
const libconfig::Setting& setting) {
|
const libconfig::Setting& setting) {
|
||||||
const auto size = setting.getLength();
|
const auto size = setting.getLength();
|
||||||
map<K, V, KeyName, Cmp, Alloc> t;
|
map<K, V, KeyName, Cmp, Alloc> t;
|
||||||
@ -349,12 +347,12 @@ namespace logid::config {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static map<K, V, KeyName, Cmp, Alloc> get(
|
static inline map<K, V, KeyName, Cmp, Alloc> get(
|
||||||
const libconfig::Setting& parent, const std::string& name) {
|
const libconfig::Setting& parent, const std::string& name) {
|
||||||
return get(parent.lookup(name));
|
return get(parent.lookup(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& setting,
|
static inline void set(libconfig::Setting& setting,
|
||||||
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
||||||
while(setting.getLength() != 0)
|
while(setting.getLength() != 0)
|
||||||
setting.remove((int)0);
|
setting.remove((int)0);
|
||||||
@ -365,7 +363,7 @@ namespace logid::config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
||||||
if (!parent.exists(name)) {
|
if (!parent.exists(name)) {
|
||||||
@ -377,7 +375,7 @@ namespace logid::config {
|
|||||||
set(parent.lookup(name), t);
|
set(parent.lookup(name), t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(libconfig::Setting& list,
|
static inline void append(libconfig::Setting& list,
|
||||||
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
const map<K, V, KeyName, Cmp, Alloc>& t) {
|
||||||
auto& s = list.add(libconfig::Setting::TypeList);
|
auto& s = list.add(libconfig::Setting::TypeList);
|
||||||
set(s, t);
|
set(s, t);
|
||||||
@ -386,7 +384,7 @@ namespace logid::config {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct config_io<std::optional<T>> {
|
struct config_io<std::optional<T>> {
|
||||||
static std::optional<T> get(const libconfig::Setting& parent,
|
static inline std::optional<T> get(const libconfig::Setting& parent,
|
||||||
const std::string& name) {
|
const std::string& name) {
|
||||||
if(parent.exists(name))
|
if(parent.exists(name))
|
||||||
return config_io<T>::get(parent.lookup(name));
|
return config_io<T>::get(parent.lookup(name));
|
||||||
@ -394,7 +392,7 @@ namespace logid::config {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(libconfig::Setting& parent,
|
static inline void set(libconfig::Setting& parent,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
const std::optional<T>& t) {
|
const std::optional<T>& t) {
|
||||||
if (t.has_value())
|
if (t.has_value())
|
||||||
|
Loading…
Reference in New Issue
Block a user