add simple config model, an ehpemeral config impl

This commit is contained in:
Green Sky 2023-07-21 20:30:57 +02:00
parent d66ad3743b
commit 835e9c95b7
No known key found for this signature in database
3 changed files with 214 additions and 1 deletions

View File

@ -12,6 +12,6 @@ add_library(solanaceae_util
target_include_directories(solanaceae_util PUBLIC .)
target_compile_features(solanaceae_util PUBLIC cxx_std_17)
#target_link_libraries(solanaceae_contact PUBLIC
#target_link_libraries(solanaceae_util PUBLIC
#)

View File

@ -0,0 +1,168 @@
#include "./simple_config_model.hpp"
#include <iostream>
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, CM_ISV entry, const bool value) { _map_bool[module][category].second[entry] = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, CM_ISV entry, const int64_t value) { _map_int[module][category].second[entry] = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, CM_ISV entry, const double value) { _map_double[module][category].second[entry] = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, CM_ISV entry, CM_ISV value) { _map_string[module][category].second[entry] = value; }
bool SimpleConfigModel::has_bool(CM_ISV module, CM_ISV category, CM_ISV entry) { return _map_bool.count(module) && _map_bool[module].count(category) && _map_bool[module][category].second.count(entry); }
bool SimpleConfigModel::has_int(CM_ISV module, CM_ISV category, CM_ISV entry) { return _map_int.count(module) && _map_int[module].count(category) && _map_int[module][category].second.count(entry); }
bool SimpleConfigModel::has_double(CM_ISV module, CM_ISV category, CM_ISV entry) { return _map_double.count(module) && _map_double[module].count(category) && _map_double[module][category].second.count(entry); }
bool SimpleConfigModel::has_string(CM_ISV module, CM_ISV category, CM_ISV entry) { return _map_string.count(module) && _map_string[module].count(category) && _map_string[module][category].second.count(entry); }
CM_InternalOptional<bool> SimpleConfigModel::get_bool(CM_ISV module, CM_ISV category, CM_ISV entry) {
auto& map = _map_bool;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else if (map[module][category].second.count(entry)) {
return map[module][category].second[entry];
} else {
return map[module][category].first;
}
}
CM_InternalOptional<int64_t> SimpleConfigModel::get_int(CM_ISV module, CM_ISV category, CM_ISV entry) {
auto& map = _map_int;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else if (map[module][category].second.count(entry)) {
return map[module][category].second[entry];
} else {
return map[module][category].first;
}
}
CM_InternalOptional<double> SimpleConfigModel::get_double(CM_ISV module, CM_ISV category, CM_ISV entry) {
auto& map = _map_double;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else if (map[module][category].second.count(entry)) {
return map[module][category].second[entry];
} else {
return map[module][category].first;
}
}
CM_InternalOptional<CM_ISV> SimpleConfigModel::get_string(CM_ISV module, CM_ISV category, CM_ISV entry) {
auto& map = _map_string;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else if (map[module][category].second.count(entry)) {
return CM_ISV{map[module][category].second[entry]};
} else if (map[module][category].first.has_value()){
return CM_ISV{map[module][category].first.value()};
} else {
return std::nullopt;
}
}
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, const bool value) { _map_bool[module][category].first = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, const int64_t value) { _map_int[module][category].first = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, const double value) { _map_double[module][category].first = value; }
void SimpleConfigModel::set(CM_ISV module, CM_ISV category, CM_ISV value) { _map_string[module][category].first = value; }
bool SimpleConfigModel::has_bool(CM_ISV module, CM_ISV category) { return _map_bool.count(module) && _map_bool[module].count(category) && _map_bool[module][category].first.has_value(); }
bool SimpleConfigModel::has_int(CM_ISV module, CM_ISV category) { return _map_int.count(module) && _map_int[module].count(category) && _map_int[module][category].first.has_value(); }
bool SimpleConfigModel::has_double(CM_ISV module, CM_ISV category) { return _map_double.count(module) && _map_double[module].count(category) && _map_double[module][category].first.has_value(); }
bool SimpleConfigModel::has_string(CM_ISV module, CM_ISV category) { return _map_string.count(module) && _map_string[module].count(category) && _map_string[module][category].first.has_value(); }
CM_InternalOptional<bool> SimpleConfigModel::get_bool(CM_ISV module, CM_ISV category) {
auto& map = _map_bool;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else {
return map[module][category].first;
}
}
CM_InternalOptional<int64_t> SimpleConfigModel::get_int(CM_ISV module, CM_ISV category) {
auto& map = _map_int;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else {
return map[module][category].first;
}
}
CM_InternalOptional<double> SimpleConfigModel::get_double(CM_ISV module, CM_ISV category) {
auto& map = _map_double;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else {
return map[module][category].first;
}
}
CM_InternalOptional<CM_ISV> SimpleConfigModel::get_string(CM_ISV module, CM_ISV category) {
auto& map = _map_string;
if (!map.count(module) || !map[module].count(category)) {
return std::nullopt;
} else if (map[module][category].first.has_value()){
return CM_ISV{map[module][category].first.value()};
} else {
return std::nullopt;
}
}
void SimpleConfigModel::dump(void) {
std::cout << "SCM dump:\n";
for (const auto& [k_m, v_c] : _map_bool) {
for (const auto& [k_c, v_e] : v_c) {
if (v_e.first.has_value()) {
std::cout << " " << k_m << "::" << k_c << " = " << (v_e.first.value()?"true":"false") << "\n";
}
for (const auto& [k_e, e] : v_e.second) {
std::cout << " " << k_m << "::" << k_c << "::" << k_e << " = " << (e?"true":"false") << "\n";
}
}
}
for (const auto& [k_m, v_c] : _map_int) {
for (const auto& [k_c, v_e] : v_c) {
if (v_e.first.has_value()) {
std::cout << " " << k_m << "::" << k_c << " = " << v_e.first.value() << "\n";
}
for (const auto& [k_e, e] : v_e.second) {
std::cout << " " << k_m << "::" << k_c << "::" << k_e << " = " << e << "\n";
}
}
}
for (const auto& [k_m, v_c] : _map_double) {
for (const auto& [k_c, v_e] : v_c) {
if (v_e.first.has_value()) {
std::cout << " " << k_m << "::" << k_c << " = " << v_e.first.value() << "\n";
}
for (const auto& [k_e, e] : v_e.second) {
std::cout << " " << k_m << "::" << k_c << "::" << k_e << " = " << e << "\n";
}
}
}
for (const auto& [k_m, v_c] : _map_string) {
for (const auto& [k_c, v_e] : v_c) {
if (v_e.first.has_value()) {
std::cout << " " << k_m << "::" << k_c << " = " << v_e.first.value() << "\n";
}
for (const auto& [k_e, e] : v_e.second) {
std::cout << " " << k_m << "::" << k_c << "::" << k_e << " = " << e << "\n";
}
}
}
}

View File

@ -0,0 +1,45 @@
#pragma once
#include "./config_model.hpp"
#include <map>
struct SimpleConfigModel : public ConfigModelI {
std::map<std::string, std::map<std::string, std::pair<std::optional<bool>, std::map<std::string, bool>>>> _map_bool;
std::map<std::string, std::map<std::string, std::pair<std::optional<int64_t>, std::map<std::string, int64_t>>>> _map_int;
std::map<std::string, std::map<std::string, std::pair<std::optional<double>, std::map<std::string, double>>>> _map_double;
std::map<std::string, std::map<std::string, std::pair<std::optional<std::string>, std::map<std::string, std::string>>>> _map_string;
// level3
void set(CM_ISV module, CM_ISV category, CM_ISV entry, const bool value) override;
void set(CM_ISV module, CM_ISV category, CM_ISV entry, const int64_t value) override;
void set(CM_ISV module, CM_ISV category, CM_ISV entry, const double value) override;
void set(CM_ISV module, CM_ISV category, CM_ISV entry, CM_ISV value) override;
bool has_bool(CM_ISV module, CM_ISV category, CM_ISV entry) override;
bool has_int(CM_ISV module, CM_ISV category, CM_ISV entry) override;
bool has_double(CM_ISV module, CM_ISV category, CM_ISV entry) override;
bool has_string(CM_ISV module, CM_ISV category, CM_ISV entry) override;
CM_InternalOptional<bool> get_bool(CM_ISV module, CM_ISV category, CM_ISV entry) override;
CM_InternalOptional<int64_t> get_int(CM_ISV module, CM_ISV category, CM_ISV entry) override;
CM_InternalOptional<double> get_double(CM_ISV module, CM_ISV category, CM_ISV entry) override;
CM_InternalOptional<CM_ISV> get_string(CM_ISV module, CM_ISV category, CM_ISV entry) override;
// level2
void set(CM_ISV module, CM_ISV category, const bool value) override;
void set(CM_ISV module, CM_ISV category, const int64_t value) override;
void set(CM_ISV module, CM_ISV category, const double value) override;
void set(CM_ISV module, CM_ISV category, CM_ISV value) override;
bool has_bool(CM_ISV module, CM_ISV category) override;
bool has_int(CM_ISV module, CM_ISV category) override;
bool has_double(CM_ISV module, CM_ISV category) override;
bool has_string(CM_ISV module, CM_ISV category) override;
CM_InternalOptional<bool> get_bool(CM_ISV module, CM_ISV category) override;
CM_InternalOptional<int64_t> get_int(CM_ISV module, CM_ISV category) override;
CM_InternalOptional<double> get_double(CM_ISV module, CM_ISV category) override;
CM_InternalOptional<CM_ISV> get_string(CM_ISV module, CM_ISV category) override;
// debug
void dump(void);
};