diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-10-17 13:38:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-17 13:38:16 +0200 |
commit | d4f3231d53da69d98312523fbd9ffee31f3aeaf1 (patch) | |
tree | efe00bd1761b4e430fc0efcbfb6cd01df41b4e39 /src/openvic-simulation/dataloader/NodeTools.hpp | |
parent | f85a0880e19c0ca75eea80f8f38c1e061269dd08 (diff) | |
parent | e9a25973d209702e05e30bee4e8f96376ce800a9 (diff) |
Merge pull request #56 from OpenVicProject/some-node-tool-concepts
Diffstat (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp')
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp | 78 |
1 files changed, 60 insertions, 18 deletions
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 <concepts> +#include <cstdint> +#include <functional> #include <map> +#include <type_traits> #include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> @@ -18,6 +22,28 @@ namespace OpenVic { namespace NodeTools { + template<typename Fn, typename Return = void, typename ...Args> + concept Functor = requires(Fn&& fn, Args&& ...args) { + { std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...) } -> std::same_as<Return>; + }; + + template<typename Fn, typename Return = void, typename ...Args> + concept FunctorConvertible = requires(Fn&& fn, Args&& ...args) { + { std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...) } -> std::convertible_to<Return>; + }; + + template<typename Fn, typename ...Args> + concept Callback = Functor<Fn, bool, Args...>; + + template<typename Fn> + concept NodeCallback = Callback<Fn, ast::NodeCPtr>; + + template<typename Fn> + concept KeyValueCallback = Callback<Fn, std::string_view, ast::NodeCPtr>; + + template<typename Fn> + concept LengthCallback = Functor<Fn, std::size_t, std::size_t>; + template<typename... Args> using callback_t = std::function<bool(Args...)>; @@ -42,7 +68,7 @@ namespace OpenVic { node_callback_t expect_uint64(callback_t<uint64_t> callback); template<std::signed_integral T> - node_callback_t expect_int(callback_t<T> callback) { + NodeCallback auto 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())) { @@ -56,7 +82,7 @@ namespace OpenVic { } template<std::integral T> - node_callback_t expect_uint(callback_t<T> callback) { + NodeCallback auto 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); @@ -125,7 +151,7 @@ namespace OpenVic { constexpr bool add_key_map_entries(key_map_t& key_map) { return true; } template<typename... Args> - 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<typename... Args> - 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<typename... Args> - 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<typename... Args> - 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<typename... Args> - 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<typename... Args> - 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<Reservable T> - 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<Reservable T> - 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<std::vector<std::string>&&> callback); template<typename T> - callback_t<std::string_view> expect_mapped_string(string_map_t<T> const& map, callback_t<T> callback) { + Callback<std::string_view> auto expect_mapped_string(string_map_t<T> const& map, Callback<T> auto 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()) { @@ -197,8 +223,24 @@ namespace OpenVic { }; } - template<typename T, typename U> - callback_t<T> assign_variable_callback_cast(U& var) { + template<typename T> + Callback<T> auto assign_variable_callback_cast(auto& var) { + return [&var](T val) -> bool { + var = val; + return true; + }; + } + + template<std::integral T> + callback_t<T> assign_variable_callback_cast(auto& var) { + return [&var](T val) -> bool { + var = val; + return true; + }; + } + + template<std::signed_integral T> + callback_t<T> assign_variable_callback_cast(auto& var) { return [&var](T val) -> bool { var = val; return true; @@ -206,14 +248,14 @@ namespace OpenVic { } template<typename T> - callback_t<T> assign_variable_callback(T& var) { + Callback<T> auto 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) { + Callback<T&&> 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<T> add_variable_callback(T& var) { + Callback<T> 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<typename T> - callback_t<T const&> assign_variable_callback_pointer(T const*& var) { + Callback<T const&> auto assign_variable_callback_pointer(T const*& var) { return [&var](T const& val) -> bool { var = &val; return true; |