From 574d767d54edbca6d4a01e6ab876a26f7478ff5f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Jan 2022 01:46:50 +0000 Subject: Process UI layers in reverse, so it's stack like --- lib/collection.hpp | 69 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/collection.hpp b/lib/collection.hpp index a80faa2..8b39ae4 100644 --- a/lib/collection.hpp +++ b/lib/collection.hpp @@ -26,29 +26,32 @@ public: } } - template + template auto - apply(const M & m, Params &&... params) const + apply(const auto & m, Params &&... params) const { - return std::count_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast(op.get())) { - std::invoke(m, o, std::forward(params)...); - return true; - } - return false; - }); + return apply_internal(objects.begin(), objects.end(), m, std::forward(params)...); } - template + template auto - applyOne(const M & m, Params &&... params) const + rapply(const auto & m, Params &&... params) const { - return std::find_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast(op.get())) { - return std::invoke(m, o, std::forward(params)...); - } - return false; - }); + return apply_internal(objects.rbegin(), objects.rend(), m, std::forward(params)...); + } + + template + auto + applyOne(const auto & m, Params &&... params) const + { + return applyOne_internal(objects.begin(), objects.end(), m, std::forward(params)...); + } + + template + auto + rapplyOne(const auto & m, Params &&... params) const + { + return applyOne_internal(objects.rbegin(), objects.rend(), m, std::forward(params)...); } template @@ -67,4 +70,36 @@ public: { return objects.end(); } + + auto + rend() const + { + return objects.rend(); + } + +protected: + template + auto + apply_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::count_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast(op.get())) { + std::invoke(m, o, std::forward(params)...); + return true; + } + return false; + }); + } + + template + auto + applyOne_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::find_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast(op.get())) { + return std::invoke(m, o, std::forward(params)...); + } + return false; + }); + } }; -- cgit v1.2.3