diff options
author | Hop311 <hop3114@gmail.com> | 2023-09-07 00:54:09 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-09-07 20:37:24 +0200 |
commit | 3cd1d62ec00690a1b29070dd4903754e8f089a21 (patch) | |
tree | 61d630aa130f8411cc0d539dbfb3705154740631 /src/openvic/dataloader/NodeTools.hpp | |
parent | 8e12540771f1fcc670481aa6299fd73cb10aad00 (diff) |
NodeTools cleanup+province definition csv loading
Diffstat (limited to 'src/openvic/dataloader/NodeTools.hpp')
-rw-r--r-- | src/openvic/dataloader/NodeTools.hpp | 169 |
1 files changed, 136 insertions, 33 deletions
diff --git a/src/openvic/dataloader/NodeTools.hpp b/src/openvic/dataloader/NodeTools.hpp index 48c3710..6dbaa1c 100644 --- a/src/openvic/dataloader/NodeTools.hpp +++ b/src/openvic/dataloader/NodeTools.hpp @@ -14,46 +14,149 @@ namespace OpenVic { namespace NodeTools { + using node_callback_t = std::function<return_t(ast::NodeCPtr)>; + constexpr return_t success_callback(ast::NodeCPtr) { return SUCCESS; } + + using key_value_callback_t = std::function<return_t(std::string_view, ast::NodeCPtr)>; + + node_callback_t expect_identifier(std::function<return_t(std::string_view)> callback); + node_callback_t expect_string(std::function<return_t(std::string_view)> callback); + node_callback_t expect_identifier_or_string(std::function<return_t(std::string_view)> callback); + node_callback_t expect_bool(std::function<return_t(bool)> callback); + node_callback_t expect_int(std::function<return_t(int64_t)> callback); + node_callback_t expect_uint(std::function<return_t(uint64_t)> callback); + node_callback_t expect_fixed_point(std::function<return_t(FP)> callback); + node_callback_t expect_colour(std::function<return_t(colour_t)> callback); + node_callback_t expect_date(std::function<return_t(Date)> callback); + node_callback_t expect_assign(key_value_callback_t callback); + + using length_callback_t = std::function<size_t(size_t)>; + constexpr size_t default_length_callback(size_t size) { return size; }; + + 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_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback); + node_callback_t expect_dictionary(key_value_callback_t callback); + + struct dictionary_entry_t { + const enum class expected_count_t : uint8_t { + _MUST_APPEAR = 0b01, + _CAN_REPEAT = 0b10, + + ZERO_OR_ONE = 0, + ONE_EXACTLY = _MUST_APPEAR, + ZERO_OR_MORE = _CAN_REPEAT, + ONE_OR_MORE = _MUST_APPEAR | _CAN_REPEAT + } expected_count; + const node_callback_t callback; + size_t count; + + dictionary_entry_t(expected_count_t new_expected_count, node_callback_t new_callback) + : expected_count { new_expected_count }, callback { new_callback }, count { 0 } {} + + constexpr bool must_appear() const { + return static_cast<uint8_t>(expected_count) & static_cast<uint8_t>(expected_count_t::_MUST_APPEAR); + } + constexpr bool can_repeat() const { + return static_cast<uint8_t>(expected_count) & static_cast<uint8_t>(expected_count_t::_CAN_REPEAT); + } + }; + using enum dictionary_entry_t::expected_count_t; + using key_map_t = std::map<std::string, dictionary_entry_t, std::less<void>>; + + 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); + + 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, + 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); + } + return _expect_dictionary_keys_and_length(length_callback, allow_other_keys, std::move(key_map), args...); + } + + template<typename... Args> + node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, + const 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...); + } + + 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...); + } + + template<typename... Args> + node_callback_t expect_dictionary_keys(Args... args) { + return expect_dictionary_keys_and_length(default_length_callback, args...); + } + template<typename T> - return_t expect_type(ast::NodeCPtr node, std::function<return_t(T const&)> callback); - - return_t expect_identifier(ast::NodeCPtr node, std::function<return_t(std::string_view)> callback); - return_t expect_string(ast::NodeCPtr node, std::function<return_t(std::string_view)> callback); - return_t expect_identifier_or_string(ast::NodeCPtr node, std::function<return_t(std::string_view)> callback); - return_t expect_bool(ast::NodeCPtr node, std::function<return_t(bool)> callback); - return_t expect_int(ast::NodeCPtr node, std::function<return_t(int64_t)> callback); - return_t expect_uint(ast::NodeCPtr node, std::function<return_t(uint64_t)> callback); - return_t expect_fixed_point(ast::NodeCPtr node, std::function<return_t(FP)> callback); - return_t expect_colour(ast::NodeCPtr node, std::function<return_t(colour_t)> callback); - return_t expect_date(ast::NodeCPtr node, std::function<return_t(Date)> callback); - return_t expect_assign(ast::NodeCPtr node, std::function<return_t(std::string_view, ast::NodeCPtr)> callback); - - static const std::function<size_t(size_t)> default_length_callback = [](size_t size) -> size_t { return size; }; - - template<typename T> requires requires(T& t) { + concept Reservable = requires(T& t) { { t.size() } -> std::same_as<size_t>; t.reserve( size_t {} ); + }; + template<Reservable T> + node_callback_t expect_list_reserve_length(T& t, node_callback_t callback) { + return expect_list_and_length( + [&t](size_t size) -> size_t { + t.reserve(t.size() + size); + return size; + }, + callback + ); } - std::function<size_t(size_t)> reserve_length_callback(T& t) { - return [&t](size_t size) -> size_t { - t.reserve(t.size() + size); - return size; - }; + template<Reservable T> + node_callback_t expect_dictionary_reserve_length(T& t, key_value_callback_t callback) { + return expect_list_reserve_length(t, expect_assign(callback)); } - return_t expect_list_and_length(ast::NodeCPtr node, std::function<size_t(size_t)> length_callback, std::function<return_t(ast::NodeCPtr)> callback, bool file_node = false); - return_t expect_list_of_length(ast::NodeCPtr node, std::function<return_t(ast::NodeCPtr)> callback, size_t length, bool file_node = false); - return_t expect_list(ast::NodeCPtr node, std::function<return_t(ast::NodeCPtr)> callback, bool file_node = false); - return_t expect_dictionary_and_length(ast::NodeCPtr node, std::function<size_t(size_t)> length_callback, std::function<return_t(std::string_view, ast::NodeCPtr)> callback, bool file_node = false); - return_t expect_dictionary(ast::NodeCPtr node, std::function<return_t(std::string_view, ast::NodeCPtr)> callback, bool file_node = false); + node_callback_t name_list_callback(std::vector<std::string>& list); + + template<typename T> + std::function<return_t(T)> assign_variable_callback(T& var) { + return [&var](T val) -> return_t { + var = val; + return SUCCESS; + }; + } - static const std::function<return_t(ast::NodeCPtr)> success_callback = [](ast::NodeCPtr) -> return_t { return SUCCESS; }; + template<typename T> + requires(std::integral<T>) + std::function<return_t(uint64_t)> assign_variable_callback_uint(const std::string_view name, T& var) { + return [&var, name](uint64_t val) -> return_t { + if (val <= std::numeric_limits<T>::max()) { + var = val; + return SUCCESS; + } + Logger::error("Invalid ", name, ": ", val, " (valid range: [0, ", static_cast<uint64_t>(std::numeric_limits<T>::max()), "])"); + return FAILURE; + }; + } - struct dictionary_entry_t { - bool must_appear, can_repeat; - std::function<return_t(ast::NodeCPtr)> callback; - }; - using dictionary_key_map_t = std::map<std::string, dictionary_entry_t, std::less<void>>; - return_t expect_dictionary_keys(ast::NodeCPtr node, dictionary_key_map_t const& keys, bool allow_other_keys = false, bool file_node = false); + template<typename T> + requires(std::integral<T>) + std::function<return_t(int64_t)> assign_variable_callback_int(const std::string_view name, T& var) { + return [&var, name](int64_t val) -> return_t { + if (std::numeric_limits<T>::lowest() <= val && val <= std::numeric_limits<T>::max()) { + var = val; + return SUCCESS; + } + Logger::error("Invalid ", name, ": ", val, " (valid range: [", + static_cast<int64_t>(std::numeric_limits<T>::lowest()), ", ", + static_cast<uint64_t>(std::numeric_limits<T>::max()), "])"); + return FAILURE; + }; + } } } |