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

EventManager: Allow multiple events to be unregistered at once

parent 9af80eb5
......@@ -118,14 +118,25 @@ private:
libvlc_event_e m_eventType;
};
private:
// variadic template recursion termination
void unregister(){}
public:
void unregister(const RegisteredEvent e)
template <typename T, typename... Args>
void unregister(const T& e, const Args&... args)
{
auto it = std::find_if(begin(m_lambdas), end(m_lambdas), [&e](decltype(m_lambdas)::value_type &value) {
return &e == value.get();
});
if (it != end(m_lambdas))
m_lambdas.erase( it );
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();
});
if (it != end(m_lambdas))
m_lambdas.erase( it );
}
unregister(args...);
}
protected:
......
......@@ -46,5 +46,20 @@ int main(int ac, char** av)
std::this_thread::sleep_for( std::chrono::seconds( 2 ) );
expected = true;
auto l = [&expected] (float){
std::cout << "Lambda called" << std::endl;
assert(expected);
};
auto& h1 = mp.eventManager()->onTimeChanged(l);
auto& h2 = mp.eventManager()->onPositionChanged(l);
std::this_thread::sleep_for( std::chrono::seconds( 2 ) );
// Unregistering multiple events at once.
// h1 and h2 are now invalid.
mp.eventManager()->unregister(h1, h2);
expected = false;
mp.stop();
}
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