summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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;
+ });
+ }
};