diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-10-08 16:23:18 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-10-08 16:23:18 +0100 |
commit | c2c21d1aefa023693bc8e2ea70401ce1e8de2270 (patch) | |
tree | f411f1babf0d62407be513bb9fbb0d64b24c9cd2 | |
parent | Use mode helper in main window toolbar (diff) | |
download | ilt-c2c21d1aefa023693bc8e2ea70401ce1e8de2270.tar.bz2 ilt-c2c21d1aefa023693bc8e2ea70401ce1e8de2270.tar.xz ilt-c2c21d1aefa023693bc8e2ea70401ce1e8de2270.zip |
Add collection methods for finding and/or creating objects
-rw-r--r-- | lib/collection.hpp | 25 |
1 files changed, 25 insertions, 0 deletions
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<typename T = Object> + T * + find() + { + if (auto i = std::find_if(objects.begin(), objects.end(), + [](auto && o) { + return (dynamic_cast<T *>(o.get())); + }); + i != objects.end()) { + return static_cast<T *>(i->get()); + } + return nullptr; + } + + template<typename T = Object, typename... Params> + auto + findOrCreate(Params &&... params) + requires std::is_base_of_v<Object, T> + { + if (auto o = find<T>()) { + return o; + } + return create<T>(std::forward<Params>(params)...).get(); + } + template<typename T = Object, typename... Params> auto apply(const auto & m, Params &&... params) const |