diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-27 23:39:11 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-27 23:39:11 +0000 |
commit | ad5108bd5d8c91bc8d3cab0ab6240c4c6dc71ebc (patch) | |
tree | 27c52443c08e38906f3b4cef980bbcedfbbf4145 /lib | |
parent | applyToOthersType allows passing any params in, not just a T (diff) | |
download | ilt-ad5108bd5d8c91bc8d3cab0ab6240c4c6dc71ebc.tar.bz2 ilt-ad5108bd5d8c91bc8d3cab0ab6240c4c6dc71ebc.tar.xz ilt-ad5108bd5d8c91bc8d3cab0ab6240c4c6dc71ebc.zip |
Fix up removeAll and test with more complex hierarchy
Diffstat (limited to 'lib')
-rw-r--r-- | lib/collection.h | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/collection.h b/lib/collection.h index 853b982..5f39e71 100644 --- a/lib/collection.h +++ b/lib/collection.h @@ -115,14 +115,24 @@ public: } template<typename T> - requires(std::is_convertible_v<T *, Others *> || ...) + requires std::is_base_of_v<Object, T> auto removeAll() { - std::get<OtherObjects<T>>(otherObjects).clear(); - return std::erase_if(objects, [](auto && op) { - return dynamic_cast<T *>(op.get()); - }); + auto removeAllFrom = [](auto & container) { + if constexpr (std::is_base_of_v<T, decltype(std::to_address(container.front()))>) { + const auto size = container.size(); + container.clear(); + return size; + } + else { + return std::erase_if(container, [](auto && objPtr) -> bool { + return dynamic_cast<const T *>(std::to_address(objPtr)); + }); + } + }; + (removeAllFrom(std::get<OtherObjects<Others>>(otherObjects)), ...); + return removeAllFrom(objects); } void |