diff --git a/CMakeLists.txt b/CMakeLists.txt index 36d0d4c..7217b41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 #) diff --git a/solanaceae/util/simple_config_model.cpp b/solanaceae/util/simple_config_model.cpp new file mode 100644 index 0000000..5bae34e --- /dev/null +++ b/solanaceae/util/simple_config_model.cpp @@ -0,0 +1,168 @@ +#include "./simple_config_model.hpp" + +#include + +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 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 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 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 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 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 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 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 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"; + } + } + } +} + diff --git a/solanaceae/util/simple_config_model.hpp b/solanaceae/util/simple_config_model.hpp new file mode 100644 index 0000000..797587e --- /dev/null +++ b/solanaceae/util/simple_config_model.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include "./config_model.hpp" + +#include + +struct SimpleConfigModel : public ConfigModelI { + std::map, std::map>>> _map_bool; + std::map, std::map>>> _map_int; + std::map, std::map>>> _map_double; + std::map, std::map>>> _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 get_bool(CM_ISV module, CM_ISV category, CM_ISV entry) override; + CM_InternalOptional get_int(CM_ISV module, CM_ISV category, CM_ISV entry) override; + CM_InternalOptional get_double(CM_ISV module, CM_ISV category, CM_ISV entry) override; + CM_InternalOptional 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 get_bool(CM_ISV module, CM_ISV category) override; + CM_InternalOptional get_int(CM_ISV module, CM_ISV category) override; + CM_InternalOptional get_double(CM_ISV module, CM_ISV category) override; + CM_InternalOptional get_string(CM_ISV module, CM_ISV category) override; + + // debug + void dump(void); +}; +