Commit 23c12a83 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

EventManager: Use pointer to return registered event

This is easier to store the handler, as it only requires auto h = ..., and it also
makes it easier to store in a container.
parent 66827012
......@@ -70,12 +70,12 @@ int main(int ac, char** av)
}, nullptr, nullptr, nullptr, nullptr
);
auto& handler = mp.eventManager().onPositionChanged([&expected](float pos) {
auto handler = mp.eventManager().onPositionChanged([&expected](float pos) {
std::cout << "position changed " << pos << std::endl;
assert(expected);
});
std::this_thread::sleep_for( std::chrono::seconds( 2 ) );
handler.unregister();
handler->unregister();
// handler must be considered a dangling reference from now on.
// We might want to fix this, but is it worth the cost of a shared/weak_pointer?
expected = false;
......@@ -87,8 +87,8 @@ int main(int ac, char** av)
std::cout << "Lambda called" << std::endl;
assert(expected);
};
auto& h1 = mp.eventManager().onTimeChanged(l);
auto& h2 = mp.eventManager().onPositionChanged(l);
auto h1 = mp.eventManager().onTimeChanged(l);
auto h2 = mp.eventManager().onPositionChanged(l);
std::this_thread::sleep_for( std::chrono::seconds( 2 ) );
......@@ -117,7 +117,7 @@ int main(int ac, char** av)
// Showing that copying an object shares the associated eventmanager
auto mp2 = mp;
expected = true;
auto& h3 = mp2.eventManager().onStopped([&expected]() {
auto h3 = mp2.eventManager().onStopped([&expected]() {
std::cout << "MediaPlayer stopped" << std::endl;
assert(expected);
// expect a single call since both media player share the same event manager
......
......@@ -85,7 +85,7 @@ private:
virtual void unregister() override
{
m_eventManager->unregister(*this);
m_eventManager->unregister(this);
}
EventHandler(const EventHandler&) = delete;
......@@ -107,13 +107,13 @@ private:
public:
template <typename T, typename... Args>
void unregister(const T& e, const Args&... args)
void unregister(const T e, const Args... args)
{
static_assert(std::is_convertible<decltype(e), const EventHandlerBase&>::value, "Expected const RegisteredEvent");
static_assert(std::is_convertible<decltype(e), const EventHandlerBase*>::value, "Expected const RegisteredEvent");
{
auto it = std::find_if(begin(m_lambdas), end(m_lambdas), [&e](decltype(m_lambdas)::value_type &value) {
return &e == value.get();
return e == value.get();
});
if (it != end(m_lambdas))
m_lambdas.erase( it );
......@@ -147,7 +147,7 @@ public:
EventManager& operator=(EventManager&&) = default;
protected:
using RegisteredEvent = EventHandlerBase&;
using RegisteredEvent = EventHandlerBase*;
/**
* @brief handle Provides the common behavior for all event handlers
......@@ -170,7 +170,7 @@ protected:
*this, eventType, std::forward<Func>( f ), wrapper ) );
auto raw = ptr.get();
m_lambdas.push_back( std::move( ptr ) );
return *raw;
return raw;
}
template <typename Func>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment