summaryrefslogtreecommitdiff
path: root/utility
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-28 18:51:30 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-01-28 18:51:30 +0000
commit235f128136c7c23a64fa5f0424edf05879b8ee9b (patch)
treef703d173de2e55fdbdf9723cc5993df63bcff200 /utility
parentGenerate some random terrain with a water layer (diff)
downloadilt-235f128136c7c23a64fa5f0424edf05879b8ee9b.tar.bz2
ilt-235f128136c7c23a64fa5f0424edf05879b8ee9b.tar.xz
ilt-235f128136c7c23a64fa5f0424edf05879b8ee9b.zip
Input stack and logical split of camera controller
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