From e9a25973d209702e05e30bee4e8f96376ce800a9 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Mon, 16 Oct 2023 08:26:24 -0400 Subject: Change some NodeTools functions to return Callback concepts --- src/openvic-simulation/dataloader/NodeTools.cpp | 6 +- src/openvic-simulation/dataloader/NodeTools.hpp | 78 +++++++++++++++++++------ 2 files changed, 63 insertions(+), 21 deletions(-) (limited to 'src/openvic-simulation') diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 77eafcd..85bc572 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -4,7 +4,7 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; template -static node_callback_t _expect_type(callback_t callback) { +static NodeCallback auto _expect_type(Callback auto callback) { return [callback](ast::NodeCPtr node) -> bool { if (node != nullptr) { T const* cast_node = node->cast_to(); @@ -20,7 +20,7 @@ static node_callback_t _expect_type(callback_t callback) { } template T> -static callback_t _abstract_string_node_callback(callback_t callback, bool allow_empty) { +static Callback auto _abstract_string_node_callback(Callback auto callback, bool allow_empty) { return [callback, allow_empty](T const& node) -> bool { if (allow_empty) { return callback(node._name); @@ -175,7 +175,7 @@ node_callback_t NodeTools::expect_days(callback_t callback) { } template)> -node_callback_t _expect_vec2(callback_t> callback) { +NodeCallback auto _expect_vec2(Callback> auto callback) { return [callback](ast::NodeCPtr node) -> bool { vec2_t vec; bool ret = expect_dictionary_keys( diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 42e7c5d..f06a85f 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -1,6 +1,10 @@ #pragma once +#include +#include +#include #include +#include #include @@ -18,6 +22,28 @@ namespace OpenVic { namespace NodeTools { + template + concept Functor = requires(Fn&& fn, Args&& ...args) { + { std::invoke(std::forward(fn), std::forward(args)...) } -> std::same_as; + }; + + template + concept FunctorConvertible = requires(Fn&& fn, Args&& ...args) { + { std::invoke(std::forward(fn), std::forward(args)...) } -> std::convertible_to; + }; + + template + concept Callback = Functor; + + template + concept NodeCallback = Callback; + + template + concept KeyValueCallback = Callback; + + template + concept LengthCallback = Functor; + template using callback_t = std::function; @@ -42,7 +68,7 @@ namespace OpenVic { node_callback_t expect_uint64(callback_t callback); template - node_callback_t expect_int(callback_t callback) { + NodeCallback auto expect_int(callback_t callback) { return expect_int64([callback](int64_t val) -> bool { if (static_cast(std::numeric_limits::lowest()) <= val && val <= static_cast(std::numeric_limits::max())) { @@ -56,7 +82,7 @@ namespace OpenVic { } template - node_callback_t expect_uint(callback_t callback) { + NodeCallback auto expect_uint(callback_t callback) { return expect_uint64([callback](uint64_t val) -> bool { if (val <= static_cast(std::numeric_limits::max())) { return callback(val); @@ -125,7 +151,7 @@ namespace OpenVic { constexpr bool add_key_map_entries(key_map_t& key_map) { return true; } template - bool add_key_map_entries(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback, Args... args) { + bool add_key_map_entries(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, NodeCallback auto callback, Args... args) { bool ret = add_key_map_entry(key_map, key, expected_count, callback); ret &= add_key_map_entries(key_map, args...); return ret; @@ -137,29 +163,29 @@ namespace OpenVic { node_callback_t expect_dictionary_key_map(key_map_t key_map); template - 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, Args... args) { + NodeCallback auto expect_dictionary_key_map_and_length_and_default(key_map_t key_map, length_callback_t length_callback, key_value_callback_t default_callback, Args... args) { // TODO - pass return value back up (part of big key_map_t rewrite?) add_key_map_entries(key_map, args...); return expect_dictionary_key_map_and_length_and_default(std::move(key_map), length_callback, default_callback); } template - node_callback_t expect_dictionary_keys_and_length_and_default(length_callback_t length_callback, key_value_callback_t default_callback, Args... args) { + NodeCallback auto expect_dictionary_keys_and_length_and_default(LengthCallback auto length_callback, KeyValueCallback auto default_callback, Args... args) { return expect_dictionary_key_map_and_length_and_default({}, length_callback, default_callback, args...); } template - node_callback_t expect_dictionary_keys_and_length(length_callback_t length_callback, Args... args) { + NodeCallback auto expect_dictionary_keys_and_length(LengthCallback auto length_callback, Args... args) { return expect_dictionary_key_map_and_length_and_default({}, length_callback, key_value_invalid_callback, args...); } template - node_callback_t expect_dictionary_keys_and_default(key_value_callback_t default_callback, Args... args) { + NodeCallback auto expect_dictionary_keys_and_default(KeyValueCallback auto default_callback, Args... args) { return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, default_callback, args...); } template - node_callback_t expect_dictionary_keys(Args... args) { + NodeCallback auto expect_dictionary_keys(Args... args) { return expect_dictionary_key_map_and_length_and_default({}, default_length_callback, key_value_invalid_callback, args...); } @@ -169,7 +195,7 @@ namespace OpenVic { t.reserve(size_t {}); }; template - node_callback_t expect_list_reserve_length(T& t, node_callback_t callback) { + NodeCallback auto expect_list_reserve_length(T& t, NodeCallback auto callback) { return expect_list_and_length( [&t](size_t size) -> size_t { t.reserve(t.size() + size); @@ -179,14 +205,14 @@ namespace OpenVic { ); } template - node_callback_t expect_dictionary_reserve_length(T& t, key_value_callback_t callback) { + NodeCallback auto expect_dictionary_reserve_length(T& t, KeyValueCallback auto callback) { return expect_list_reserve_length(t, expect_assign(callback)); } node_callback_t name_list_callback(callback_t&&> callback); template - callback_t expect_mapped_string(string_map_t const& map, callback_t callback) { + Callback auto expect_mapped_string(string_map_t const& map, Callback auto callback) { return [&map, callback](std::string_view string) -> bool { const typename string_map_t::const_iterator it = map.find(string); if (it != map.end()) { @@ -197,8 +223,24 @@ namespace OpenVic { }; } - template - callback_t assign_variable_callback_cast(U& var) { + template + Callback auto assign_variable_callback_cast(auto& var) { + return [&var](T val) -> bool { + var = val; + return true; + }; + } + + template + callback_t assign_variable_callback_cast(auto& var) { + return [&var](T val) -> bool { + var = val; + return true; + }; + } + + template + callback_t assign_variable_callback_cast(auto& var) { return [&var](T val) -> bool { var = val; return true; @@ -206,14 +248,14 @@ namespace OpenVic { } template - callback_t assign_variable_callback(T& var) { + Callback auto assign_variable_callback(T& var) { return assign_variable_callback_cast(var); } callback_t assign_variable_callback_string(std::string& var); template - callback_t move_variable_callback(T& var) { + Callback auto move_variable_callback(T& var) { return [&var](T&& val) -> bool { var = std::move(val); return true; @@ -224,7 +266,7 @@ namespace OpenVic { requires requires(T& t) { t += T {}; } - callback_t add_variable_callback(T& var) { + Callback auto add_variable_callback(T& var) { return [&var](T val) -> bool { var += val; return true; @@ -235,7 +277,7 @@ namespace OpenVic { requires requires(T& t) { t++; } - key_value_callback_t increment_callback(T& var) { + KeyValueCallback auto increment_callback(T& var) { return [&var](std::string_view, ast::NodeCPtr) -> bool { var++; return true; @@ -243,7 +285,7 @@ namespace OpenVic { } template - callback_t assign_variable_callback_pointer(T const*& var) { + Callback auto assign_variable_callback_pointer(T const*& var) { return [&var](T const& val) -> bool { var = &val; return true; -- cgit v1.2.3-56-ga3b1