From 8734626b2c6fc36acc59e0d7bf7e51d4cff719de Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Mar 2025 18:09:16 +0000 Subject: Use typed collections for apply/applyOne --- lib/collection.h | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/collection.h b/lib/collection.h index 6ee6c82..d32cff1 100644 --- a/lib/collection.h +++ b/lib/collection.h @@ -86,28 +86,32 @@ public: auto apply(const auto & m, Params &&... params) const { - return apply_internal(objects.begin(), objects.end(), m, std::forward(params)...); + const auto & srcObjects = containerFor(); + return apply_internal(srcObjects.begin(), srcObjects.end(), m, std::forward(params)...); } template auto rapply(const auto & m, Params &&... params) const { - return apply_internal(objects.rbegin(), objects.rend(), m, std::forward(params)...); + const auto & srcObjects = containerFor(); + return apply_internal(srcObjects.rbegin(), srcObjects.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)...); + const auto & srcObjects = containerFor(); + return applyOne_internal(srcObjects.begin(), srcObjects.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)...); + const auto & srcObjects = containerFor(); + return applyOne_internal(srcObjects.rbegin(), srcObjects.rend(), m, std::forward(params)...); } template @@ -248,25 +252,40 @@ protected: 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; - }); + if constexpr (std::is_convertible_v) { + std::for_each(begin, end, [&m, ¶ms...](auto && op) { + std::invoke(m, op, std::forward(params)...); + }); + return std::distance(begin, end); + } + else { + 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; - }); + if constexpr (std::is_convertible_v) { + return std::find_if(begin, end, [&m, ¶ms...](auto && op) { + return std::invoke(m, op, std::forward(params)...); + }); + } + else { + 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