aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-09-23 01:35:21 +0200
committer Hop311 <hop3114@gmail.com>2023-09-23 01:35:21 +0200
commit6edb54dc3f308c1e9b2ccb7bed21facb129ab963 (patch)
treec39e52312e20fa9cdf8934c21d4016364bfb3e85 /src/openvic-simulation/dataloader
parentd7022294d43a0b173de4f060e3260e986f03853d (diff)
Various fixes, refactors and general cleanup
Diffstat (limited to 'src/openvic-simulation/dataloader')
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.cpp14
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.hpp23
2 files changed, 37 insertions, 0 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp
index 7dd6e16..2367f86 100644
--- a/src/openvic-simulation/dataloader/NodeTools.cpp
+++ b/src/openvic-simulation/dataloader/NodeTools.cpp
@@ -220,6 +220,20 @@ node_callback_t NodeTools::expect_list(node_callback_t callback) {
return expect_list_and_length(default_length_callback, callback);
}
+node_callback_t NodeTools::expect_length(callback_t<size_t> callback) {
+ return [callback](ast::NodeCPtr node) -> bool {
+ bool ret = true;
+ ret &= expect_list_and_length(
+ [callback, &ret](size_t size) -> size_t {
+ ret &= callback(size);
+ return 0;
+ },
+ success_callback
+ )(node);
+ return ret;
+ };
+}
+
node_callback_t NodeTools::expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback) {
return expect_list_and_length(length_callback, expect_assign(callback));
}
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp
index 51bbfa9..5c22e11 100644
--- a/src/openvic-simulation/dataloader/NodeTools.hpp
+++ b/src/openvic-simulation/dataloader/NodeTools.hpp
@@ -41,6 +41,7 @@ namespace OpenVic {
node_callback_t expect_list_and_length(length_callback_t length_callback, node_callback_t callback);
node_callback_t expect_list_of_length(size_t length, node_callback_t callback);
node_callback_t expect_list(node_callback_t callback);
+ node_callback_t expect_length(callback_t<size_t> callback);
node_callback_t expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback);
node_callback_t expect_dictionary(key_value_callback_t callback);
@@ -136,6 +137,28 @@ namespace OpenVic {
};
}
+ template<typename T>
+ requires requires(T& t) {
+ t += T {};
+ }
+ callback_t<T> add_variable_callback(T& var) {
+ return [&var](T val) -> bool {
+ var += val;
+ return true;
+ };
+ }
+
+ template<typename T>
+ requires requires(T& t) {
+ t--;
+ }
+ node_callback_t decrement_callback(T& var, node_callback_t callback) {
+ return [&var, callback](ast::NodeCPtr node) -> bool {
+ var--;
+ return callback(node);
+ };
+ }
+
template<typename I, typename T>
requires(std::integral<I>, std::integral<T>)
callback_t<I> _assign_variable_callback_int(const std::string_view name, T& var) {