From 945e435ad521734ddebc2c88a5a56acf6f71af26 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Sun, 23 Jul 2017 12:59:14 -0700 Subject: [PATCH] Changed event polling functions from templates to use std::function --- SDL2pp/EventPolling.cc | 19 +++++++++++++++ SDL2pp/EventPolling.hh | 47 ++++++++++++++++++++++++-------------- tests/test_eventpolling.cc | 4 ++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/SDL2pp/EventPolling.cc b/SDL2pp/EventPolling.cc index 06b684d..9df99a6 100644 --- a/SDL2pp/EventPolling.cc +++ b/SDL2pp/EventPolling.cc @@ -21,6 +21,8 @@ #include +using std::function; + namespace SDL2pp { namespace Event { bool PollEvent() { @@ -30,6 +32,17 @@ namespace SDL2pp { return result; } + bool PollEvent(function eventHandler) { + SDL_Event event; + if (!SDL_PollEvent(&event)) { + return false; + } + + eventHandler(event); + + return true; + } + int PollAllEvents() { int result; @@ -37,5 +50,11 @@ namespace SDL2pp { return result; } + + int PollAllEvents(function eventHandler) { + int result; + for (result = 0; PollEvent(eventHandler); result++); + return result; + } } } diff --git a/SDL2pp/EventPolling.hh b/SDL2pp/EventPolling.hh index 16b0613..eed2d2b 100644 --- a/SDL2pp/EventPolling.hh +++ b/SDL2pp/EventPolling.hh @@ -26,6 +26,8 @@ #include +#include + namespace SDL2pp { namespace Event { //////////////////////////////////////////////////////////// @@ -56,6 +58,19 @@ namespace SDL2pp { /// retrieved the event handler is not called and this function /// returns false. /// + /// This function accepts free functions, lambdas and callable objects. + /// + /// If an instance of a callable struct/class needs to be passed + /// by reference, std::ref() needs to be used: + /// \code + /// struct EventHandler { + /// void operator()(const SDL_Event &); + /// }; + /// + /// EventHandler myEventHandler; + /// PollEvent(std::ref(myEventHandler)); + /// \endcode + /// /// \ingroup events /// /// \headerfile SDL2pp/EventPolling.hh @@ -69,17 +84,7 @@ namespace SDL2pp { /// \see https://wiki.libsdl.org/SDL_PollEvent /// //////////////////////////////////////////////////////////// - template - bool PollEvent(T&& eventHandler) { - SDL_Event event; - if (!SDL_PollEvent(&event)) { - return false; - } - - eventHandler(event); - - return true; - } + SDL2PP_EXPORT bool PollEvent(std::function eventHandler); //////////////////////////////////////////////////////////// /// \brief Polls all the events from the event queue @@ -106,6 +111,19 @@ namespace SDL2pp { /// using the polled event as an argument. This function returns the /// amount of events that were polled. /// + /// This function accepts free functions, lambdas and callable objects. + /// + /// If an instance of a callable struct/class needs to be passed + /// by reference, std::ref() needs to be used: + /// \code + /// struct EventHandler { + /// void operator()(const SDL_Event &); + /// }; + /// + /// EventHandler myEventHandler; + /// PollAllEvents(std::ref(myEventHandler)); + /// \endcode + /// /// \ingroup events /// /// \headerfile SDL2pp/EventPolling.hh @@ -119,12 +137,7 @@ namespace SDL2pp { /// \see https://wiki.libsdl.org/SDL_PollEvent /// //////////////////////////////////////////////////////////// - template - int PollAllEvents(T&& eventHandler) { - int result; - for (result = 0; PollEvent(eventHandler); result++); - return result; - } + SDL2PP_EXPORT int PollAllEvents(std::function eventHandler); } } diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc index 66e4451..a7a662e 100644 --- a/tests/test_eventpolling.cc +++ b/tests/test_eventpolling.cc @@ -101,7 +101,7 @@ BEGIN_TEST(int, char*[]) const SDL_Event expectedEvent = PushUserEvent(45); - EXPECT_TRUE(PollEvent(eventHandler) == true); + EXPECT_TRUE(PollEvent(std::ref(eventHandler)) == true); EXPECT_TRUE(eventHandler.events.size() == 1); const SDL_Event result = eventHandler.events[0]; @@ -195,7 +195,7 @@ BEGIN_TEST(int, char*[]) } int totalExpectedEvents = static_cast(expectedEvents.size()); - EXPECT_TRUE(PollAllEvents(eventHandler) == totalExpectedEvents); + EXPECT_TRUE(PollAllEvents(std::ref(eventHandler)) == totalExpectedEvents); EXPECT_TRUE(eventHandler.events.size() == expectedEvents.size()); for (int n = 0; n < totalExpectedEvents; n++) {