diff --git a/include/SFML/Window/Event.hpp b/include/SFML/Window/Event.hpp index 4ff5a5a3a..df2972b6a 100644 --- a/include/SFML/Window/Event.hpp +++ b/include/SFML/Window/Event.hpp @@ -34,7 +34,6 @@ #include -#include #include @@ -340,26 +339,26 @@ public: [[nodiscard]] const TEventSubtype* getIf() const; //////////////////////////////////////////////////////////// - /// \brief Apply handlers to the event + /// \brief Apply a visitor to the event /// - /// \param handlers Handlers to apply + /// \param visitor The visitor to apply /// - /// \return The result of applying the handlers to the event + /// \return The result of applying the visitor to the event /// //////////////////////////////////////////////////////////// - template - decltype(auto) visit(Handlers&&... handlers); + template + decltype(auto) visit(T&& visitor); //////////////////////////////////////////////////////////// - /// \brief Apply handlers to the event + /// \brief Apply a visitor to the event /// - /// \param handlers Handlers to apply + /// \param visitor The visitor to apply /// - /// \return The result of applying the handlers to the event + /// \return The result of applying the visitor to the event /// //////////////////////////////////////////////////////////// - template - decltype(auto) visit(Handlers&&... handlers) const; + template + decltype(auto) visit(T&& visitor) const; private: //////////////////////////////////////////////////////////// @@ -396,20 +395,11 @@ private: template [[nodiscard]] static constexpr bool isInParameterPack(const std::variant*) { - return std::disjunction_v...>; + return (std::is_same_v || ...); } template - static constexpr bool isEventSubtype = isInParameterPack(static_cast(nullptr)); - - template - static constexpr bool isInvokableWithAnyOf(std::variant*) - { - return std::disjunction_v...>; - } - - template - static constexpr bool isValidHandler = isInvokableWithAnyOf(static_cast(nullptr)); + static constexpr bool isEventSubtype = isInParameterPack(decltype (&m_data)(nullptr)); }; } // namespace sf diff --git a/include/SFML/Window/Event.inl b/include/SFML/Window/Event.inl index 1e48e6bcb..daabd2fd3 100644 --- a/include/SFML/Window/Event.inl +++ b/include/SFML/Window/Event.inl @@ -38,20 +38,6 @@ namespace sf { -namespace priv -{ -template -struct OverloadSet : Ts... -{ - using Ts::operator()...; -#if defined(_MSC_VER) && !defined(__clang__) - unsigned char dummy; // Dummy variable to ensure that this struct is not empty thus avoiding a crash due to an MSVC bug -#endif -}; -template -OverloadSet(Ts...) -> OverloadSet; -} // namespace priv - //////////////////////////////////////////////////////////// template Event::Event(const TEventSubtype& eventSubtype) @@ -93,20 +79,18 @@ const TEventSubtype* Event::getIf() const //////////////////////////////////////////////////////////// -template -decltype(auto) Event::visit(Handlers&&... handlers) +template +decltype(auto) Event::visit(T&& visitor) { - static_assert((isValidHandler && ...), "All handlers must accept a single event subtype parameter"); - return std::visit(priv::OverloadSet{std::forward(handlers)...}, m_data); + return std::visit(std::forward(visitor), m_data); } //////////////////////////////////////////////////////////// -template -decltype(auto) Event::visit(Handlers&&... handlers) const +template +decltype(auto) Event::visit(T&& visitor) const { - static_assert((isValidHandler && ...), "All handlers must accept a single event subtype parameter"); - return std::visit(priv::OverloadSet{std::forward(handlers)...}, m_data); + return std::visit(std::forward(visitor), m_data); } } // namespace sf diff --git a/include/SFML/Window/WindowBase.inl b/include/SFML/Window/WindowBase.inl index 4f482432b..2c5c89c7b 100644 --- a/include/SFML/Window/WindowBase.inl +++ b/include/SFML/Window/WindowBase.inl @@ -35,6 +35,17 @@ namespace sf { namespace priv { +template +struct OverloadSet : Ts... +{ + using Ts::operator()...; +#if defined(_MSC_VER) && !defined(__clang__) + unsigned char dummy; // Dummy variable to ensure that this struct is not empty thus avoiding a crash due to an MSVC bug +#endif +}; +template +OverloadSet(Ts...) -> OverloadSet; + struct DelayOverloadResolution { template @@ -44,14 +55,21 @@ struct DelayOverloadResolution }; } // namespace priv + //////////////////////////////////////////////////////////// -template -void WindowBase::handleEvents(Handlers&&... handlers) +template +void WindowBase::handleEvents(Ts&&... handlers) // NOLINT(cppcoreguidelines-missing-std-forward) { - static_assert(sizeof...(Handlers) > 0, "Must provide at least one handler"); + static_assert(sizeof...(Ts) > 0, "Must provide at least one handler"); + + // Disable misc-const-correctness for this line since clang-tidy + // complains about it even though the code would become uncompilable + + // NOLINTNEXTLINE(misc-const-correctness) + priv::OverloadSet overloadSet{std::forward(handlers)..., [](const priv::DelayOverloadResolution&) { /* ignore */ }}; while (std::optional event = pollEvent()) - event->visit(std::forward(handlers)..., [](priv::DelayOverloadResolution) { /* ignore */ }); + event->visit(overloadSet); } } // namespace sf