diff options
-rw-r--r-- | lib/collection.hpp | 69 | ||||
-rw-r--r-- | test/test-collection.cpp | 10 | ||||
-rw-r--r-- | ui/gameMainWindow.cpp | 3 | ||||
-rw-r--r-- | ui/window.cpp | 2 |
4 files changed, 65 insertions, 19 deletions
diff --git a/lib/collection.hpp b/lib/collection.hpp index a80faa2..8b39ae4 100644 --- a/lib/collection.hpp +++ b/lib/collection.hpp @@ -26,29 +26,32 @@ public: } } - template<typename T = Object, typename M = void, typename... Params> + template<typename T = Object, typename... Params> auto - apply(const M & m, Params &&... params) const + apply(const auto & m, Params &&... params) const { - return std::count_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast<T *>(op.get())) { - std::invoke(m, o, std::forward<Params>(params)...); - return true; - } - return false; - }); + return apply_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...); } - template<typename T = Object, typename M = void, typename... Params> + template<typename T = Object, typename... Params> auto - applyOne(const M & m, Params &&... params) const + rapply(const auto & m, Params &&... params) const { - return std::find_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast<T *>(op.get())) { - return std::invoke(m, o, std::forward<Params>(params)...); - } - return false; - }); + return apply_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...); + } + + template<typename T = Object, typename... Params> + auto + applyOne(const auto & m, Params &&... params) const + { + return applyOne_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...); + } + + template<typename T = Object, typename... Params> + auto + rapplyOne(const auto & m, Params &&... params) const + { + return applyOne_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...); } template<typename T = Object> @@ -67,4 +70,36 @@ public: { return objects.end(); } + + auto + rend() const + { + return objects.rend(); + } + +protected: + template<typename T = Object, typename... Params> + auto + apply_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::count_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast<T *>(op.get())) { + std::invoke(m, o, std::forward<Params>(params)...); + return true; + } + return false; + }); + } + + template<typename T = Object, typename... Params> + auto + applyOne_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::find_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast<T *>(op.get())) { + return std::invoke(m, o, std::forward<Params>(params)...); + } + return false; + }); + } }; diff --git a/test/test-collection.cpp b/test/test-collection.cpp index 6e54da3..f31b3a6 100644 --- a/test/test-collection.cpp +++ b/test/test-collection.cpp @@ -35,6 +35,7 @@ public: using TestCollection = Collection<Base>; BOOST_TEST_DONT_PRINT_LOG_VALUE(Collection<Base>::Objects::const_iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(Collection<Base>::Objects::const_reverse_iterator) BOOST_FIXTURE_TEST_SUITE(tc, TestCollection) @@ -54,6 +55,15 @@ BOOST_AUTO_TEST_CASE(a_base) BOOST_CHECK_EQUAL(i, end()); } +BOOST_AUTO_TEST_CASE(a_rbase) +{ + auto b = create<Base>(); + BOOST_REQUIRE(rapply(&Base::add)); + BOOST_CHECK_EQUAL(b->total, 1); + const auto i = rapplyOne(&Base::add); + BOOST_CHECK_EQUAL(i, rend()); +} + BOOST_AUTO_TEST_CASE(a_sub) { auto s = create<Sub>(); diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index 3f9ce8c..b559341 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -75,6 +75,7 @@ public: else { clicked.clear(); } + return true; } } return false; @@ -88,9 +89,9 @@ private: GameMainWindow::GameMainWindow(size_t w, size_t h) : Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, rdiv(w, h), 0.1F, 10000.0F} { + uiComponents.create<ManualCameraController>(glm::vec2 {-1150, -1150}); uiComponents.create<GameMainSelector>(&camera, glm::vec2 {w, h}); uiComponents.create<GameMainToolbar>(); - uiComponents.create<ManualCameraController>(glm::vec2 {-1150, -1150}); shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); shader.setUniform("lightColor", {.6, .6, .6}); diff --git a/ui/window.cpp b/ui/window.cpp index 74a3a2f..9ecc88c 100644 --- a/ui/window.cpp +++ b/ui/window.cpp @@ -63,7 +63,7 @@ Window::handleInput(const SDL_Event & e) eAdjusted.button.y = size.y - e.button.y;
break;
}
- uiComponents.applyOne(&UIComponent::handleInput, eAdjusted, UIComponent::Position {{}, size});
+ uiComponents.rapplyOne(&UIComponent::handleInput, eAdjusted, UIComponent::Position {{}, size});
return true;
}
return false;
|