From 627aa88ad2559f41d5be62d36cdbf536a97e4246 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 18 Jan 2021 02:08:01 +0000 Subject: Factor to support worlds, objects, windows etc --- utility/collection.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 utility/collection.hpp (limited to 'utility/collection.hpp') 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 +#include + +template class Collection { +public: + using Ptr = std::unique_ptr; + std::vector objects; + + template + const auto & + create(Params &&... params) + { + return objects.emplace_back(std::make_unique(std::forward(params)...)); + } + + template + void + apply(const M & m, Params &&... params) const + { + std::for_each(objects.cbegin(), objects.cend(), [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast(op.get())) { + std::invoke(m, o, std::forward(params)...); + } + }); + } +}; + +#endif -- cgit v1.2.3