From bb0d68ff2f196825fce68b7ba54018e448214db9 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Thu, 17 Aug 2023 17:06:52 +0100 Subject: [PATCH] eckit::geometry --- src/eckit/geometry/GridConfig.cc | 68 +++++++++++++++----------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/eckit/geometry/GridConfig.cc b/src/eckit/geometry/GridConfig.cc index 4ba3e59f1..5593e1d69 100644 --- a/src/eckit/geometry/GridConfig.cc +++ b/src/eckit/geometry/GridConfig.cc @@ -28,26 +28,25 @@ namespace { template -T from_value_t(const Value& from) { +MappedConfiguration::value_type __from_value_list(const ValueList& list) { + if (list.size() == 1) { + typename T::value_type to; + fromValue(to, list[0]); + return {to}; + } + T to; - fromValue(to, from); - return to; + fromValue(to, list); + return {to}; } void set_config_value(MappedConfiguration& config, const std::string& key, const Value& value) { - auto all_values = [](const Value& value, const std::function& pred) -> bool { - ValueList list(value); - return std::all_of(list.begin(), list.end(), pred); - }; - - value.isDouble() ? config.set(key, from_value_t(value)) - : value.isNumber() ? config.set(key, from_value_t(value)) - : value.isBool() ? config.set(key, from_value_t(value)) - : all_values(value, [](const Value& value) { return value.isDouble(); }) ? config.set(key, from_value_t>(value)) - : all_values(value, [](const Value& value) { return value.isNumber(); }) ? config.set(key, from_value_t>(value)) - : all_values(value, [](const Value& value) { return value.isString(); }) ? config.set(key, from_value_t>(value)) - : config.set(key, from_value_t(value)); + ValueList list(value); + auto val = std::all_of(list.begin(), list.end(), [](const Value& v) { return v.isDouble(); }) ? __from_value_list>(list) + : std::all_of(list.begin(), list.end(), [](const Value& v) { return v.isNumber(); }) ? __from_value_list>(list) + : __from_value_list>(list); + std::visit([&](const auto& val) { config.set(key, val); }, val); } @@ -70,36 +69,33 @@ const GridConfig& GridConfig::instance() { GridConfig::GridConfig(const PathName& path) { - if (!path.exists()) { - return; - } - auto* config = new MappedConfiguration; config_.reset(config); - ValueMap map(YAMLParser::decodeFile(path)); + if (path.exists()) { + ValueMap map(YAMLParser::decodeFile(path)); - for (const auto& kv : map) { - ASSERT(kv.first.isString()); - const auto key = kv.first.as(); + for (const auto& kv : map) { + const auto key = kv.first.as(); - if (key == "grid-names") { - for (ValueMap m : static_cast(kv.second)) { - ASSERT(m.size() == 1); - GridFactoryName::insert(m.begin()->first, config_from_value_map(m.begin()->second)); + if (key == "grid_names") { + for (ValueMap m : static_cast(kv.second)) { + ASSERT(m.size() == 1); + GridFactoryName::insert(m.begin()->first, config_from_value_map(m.begin()->second)); + } + continue; } - continue; - } - if (key == "grid-uids") { - for (ValueMap m : static_cast(kv.second)) { - ASSERT(m.size() == 1); - GridFactoryUID::insert(m.begin()->first, config_from_value_map(m.begin()->second)); + if (key == "grid_uids") { + for (ValueMap m : static_cast(kv.second)) { + ASSERT(m.size() == 1); + GridFactoryUID::insert(m.begin()->first, config_from_value_map(m.begin()->second)); + } + continue; } - continue; - } - set_config_value(*config, key, kv.second); + set_config_value(*config, key, kv.second); + } } }