Skip to content

Commit 796592e

Browse files
committed
Add non-const overload of sf::Event::getIf
1 parent 3c084bf commit 796592e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

include/SFML/Window/Event.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,17 @@ class Event
316316
template <typename TEventSubtype>
317317
[[nodiscard]] bool is() const;
318318

319+
////////////////////////////////////////////////////////////
320+
/// \brief Attempt to get specified event subtype
321+
///
322+
/// \tparam `TEventSubtype` Type of the desired event subtype
323+
///
324+
/// \return Address of current event subtype, otherwise `nullptr`
325+
///
326+
////////////////////////////////////////////////////////////
327+
template <typename TEventSubtype>
328+
[[nodiscard]] TEventSubtype* getIf();
329+
319330
////////////////////////////////////////////////////////////
320331
/// \brief Attempt to get specified event subtype
321332
///

include/SFML/Window/Event.inl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ bool Event::is() const
5858
}
5959

6060

61+
////////////////////////////////////////////////////////////
62+
template <typename TEventSubtype>
63+
TEventSubtype* Event::getIf()
64+
{
65+
static_assert(isEventSubtype<TEventSubtype>, "TEventSubtype must be a subtype of sf::Event");
66+
if constexpr (isEventSubtype<TEventSubtype>)
67+
return std::get_if<TEventSubtype>(&m_data);
68+
}
69+
70+
6171
////////////////////////////////////////////////////////////
6272
template <typename TEventSubtype>
6373
const TEventSubtype* Event::getIf() const

test/Window/Event.test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ TEST_CASE("[Window] sf::Event")
295295
CHECK(sensorChanged.value == sf::Vector3f());
296296
}
297297

298+
SECTION("getIf()")
299+
{
300+
sf::Event event = sf::Event::MouseMoved{{4, 2}};
301+
auto* mouseMoved = event.getIf<sf::Event::MouseMoved>();
302+
REQUIRE(mouseMoved);
303+
mouseMoved->position = sf::Vector2i(6, 9);
304+
CHECK(mouseMoved->position == sf::Vector2i(6, 9));
305+
}
306+
298307
SECTION("visit()")
299308
{
300309
CHECK(sf::Event(sf::Event::Closed{}).visit(visitor) == "Closed");

0 commit comments

Comments
 (0)