From 1c873112ec67f5a979c3f24d4caac50b386192f2 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 9 Nov 2023 16:46:35 -0700 Subject: [PATCH] Return std::optional from `sf::WindowImpl::popEvent` --- src/SFML/Window/WindowBase.cpp | 28 ++++++++++++++++------------ src/SFML/Window/WindowImpl.cpp | 8 ++++---- src/SFML/Window/WindowImpl.hpp | 2 +- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/SFML/Window/WindowBase.cpp b/src/SFML/Window/WindowBase.cpp index d2bc2da2f..deb7d37c4 100644 --- a/src/SFML/Window/WindowBase.cpp +++ b/src/SFML/Window/WindowBase.cpp @@ -159,30 +159,34 @@ bool WindowBase::isOpen() const //////////////////////////////////////////////////////////// bool WindowBase::pollEvent(Event& event) { - if (m_impl && m_impl->popEvent(event, false)) + if (!m_impl) + return false; + + if (const auto maybeEvent = m_impl->popEvent(false)) { - filterEvent(event); + filterEvent(*maybeEvent); + event = *maybeEvent; return true; } - else - { - return false; - } + + return false; } //////////////////////////////////////////////////////////// bool WindowBase::waitEvent(Event& event) { - if (m_impl && m_impl->popEvent(event, true)) + if (!m_impl) + return false; + + if (const auto maybeEvent = m_impl->popEvent(true)) { - filterEvent(event); + filterEvent(*maybeEvent); + event = *maybeEvent; return true; } - else - { - return false; - } + + return false; } diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index d75d87751..a588b7268 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -171,7 +171,7 @@ void WindowImpl::setMaximumSize(const std::optional& maximumSize) //////////////////////////////////////////////////////////// -bool WindowImpl::popEvent(Event& event, bool block) +std::optional WindowImpl::popEvent(bool block) { // If the event queue is empty, let's first check if new events are available from the OS if (m_events.empty()) @@ -200,13 +200,13 @@ bool WindowImpl::popEvent(Event& event, bool block) // Pop the first event of the queue, if it is not empty if (!m_events.empty()) { - event = m_events.front(); + const auto event = m_events.front(); m_events.pop(); - return true; + return event; } - return false; + return std::nullopt; } diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 1cf6ddf11..ff08577ea 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -125,7 +125,7 @@ public: /// \param block Use true to block the thread until an event arrives /// //////////////////////////////////////////////////////////// - bool popEvent(Event& event, bool block); + std::optional popEvent(bool block); //////////////////////////////////////////////////////////// /// \brief Get the OS-specific handle of the window