summaryrefslogtreecommitdiff
path: root/lib/collection.hpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-01-14 01:46:50 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-01-16 15:43:10 +0000
commit574d767d54edbca6d4a01e6ab876a26f7478ff5f (patch)
tree0ec5021f1fe59a3169bf8dc69a35278154456689 /lib/collection.hpp
parentProgressive constructors for RayTracer (diff)
downloadilt-574d767d54edbca6d4a01e6ab876a26f7478ff5f.tar.bz2
ilt-574d767d54edbca6d4a01e6ab876a26f7478ff5f.tar.xz
ilt-574d767d54edbca6d4a01e6ab876a26f7478ff5f.zip
Process UI layers in reverse, so it's stack like
Diffstat (limited to 'lib/collection.hpp')
-rw-r--r--lib/collection.hpp69
1 files changed, 52 insertions, 17 deletions
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<typename T = Object, typename M = void, typename... Params>
+ template<typename T = Object, typename... Params>
auto
- apply(const M & m, Params &&... params) const
+ apply(const auto & m, Params &&... params) const
{
- return std::count_if(objects.begin(), objects.end(), [&m, &params...](auto && op) {
- if (auto o = dynamic_cast<T *>(op.get())) {
- std::invoke(m, o, std::forward<Params>(params)...);
- return true;
- }
- return false;
- });
+ return apply_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...);
}
- template<typename T = Object, typename M = void, typename... Params>
+ template<typename T = Object, typename... Params>
auto
- applyOne(const M & m, Params &&... params) const
+ rapply(const auto & m, Params &&... params) const
{
- return std::find_if(objects.begin(), objects.end(), [&m, &params...](auto && op) {
- if (auto o = dynamic_cast<T *>(op.get())) {
- return std::invoke(m, o, std::forward<Params>(params)...);
- }
- return false;
- });
+ return apply_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...);
+ }
+
+ template<typename T = Object, typename... Params>
+ auto
+ applyOne(const auto & m, Params &&... params) const
+ {
+ return applyOne_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...);
+ }
+
+ template<typename T = Object, typename... Params>
+ auto
+ rapplyOne(const auto & m, Params &&... params) const
+ {
+ return applyOne_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...);
}
template<typename T = Object>
@@ -67,4 +70,36 @@ public:
{
return objects.end();
}
+
+ auto
+ rend() const
+ {
+ return objects.rend();
+ }
+
+protected:
+ template<typename T = Object, typename... Params>
+ auto
+ apply_internal(const auto begin, const auto end, const auto & m, Params &&... params) const
+ {
+ return std::count_if(begin, end, [&m, &params...](auto && op) {
+ if (auto o = dynamic_cast<T *>(op.get())) {
+ std::invoke(m, o, std::forward<Params>(params)...);
+ return true;
+ }
+ return false;
+ });
+ }
+
+ template<typename T = Object, typename... Params>
+ auto
+ applyOne_internal(const auto begin, const auto end, const auto & m, Params &&... params) const
+ {
+ return std::find_if(begin, end, [&m, &params...](auto && op) {
+ if (auto o = dynamic_cast<T *>(op.get())) {
+ return std::invoke(m, o, std::forward<Params>(params)...);
+ }
+ return false;
+ });
+ }
};