summaryrefslogtreecommitdiff
path: root/utility
diff options
context:
space:
mode:
Diffstat (limited to 'utility')
-rw-r--r--utility/collection.hpp37
1 files changed, 32 insertions, 5 deletions
diff --git a/utility/collection.hpp b/utility/collection.hpp
index 1422a84..a9ddb78 100644
--- a/utility/collection.hpp
+++ b/utility/collection.hpp
@@ -6,26 +6,53 @@
template<typename Object> class Collection {
public:
- using Ptr = std::unique_ptr<Object>;
+ using Ptr = std::shared_ptr<Object>;
std::vector<Ptr> objects;
template<typename T = Object, typename... Params>
- const auto &
+ auto
create(Params &&... params)
{
- return objects.emplace_back(std::make_unique<T>(std::forward<Params>(params)...));
+ auto obj = std::make_shared<T>(std::forward<Params>(params)...);
+ objects.emplace_back(obj);
+ return obj;
}
template<typename T = Object, typename M = void, typename... Params>
- void
+ auto
apply(const M & m, Params &&... params) const
{
- std::for_each(objects.cbegin(), objects.cend(), [&m, &params...](auto && op) {
+ 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;
+ });
+ }
+
+ template<typename T = Object, typename M = void, typename... Params>
+ auto
+ applyOne(const M & 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;
});
}
+
+ template<typename T = Object>
+ void
+ removeAll()
+ {
+ objects.erase(std::remove_if(objects.begin(), objects.end(),
+ [](auto && op) {
+ return dynamic_cast<T *>(op.get());
+ }),
+ objects.end());
+ }
};
#endif