From 796592edae7cfe416d94c71ffda80c21eddc8010 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 29 Dec 2024 10:57:13 -0600 Subject: [PATCH] Add non-const overload of `sf::Event::getIf` --- include/SFML/Window/Event.hpp | 11 +++++++++++ include/SFML/Window/Event.inl | 10 ++++++++++ test/Window/Event.test.cpp | 9 +++++++++ 3 files changed, 30 insertions(+) diff --git a/include/SFML/Window/Event.hpp b/include/SFML/Window/Event.hpp index 98f66e3d3..f68a29b47 100644 --- a/include/SFML/Window/Event.hpp +++ b/include/SFML/Window/Event.hpp @@ -316,6 +316,17 @@ public: template [[nodiscard]] bool is() const; + //////////////////////////////////////////////////////////// + /// \brief Attempt to get specified event subtype + /// + /// \tparam `TEventSubtype` Type of the desired event subtype + /// + /// \return Address of current event subtype, otherwise `nullptr` + /// + //////////////////////////////////////////////////////////// + template + [[nodiscard]] TEventSubtype* getIf(); + //////////////////////////////////////////////////////////// /// \brief Attempt to get specified event subtype /// diff --git a/include/SFML/Window/Event.inl b/include/SFML/Window/Event.inl index 70cb639c1..4dc18a9ca 100644 --- a/include/SFML/Window/Event.inl +++ b/include/SFML/Window/Event.inl @@ -58,6 +58,16 @@ bool Event::is() const } +//////////////////////////////////////////////////////////// +template +TEventSubtype* Event::getIf() +{ + static_assert(isEventSubtype, "TEventSubtype must be a subtype of sf::Event"); + if constexpr (isEventSubtype) + return std::get_if(&m_data); +} + + //////////////////////////////////////////////////////////// template const TEventSubtype* Event::getIf() const diff --git a/test/Window/Event.test.cpp b/test/Window/Event.test.cpp index 198c60c9d..11d9ee3e4 100644 --- a/test/Window/Event.test.cpp +++ b/test/Window/Event.test.cpp @@ -295,6 +295,15 @@ TEST_CASE("[Window] sf::Event") CHECK(sensorChanged.value == sf::Vector3f()); } + SECTION("getIf()") + { + sf::Event event = sf::Event::MouseMoved{{4, 2}}; + auto* mouseMoved = event.getIf(); + REQUIRE(mouseMoved); + mouseMoved->position = sf::Vector2i(6, 9); + CHECK(mouseMoved->position == sf::Vector2i(6, 9)); + } + SECTION("visit()") { CHECK(sf::Event(sf::Event::Closed{}).visit(visitor) == "Closed");