aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader/NodeTools.hpp
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2023-10-12 21:19:00 +0200
committer hop311 <hop3114@gmail.com>2023-10-12 21:19:00 +0200
commite50c67eb1aaa54f5fb31425f81616bea4e6b880a (patch)
treec4fbc6ee494f8ad33a8de36be5fc300165ce05fc /src/openvic-simulation/dataloader/NodeTools.hpp
parentbb22324da1225a0ac458c1d69893bb3bd28bd6b7 (diff)
Lots of accumulated changes
Diffstat (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp')
-rw-r--r--src/openvic-simulation/dataloader/NodeTools.hpp151
1 files changed, 94 insertions, 57 deletions
diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp
index 5ba9d63..44ac271 100644
--- a/src/openvic-simulation/dataloader/NodeTools.hpp
+++ b/src/openvic-simulation/dataloader/NodeTools.hpp
@@ -11,6 +11,11 @@
namespace OpenVic {
namespace ast = ovdl::v2script::ast;
+ /* Template for map from strings to Ts, in which string_views can be
+ * searched for without needing to be copied into a string, */
+ template<typename T>
+ using string_map_t = std::map<std::string, T, std::less<void>>;
+
namespace NodeTools {
template<typename... Args>
@@ -21,17 +26,55 @@ namespace OpenVic {
using key_value_callback_t = callback_t<std::string_view, ast::NodeCPtr>;
constexpr bool key_value_success_callback(std::string_view, ast::NodeCPtr) { return true; }
+ inline bool key_value_invalid_callback(std::string_view key, ast::NodeCPtr) {
+ Logger::error("Invalid dictionary key: ", key);
+ return false;
+ }
node_callback_t expect_identifier(callback_t<std::string_view> callback);
- node_callback_t expect_string(callback_t<std::string_view> callback);
+ node_callback_t expect_string(callback_t<std::string_view> callback, bool allow_empty = true);
node_callback_t expect_identifier_or_string(callback_t<std::string_view> callback);
+
node_callback_t expect_bool(callback_t<bool> callback);
- node_callback_t expect_int(callback_t<int64_t> callback);
- node_callback_t expect_uint(callback_t<uint64_t> callback);
+ node_callback_t expect_int_bool(callback_t<bool> callback);
+
+ node_callback_t expect_int64(callback_t<int64_t> callback);
+ node_callback_t expect_uint64(callback_t<uint64_t> callback);
+
+ template<std::signed_integral T>
+ node_callback_t expect_int(callback_t<T> callback) {
+ return expect_int64([callback](int64_t val) -> bool {
+ if (static_cast<int64_t>(std::numeric_limits<T>::lowest()) <= val &&
+ val <= static_cast<int64_t>(std::numeric_limits<T>::max())) {
+ return callback(val);
+ }
+ Logger::error("Invalid int: ", val, " (valid range: [",
+ static_cast<int64_t>(std::numeric_limits<T>::lowest()), ", ",
+ static_cast<int64_t>(std::numeric_limits<T>::max()), "])");
+ return false;
+ });
+ }
+
+ template<std::integral T>
+ node_callback_t expect_uint(callback_t<T> callback) {
+ return expect_uint64([callback](uint64_t val) -> bool {
+ if (val <= static_cast<uint64_t>(std::numeric_limits<T>::max())) {
+ return callback(val);
+ }
+ Logger::error("Invalid uint: ", val, " (valid range: [0, ",
+ static_cast<uint64_t>(std::numeric_limits<T>::max()), "])");
+ return false;
+ });
+ }
+
node_callback_t expect_fixed_point(callback_t<fixed_point_t> callback);
node_callback_t expect_colour(callback_t<colour_t> callback);
- node_callback_t expect_timespan(callback_t<Timespan> callback);
+
node_callback_t expect_date(callback_t<Date> callback);
+ node_callback_t expect_years(callback_t<Timespan> callback);
+ node_callback_t expect_months(callback_t<Timespan> callback);
+ node_callback_t expect_days(callback_t<Timespan> callback);
+
node_callback_t expect_ivec2(callback_t<ivec2_t> callback);
node_callback_t expect_fvec2(callback_t<fvec2_t> callback);
node_callback_t expect_assign(key_value_callback_t callback);
@@ -44,7 +87,7 @@ namespace OpenVic {
node_callback_t expect_list(node_callback_t callback);
node_callback_t expect_length(callback_t<size_t> callback);
- node_callback_t expect_key(std::string_view key, node_callback_t callback);
+ node_callback_t expect_key(std::string_view key, node_callback_t callback, bool* key_found = nullptr);
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);
@@ -73,41 +116,45 @@ namespace OpenVic {
}
};
using enum dictionary_entry_t::expected_count_t;
- using key_map_t = std::map<std::string, dictionary_entry_t, std::less<void>>;
+ using key_map_t = string_map_t<dictionary_entry_t>;
- void add_key_map_entry(key_map_t& key_map, 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 add_key_map_entry(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback);
+ bool remove_key_map_entry(key_map_t& key_map, std::string_view key);
+ key_value_callback_t dictionary_keys_callback(key_map_t& key_map, key_value_callback_t default_callback);
bool check_key_map_counts(key_map_t& 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);
+ node_callback_t expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback);
+ node_callback_t expect_dictionary_key_map_and_length(key_map_t key_map, length_callback_t length_callback);
+ node_callback_t expect_dictionary_key_map_and_default(key_map_t key_map, key_value_callback_t default_callback);
+ node_callback_t expect_dictionary_key_map(key_map_t key_map);
template<typename... Args>
- node_callback_t _expect_dictionary_keys_and_length(length_callback_t length_callback,
- bool allow_other_keys, key_map_t&& key_map,
+ node_callback_t expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback,
std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback,
Args... args) {
+ // TODO - pass return value back up (part of big key_map_t rewrite?)
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...);
+ return expect_dictionary_key_map_and_length_and_default(std::move(key_map), length_callback, default_callback, args...);
}
template<typename... Args>
- node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback,
- std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback,
- Args... args) {
- return _expect_dictionary_keys_and_length(length_callback, false, {}, key, expected_count, callback, args...);
+ node_callback_t expect_dictionary_keys_and_length_and_default(length_callback_t length_callback, key_value_callback_t default_callback, Args... args) {
+ return expect_dictionary_key_map_and_length_and_default({}, length_callback, default_callback, args...);
}
template<typename... Args>
- node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback,
- allow_other_keys_t, Args... args) {
- return _expect_dictionary_keys_and_length(length_callback, true, {}, args...);
+ node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, Args... args) {
+ return expect_dictionary_key_map_and_length_and_default({}, length_callback, key_value_invalid_callback, args...);
+ }
+
+ template<typename... Args>
+ node_callback_t expect_dictionary_keys_and_default(key_value_callback_t default_callback, Args... args) {
+ return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, default_callback, args...);
}
template<typename... Args>
node_callback_t expect_dictionary_keys(Args... args) {
- return expect_dictionary_keys_and_length(default_length_callback, args...);
+ return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, key_value_invalid_callback, args...);
}
template<typename T>
@@ -133,7 +180,19 @@ namespace OpenVic {
node_callback_t name_list_callback(std::vector<std::string>& list);
template<typename T>
- callback_t<T> assign_variable_callback(T& var) {
+ callback_t<std::string_view> expect_mapped_string(string_map_t<T> const& map, callback_t<T> callback) {
+ return [&map, callback](std::string_view string) -> bool {
+ const typename string_map_t<T>::const_iterator it = map.find(string);
+ if (it != map.end()) {
+ return callback(it->second);
+ }
+ Logger::error("String not found in map: ", string);
+ return false;
+ };
+ }
+
+ template<typename T, typename U>
+ callback_t<T> assign_variable_callback_cast(U& var) {
return [&var](T val) -> bool {
var = val;
return true;
@@ -141,6 +200,13 @@ namespace OpenVic {
}
template<typename T>
+ callback_t<T> assign_variable_callback(T& var) {
+ return assign_variable_callback_cast<T, T>(var);
+ }
+
+ callback_t<std::string_view> assign_variable_callback_string(std::string& var);
+
+ template<typename T>
callback_t<T&&> move_variable_callback(T& var) {
return [&var](T&& val) -> bool {
var = std::move(val);
@@ -161,41 +227,12 @@ namespace OpenVic {
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 T>
- requires(std::integral<T>)
- callback_t<uint64_t> assign_variable_callback_uint(T& var) {
- return [&var](uint64_t val) -> bool {
- if (val <= static_cast<uint64_t>(std::numeric_limits<T>::max())) {
- var = val;
- return true;
- }
- Logger::error("Invalid uint: ", val, " (valid range: [0, ",
- static_cast<uint64_t>(std::numeric_limits<T>::max()), "])");
- return false;
- };
+ t++;
}
-
- template<typename T>
- requires(std::signed_integral<T>)
- callback_t<int64_t> assign_variable_callback_int(T& var) {
- return [&var](int64_t val) -> bool {
- if (static_cast<int64_t>(std::numeric_limits<T>::lowest()) <= val && val <= static_cast<int64_t>(std::numeric_limits<T>::max())) {
- var = val;
- return true;
- }
- Logger::error("Invalid int: ", val, " (valid range: [",
- static_cast<int64_t>(std::numeric_limits<T>::lowest()), ", ",
- static_cast<int64_t>(std::numeric_limits<T>::max()), "])");
- return false;
+ key_value_callback_t increment_callback(T& var) {
+ return [&var](std::string_view, ast::NodeCPtr) -> bool {
+ var++;
+ return true;
};
}