mirror of
https://github.com/SFML/SFML.git
synced 2025-01-31 13:45:13 +08:00
Revert "Transform Event::visit signature to accept multiple handlers"
This reverts commit d64a222c86
.
This commit is contained in:
parent
d64a222c86
commit
c6df55eed8
@ -34,7 +34,6 @@
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
|
||||
@ -340,26 +339,26 @@ public:
|
||||
[[nodiscard]] const TEventSubtype* getIf() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Apply handlers to the event
|
||||
/// \brief Apply a visitor to the event
|
||||
///
|
||||
/// \param handlers Handlers to apply
|
||||
/// \param visitor The visitor to apply
|
||||
///
|
||||
/// \return The result of applying the handlers to the event
|
||||
/// \return The result of applying the visitor to the event
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename... Handlers>
|
||||
decltype(auto) visit(Handlers&&... handlers);
|
||||
template <typename T>
|
||||
decltype(auto) visit(T&& visitor);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Apply handlers to the event
|
||||
/// \brief Apply a visitor to the event
|
||||
///
|
||||
/// \param handlers Handlers to apply
|
||||
/// \param visitor The visitor to apply
|
||||
///
|
||||
/// \return The result of applying the handlers to the event
|
||||
/// \return The result of applying the visitor to the event
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename... Handlers>
|
||||
decltype(auto) visit(Handlers&&... handlers) const;
|
||||
template <typename T>
|
||||
decltype(auto) visit(T&& visitor) const;
|
||||
|
||||
private:
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -396,20 +395,11 @@ private:
|
||||
template <typename T, typename... Ts>
|
||||
[[nodiscard]] static constexpr bool isInParameterPack(const std::variant<Ts...>*)
|
||||
{
|
||||
return std::disjunction_v<std::is_same<T, Ts>...>;
|
||||
return (std::is_same_v<T, Ts> || ...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool isEventSubtype = isInParameterPack<T>(static_cast<decltype(&m_data)>(nullptr));
|
||||
|
||||
template <typename Handler, typename... Ts>
|
||||
static constexpr bool isInvokableWithAnyOf(std::variant<Ts...>*)
|
||||
{
|
||||
return std::disjunction_v<std::is_invocable<Handler, Ts&>...>;
|
||||
}
|
||||
|
||||
template <typename Handler>
|
||||
static constexpr bool isValidHandler = isInvokableWithAnyOf<Handler>(static_cast<decltype(&m_data)>(nullptr));
|
||||
static constexpr bool isEventSubtype = isInParameterPack<T>(decltype (&m_data)(nullptr));
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
@ -38,20 +38,6 @@
|
||||
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
template <typename... Ts>
|
||||
struct OverloadSet : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
unsigned char dummy; // Dummy variable to ensure that this struct is not empty thus avoiding a crash due to an MSVC bug
|
||||
#endif
|
||||
};
|
||||
template <typename... Ts>
|
||||
OverloadSet(Ts...) -> OverloadSet<Ts...>;
|
||||
} // namespace priv
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename TEventSubtype>
|
||||
Event::Event(const TEventSubtype& eventSubtype)
|
||||
@ -93,20 +79,18 @@ const TEventSubtype* Event::getIf() const
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename... Handlers>
|
||||
decltype(auto) Event::visit(Handlers&&... handlers)
|
||||
template <typename T>
|
||||
decltype(auto) Event::visit(T&& visitor)
|
||||
{
|
||||
static_assert((isValidHandler<Handlers> && ...), "All handlers must accept a single event subtype parameter");
|
||||
return std::visit(priv::OverloadSet{std::forward<Handlers>(handlers)...}, m_data);
|
||||
return std::visit(std::forward<T>(visitor), m_data);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename... Handlers>
|
||||
decltype(auto) Event::visit(Handlers&&... handlers) const
|
||||
template <typename T>
|
||||
decltype(auto) Event::visit(T&& visitor) const
|
||||
{
|
||||
static_assert((isValidHandler<Handlers> && ...), "All handlers must accept a single event subtype parameter");
|
||||
return std::visit(priv::OverloadSet{std::forward<Handlers>(handlers)...}, m_data);
|
||||
return std::visit(std::forward<T>(visitor), m_data);
|
||||
}
|
||||
|
||||
} // namespace sf
|
||||
|
@ -35,6 +35,17 @@ namespace sf
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
template <typename... Ts>
|
||||
struct OverloadSet : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
unsigned char dummy; // Dummy variable to ensure that this struct is not empty thus avoiding a crash due to an MSVC bug
|
||||
#endif
|
||||
};
|
||||
template <typename... Ts>
|
||||
OverloadSet(Ts...) -> OverloadSet<Ts...>;
|
||||
|
||||
struct DelayOverloadResolution
|
||||
{
|
||||
template <typename T>
|
||||
@ -44,14 +55,21 @@ struct DelayOverloadResolution
|
||||
};
|
||||
} // namespace priv
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename... Handlers>
|
||||
void WindowBase::handleEvents(Handlers&&... handlers)
|
||||
template <typename... Ts>
|
||||
void WindowBase::handleEvents(Ts&&... handlers) // NOLINT(cppcoreguidelines-missing-std-forward)
|
||||
{
|
||||
static_assert(sizeof...(Handlers) > 0, "Must provide at least one handler");
|
||||
static_assert(sizeof...(Ts) > 0, "Must provide at least one handler");
|
||||
|
||||
// Disable misc-const-correctness for this line since clang-tidy
|
||||
// complains about it even though the code would become uncompilable
|
||||
|
||||
// NOLINTNEXTLINE(misc-const-correctness)
|
||||
priv::OverloadSet overloadSet{std::forward<Ts>(handlers)..., [](const priv::DelayOverloadResolution&) { /* ignore */ }};
|
||||
|
||||
while (std::optional event = pollEvent())
|
||||
event->visit(std::forward<Handlers>(handlers)..., [](priv::DelayOverloadResolution) { /* ignore */ });
|
||||
event->visit(overloadSet);
|
||||
}
|
||||
|
||||
} // namespace sf
|
||||
|
Loading…
Reference in New Issue
Block a user