diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-18 02:08:01 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-18 02:08:01 +0000 |
commit | 627aa88ad2559f41d5be62d36cdbf536a97e4246 (patch) | |
tree | 0e98c2e9c39db6ecc88ecf1914af608241d7c18e /utility | |
parent | Helpers can be static (diff) | |
download | ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.tar.bz2 ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.tar.xz ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.zip |
Factor to support worlds, objects, windows etc
Diffstat (limited to 'utility')
-rw-r--r-- | utility/collection.hpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/utility/collection.hpp b/utility/collection.hpp new file mode 100644 index 0000000..1422a84 --- /dev/null +++ b/utility/collection.hpp @@ -0,0 +1,31 @@ +#ifndef COLLECTION_H +#define COLLECTION_H + +#include <algorithm> +#include <memory> + +template<typename Object> class Collection { +public: + using Ptr = std::unique_ptr<Object>; + std::vector<Ptr> objects; + + template<typename T = Object, typename... Params> + const auto & + create(Params &&... params) + { + return objects.emplace_back(std::make_unique<T>(std::forward<Params>(params)...)); + } + + template<typename T = Object, typename M = void, typename... Params> + void + apply(const M & m, Params &&... params) const + { + std::for_each(objects.cbegin(), objects.cend(), [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast<T *>(op.get())) { + std::invoke(m, o, std::forward<Params>(params)...); + } + }); + } +}; + +#endif |