From c2c21d1aefa023693bc8e2ea70401ce1e8de2270 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 8 Oct 2022 16:23:18 +0100 Subject: Add collection methods for finding and/or creating objects --- lib/collection.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib') diff --git a/lib/collection.hpp b/lib/collection.hpp index 65d72f3..6802bcb 100644 --- a/lib/collection.hpp +++ b/lib/collection.hpp @@ -29,6 +29,31 @@ public: } } + template + T * + find() + { + if (auto i = std::find_if(objects.begin(), objects.end(), + [](auto && o) { + return (dynamic_cast(o.get())); + }); + i != objects.end()) { + return static_cast(i->get()); + } + return nullptr; + } + + template + auto + findOrCreate(Params &&... params) + requires std::is_base_of_v + { + if (auto o = find()) { + return o; + } + return create(std::forward(params)...).get(); + } + template auto apply(const auto & m, Params &&... params) const -- cgit v1.2.3