Return std::optional<Event> from sf::WindowImpl::popEvent

This commit is contained in:
Chris Thrasher 2023-11-09 16:46:35 -07:00
parent bff2977f13
commit 1c873112ec
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
3 changed files with 21 additions and 17 deletions

View File

@ -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;
}
}
////////////////////////////////////////////////////////////
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;
}
}

View File

@ -171,7 +171,7 @@ void WindowImpl::setMaximumSize(const std::optional<Vector2u>& maximumSize)
////////////////////////////////////////////////////////////
bool WindowImpl::popEvent(Event& event, bool block)
std::optional<Event> 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;
}

View File

@ -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<Event> popEvent(bool block);
////////////////////////////////////////////////////////////
/// \brief Get the OS-specific handle of the window