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

EventManager: Do not copy construct event handlers.

Own a reference instead, and document the behavior
parent acf360a5
......@@ -36,9 +36,18 @@ int main(int ac, char** av)
const char* vlcArgs = "-vv";
auto instance = VLC::Instance(1, &vlcArgs);
instance.setExitHandler([] {
std::cout << "Libvlc is exiting" << std::endl;
});
{
auto exitHandler = [] {
std::cout << "Libvlc is exiting" << std::endl;
};
// Uncommenting this line would cause undefined behavior, as libvlcpp
// would store a reference, which would become dangling as soon as
// we leave this scope.
//instance.setExitHandler( exitHandler );
// This is fine, since we are moving the exitHandler. No dangling ref.
instance.setExitHandler( std::move( exitHandler ) );
}
instance.logSet([](int lvl, const libvlc_log_t*, std::string message ) {
std::cout << "Hooked VLC log: " << lvl << ' ' << message << std::endl;
......
......@@ -43,9 +43,11 @@ class Media;
/**
* @brief This class serves as a base for all event managers.
*
* All events can be handled by providing a std::function.
* libvlcpp will take ownership (ie. the function will be moved inside an internal list)
* If the provided std::function is a lambda, it may capture anything you desire
* Event handlers can be anything which implement the Callable concept
* (http://en.cppreference.com/w/cpp/concept/Callable)
* libvlcpp can take ownership (ie. move it to an internal storage) of your handler.
* If you provide a rvalue, libvlcpp will store a reference to the handler.
* If you provide a lvalue, libvlcpp will move the handler internaly.
*/
class EventManager : public Internal<libvlc_event_manager_t>
{
......@@ -101,9 +103,7 @@ private:
private:
// Deduced type is Func& in case of lvalue; Func in case of rvalue.
// We decay the type to ensure we either copy or take ownership.
// Taking a reference would quite likely lead to unexpected behavior
typename std::decay<Func>::type m_userCallback;
Func m_userCallback;
// EventManager always outlive EventHandler, no need for smart pointer
EventManager* m_eventManager;
Wrapper m_wrapper;
......
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