aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.cpp61
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.hpp10
2 files changed, 44 insertions, 27 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp
index 2367f86..1c7ddf0 100644
--- a/src/openvic-simulation/dataloader/NodeTools.cpp
+++ b/src/openvic-simulation/dataloader/NodeTools.cpp
@@ -242,32 +242,49 @@ node_callback_t NodeTools::expect_dictionary(key_value_callback_t callback) {
return expect_dictionary_and_length(default_length_callback, callback);
}
+void NodeTools::add_key_map_entry(key_map_t& key_map, const std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback) {
+ if (key_map.find(key) == key_map.end()) {
+ key_map.emplace(key, dictionary_entry_t { expected_count, callback });
+ } else {
+ Logger::error("Duplicate expected dictionary key: ", key);
+ }
+}
+
+key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys) {
+ return [&key_map, allow_other_keys](std::string_view key, ast::NodeCPtr value) -> bool {
+ const key_map_t::iterator it = key_map.find(key);
+ if (it == key_map.end()) {
+ if (allow_other_keys) return true;
+ Logger::error("Invalid dictionary key: ", key);
+ return false;
+ }
+ dictionary_entry_t& entry = it->second;
+ if (++entry.count > 1 && !entry.can_repeat()) {
+ Logger::error("Invalid repeat of dictionary key: ", key);
+ return false;
+ }
+ return entry.callback(value);
+ };
+}
+
+bool NodeTools::check_key_map_counts(key_map_t const& key_map) {
+ bool ret = true;
+ for (key_map_t::value_type const& key_entry : key_map) {
+ dictionary_entry_t const& entry = key_entry.second;
+ if (entry.must_appear() && entry.count < 1) {
+ Logger::error("Mandatory dictionary key not present: ", key_entry.first);
+ ret = false;
+ }
+ }
+ return ret;
+}
+
node_callback_t NodeTools::_expect_dictionary_keys_and_length(length_callback_t length_callback, bool allow_other_keys, key_map_t&& key_map) {
return [length_callback, allow_other_keys, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool {
bool ret = expect_dictionary_and_length(
- length_callback,
- [&key_map, allow_other_keys](std::string_view key, ast::NodeCPtr value) -> bool {
- const key_map_t::iterator it = key_map.find(key);
- if (it == key_map.end()) {
- if (allow_other_keys) return true;
- Logger::error("Invalid dictionary key: ", key);
- return false;
- }
- dictionary_entry_t& entry = it->second;
- if (++entry.count > 1 && !entry.can_repeat()) {
- Logger::error("Invalid repeat of dictionary key: ", key);
- return false;
- }
- return entry.callback(value);
- }
+ length_callback, dictionary_keys_callback(key_map, allow_other_keys)
)(node);
- for (key_map_t::value_type const& key_entry : key_map) {
- dictionary_entry_t const& entry = key_entry.second;
- if (entry.must_appear() && entry.count < 1) {
- Logger::error("Mandatory dictionary key not present: ", key_entry.first);
- ret = false;
- }
- }
+ ret &= check_key_map_counts(key_map);
return ret;
};
}
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp
index e49cab6..2dae05c 100644
--- a/src/openvic-simulation/dataloader/NodeTools.hpp
+++ b/src/openvic-simulation/dataloader/NodeTools.hpp
@@ -72,6 +72,10 @@ namespace OpenVic {
using enum dictionary_entry_t::expected_count_t;
using key_map_t = std::map<std::string, dictionary_entry_t, std::less<void>>;
+ void add_key_map_entry(key_map_t& key_map, const std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback);
+ key_value_callback_t dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys);
+ bool check_key_map_counts(key_map_t const& key_map);
+
constexpr struct allow_other_keys_t {} ALLOW_OTHER_KEYS;
node_callback_t _expect_dictionary_keys_and_length(length_callback_t length_callback, bool allow_other_keys, key_map_t&& key_map);
@@ -81,11 +85,7 @@ namespace OpenVic {
bool allow_other_keys, key_map_t&& key_map,
const std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback,
Args... args) {
- if (key_map.find(key) == key_map.end()) {
- key_map.emplace(key, dictionary_entry_t { expected_count, callback });
- } else {
- Logger::error("Duplicate expected dictionary key: ", key);
- }
+ add_key_map_entry(key_map, key, expected_count, callback);
return _expect_dictionary_keys_and_length(length_callback, allow_other_keys, std::move(key_map), args...);
}