diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-23 14:21:12 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-23 14:21:12 +0000 |
commit | 035299f23a9207bb521b19e2f77154c276cf3033 (patch) | |
tree | 33d24274a6eb716d1d92f9bff074c3e81b8fb71e | |
parent | Other objects support in operator= (diff) | |
download | ilt-035299f23a9207bb521b19e2f77154c276cf3033.tar.bz2 ilt-035299f23a9207bb521b19e2f77154c276cf3033.tar.xz ilt-035299f23a9207bb521b19e2f77154c276cf3033.zip |
Other objects support in removeAll/clear
removeAll requires a type that is one of Others, clear clears everything
regardless of type.
-rw-r--r-- | application/main.cpp | 4 | ||||
-rw-r--r-- | lib/collection.h | 11 | ||||
-rw-r--r-- | test/test-collection.cpp | 24 | ||||
-rw-r--r-- | ui/builders/freeExtend.cpp | 4 | ||||
-rw-r--r-- | ui/builders/join.cpp | 4 | ||||
-rw-r--r-- | ui/builders/straight.cpp | 6 |
6 files changed, 43 insertions, 10 deletions
diff --git a/application/main.cpp b/application/main.cpp index 1ca2192..9120376 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -90,7 +90,7 @@ public: for (int N = 0; N < 6; N++) { train->create<RailVehicle>(b47); } - train->orders.removeAll(); + train->orders.clear(); train->orders.create<FreeRoam>(&train->orders); train->currentActivity = train->orders.current()->createActivity(); @@ -113,7 +113,7 @@ public: mainLoop(); - world.removeAll(); + world.clear(); return 0; } }; diff --git a/lib/collection.h b/lib/collection.h index 329b681..98f043b 100644 --- a/lib/collection.h +++ b/lib/collection.h @@ -103,15 +103,24 @@ public: return applyOne_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...); } - template<typename T = Object> + template<typename T> + requires(std::is_convertible_v<T *, Others *> || ...) auto removeAll() { + std::get<OtherObjects<T>>(otherObjects).clear(); return std::erase_if(objects, [](auto && op) { return dynamic_cast<T *>(op.get()); }); } + void + clear() + { + ((std::get<OtherObjects<Others>>(otherObjects).clear()), ...); + objects.clear(); + } + [[nodiscard]] auto begin() const { diff --git a/test/test-collection.cpp b/test/test-collection.cpp index 0cd08d5..13df95c 100644 --- a/test/test-collection.cpp +++ b/test/test-collection.cpp @@ -143,6 +143,30 @@ BOOST_AUTO_TEST_CASE(move_assign) BOOST_CHECK(other.empty()); } +BOOST_AUTO_TEST_CASE(clearAll) +{ + create<Base>(); + create<Sub>(); + emplace(std::make_unique<Base>()); + emplace(std::make_unique<Sub>()); + + clear(); + BOOST_CHECK(objects.empty()); + BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty()); +} + +BOOST_AUTO_TEST_CASE(removeAllOfSub) +{ + create<Base>(); + create<Sub>(); + emplace(std::make_unique<Base>()); + emplace(std::make_unique<Sub>()); + + removeAll<Sub>(); + BOOST_CHECK_EQUAL(objects.size(), 2); + BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty()); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_FIXTURE_TEST_SUITE(btc, UniqueCollection<Base>) diff --git a/ui/builders/freeExtend.cpp b/ui/builders/freeExtend.cpp index 09e1c75..aff7cd7 100644 --- a/ui/builders/freeExtend.cpp +++ b/ui/builders/freeExtend.cpp @@ -22,11 +22,11 @@ BuilderFreeExtend::move( candidateLinks = network->candidateExtend(*p1, p->first); } else { - candidateLinks.removeAll(); + candidateLinks.clear(); } } else { - candidateLinks.removeAll(); + candidateLinks.clear(); } } diff --git a/ui/builders/join.cpp b/ui/builders/join.cpp index 161b081..f6cbce5 100644 --- a/ui/builders/join.cpp +++ b/ui/builders/join.cpp @@ -18,7 +18,7 @@ BuilderJoin::move(Network * network, const GeoData *, const SDL_MouseMotionEvent candidateLinks = network->candidateJoins(p1->pos, p->pos); } else { - candidateLinks.removeAll(); + candidateLinks.clear(); } } } @@ -33,7 +33,7 @@ BuilderJoin::click( if (p1) { create(network, geoData, p1, p); p1.reset(); - candidateLinks.removeAll(); + candidateLinks.clear(); } else { p1 = p; diff --git a/ui/builders/straight.cpp b/ui/builders/straight.cpp index 6815689..e7d83b5 100644 --- a/ui/builders/straight.cpp +++ b/ui/builders/straight.cpp @@ -20,7 +20,7 @@ BuilderStraight::move( candidateLinks = network->candidateStraight(*p1, p->first); } else { - candidateLinks.removeAll(); + candidateLinks.clear(); } } } @@ -34,7 +34,7 @@ BuilderStraight::click( if (const auto p = geoData->intersectRay(ray)) { if (p1) { create(network, geoData, *p1, p->first); - candidateLinks.removeAll(); + candidateLinks.clear(); p1.reset(); } else { @@ -44,7 +44,7 @@ BuilderStraight::click( return; case SDL_BUTTON_MIDDLE: p1.reset(); - candidateLinks.removeAll(); + candidateLinks.clear(); return; } } |