Skip to content

Commit

Permalink
Implement the empty event state with std::monostate
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 9, 2024
1 parent 0777ce5 commit 4c2c4ca
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
18 changes: 5 additions & 13 deletions include/SFML/Window/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ namespace sf
class SFML_WINDOW_API Event
{
public:
////////////////////////////////////////////////////////////
/// \brief Empty event
///
////////////////////////////////////////////////////////////
struct Empty
{
};

////////////////////////////////////////////////////////////
/// \brief Closed event
///
Expand Down Expand Up @@ -244,7 +236,7 @@ class SFML_WINDOW_API Event
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// Sets the event to sf::Event::Empty
/// Sets the event to an empty state
///
////////////////////////////////////////////////////////////
Event() = default;
Expand Down Expand Up @@ -277,21 +269,21 @@ class SFML_WINDOW_API Event
[[nodiscard]] const T* getIf() const;

////////////////////////////////////////////////////////////
/// \brief Check if current event type is not `Empty`
/// \brief Check if current event type is not empty
///
/// \return True if current event type is not `Empty`
/// \return True if current event type is not empty
///
////////////////////////////////////////////////////////////
[[nodiscard]] explicit operator bool() const
{
return !is<Empty>();
return !is<std::monostate>();
}

private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::variant<Empty,
std::variant<std::monostate,
Closed,
Resized,
FocusLost,
Expand Down
2 changes: 1 addition & 1 deletion include/SFML/Window/WindowBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class SFML_WINDOW_API WindowBase
/// }
/// \endcode
///
/// \return The event; will be `Empty` (convertible to `false`) if no events are pending
/// \return The event; will be empty (convertible to `false`) if no events are pending
///
/// \see waitEvent
///
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Window/WindowImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class WindowImpl
///
/// \param block Use true to block the thread until an event arrives
///
/// \return The event; can be `Empty` (convertible to `false`) if not blocking
/// \return The event; can be empty (convertible to `false`) if not blocking
///
////////////////////////////////////////////////////////////
Event popEvent(bool block);
Expand Down
5 changes: 2 additions & 3 deletions test/Window/Event.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ TEST_CASE("[Window] sf::Event")
{
const sf::Event event;
CHECK(!event);
CHECK(event.is<sf::Event::Empty>());
CHECK(event.getIf<sf::Event::Empty>());
CHECK(event.is<std::monostate>());
CHECK(event.getIf<std::monostate>());
}

SECTION("Template constructor")
Expand Down Expand Up @@ -208,7 +208,6 @@ TEST_CASE("[Window] sf::Event")
SECTION("Subtypes")
{
// Empty structs
STATIC_CHECK(std::is_empty_v<sf::Event::Empty>);
STATIC_CHECK(std::is_empty_v<sf::Event::Closed>);
STATIC_CHECK(std::is_empty_v<sf::Event::FocusLost>);
STATIC_CHECK(std::is_empty_v<sf::Event::FocusGained>);
Expand Down

0 comments on commit 4c2c4ca

Please sign in to comment.