From 7ba672139cea5459e2afb166933a4a28dfd6fb13 Mon Sep 17 00:00:00 2001 From: vittorioromeo Date: Tue, 9 Jul 2024 16:34:55 +0200 Subject: [PATCH] Simplify `sf::WindowBase::handleEvents` and add basic tests --- include/SFML/Window/WindowBase.inl | 19 ++++++------------- test/Window/WindowBase.test.cpp | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/SFML/Window/WindowBase.inl b/include/SFML/Window/WindowBase.inl index 52b25791c..594692ef7 100644 --- a/include/SFML/Window/WindowBase.inl +++ b/include/SFML/Window/WindowBase.inl @@ -28,7 +28,7 @@ #include #include // NOLINT(misc-header-include-cycle) -#include +#include namespace sf @@ -43,22 +43,13 @@ struct OverloadSet : Ts... template OverloadSet(Ts...) -> OverloadSet; -template -struct OverloadSetWithDefault : OverloadSet +struct DelayOverloadResolution { - // By providing our own operator() and forwarding based - // on invocability of OverloadSet on the concrete type - // of the value, we save the user from having to provide - // their own catch-all overload if they don't want to template - void operator()([[maybe_unused]] T&& value) // NOLINT(cppcoreguidelines-missing-std-forward) + DelayOverloadResolution(const T&) { - if constexpr (std::is_invocable_v, T>) - OverloadSet::operator()(std::forward(value)); } }; -template -OverloadSetWithDefault(Ts...) -> OverloadSetWithDefault; } // namespace priv @@ -68,7 +59,9 @@ void WindowBase::handleEvents(Ts&&... handlers) // NOLINT(cppcoreguidelines-miss { // Disable misc-const-correctness for this line since clang-tidy // complains about it even though the code would become uncompilable - priv::OverloadSetWithDefault overloadSet{std::forward(handlers)...}; // NOLINT(misc-const-correctness) + + // NOLINTNEXTLINE(misc-const-correctness) + priv::OverloadSet overloadSet{std::forward(handlers)..., [](const priv::DelayOverloadResolution&) { /* ignore */ }}; while (const std::optional event = pollEvent()) event->visit(overloadSet); diff --git a/test/Window/WindowBase.test.cpp b/test/Window/WindowBase.test.cpp index 76b862b7f..ab910091e 100644 --- a/test/Window/WindowBase.test.cpp +++ b/test/Window/WindowBase.test.cpp @@ -195,4 +195,21 @@ TEST_CASE("[Window] sf::WindowBase", runDisplayTests()) CHECK(windowBase.getSize() == sf::Vector2u(200, 300)); windowBase.setMinimumSize(sf::Vector2u(200, 300)); } + + SECTION("handleEvents()") + { + sf::WindowBase windowBase(sf::VideoMode({360, 240}), "WindowBase Tests"); + + // Should compile if user provides nothing + windowBase.handleEvents(); + + // Should compile if user provides only a specific handler + windowBase.handleEvents([](const sf::Event::Closed&) {}); + + // Should compile if user provides only a catch-all + windowBase.handleEvents([](const auto&) {}); + + // Should compile if user provides both a specific handler and a catch-all + windowBase.handleEvents([](const sf::Event::Closed&) {}, [](const auto&) {}); + } }