From ca6911c361b04290fe1f0e64b616249b3b3d71ba Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 May 2009 15:28:16 +0000 Subject: [PATCH 1/1] allow passing temporary functors to Bind() too (closes #10653) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60477 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/event.h | 10 +++++----- tests/events/evthandler.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/wx/event.h b/include/wx/event.h index b116e3e8c6..3ecf5dfc94 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -523,7 +523,7 @@ class wxEventFunctorFunctor : public wxEventFunctor public: typedef typename EventTag::EventClass EventArg; - wxEventFunctorFunctor(Functor& handler) + wxEventFunctorFunctor(const Functor& handler) : m_handler(handler), m_handlerAddr(&handler) { } @@ -591,14 +591,14 @@ wxMakeEventFunctor(const EventTag&, void (*func)(EventArg &)) // Create functors wrapping other functors: template inline wxEventFunctorFunctor * -wxNewEventFunctor(const EventTag&, Functor &func) +wxNewEventFunctor(const EventTag&, const Functor &func) { return new wxEventFunctorFunctor(func); } template inline wxEventFunctorFunctor -wxMakeEventFunctor(const EventTag&, Functor &func) +wxMakeEventFunctor(const EventTag&, const Functor &func) { return wxEventFunctorFunctor(func); } @@ -3059,7 +3059,7 @@ public: // Bind functors to an event: template void Bind(const EventTag& eventType, - Functor &functor, + const Functor &functor, int winid = wxID_ANY, int lastId = wxID_ANY, wxObject *userData = NULL) @@ -3072,7 +3072,7 @@ public: template bool Unbind(const EventTag& eventType, - Functor &functor, + const Functor &functor, int winid = wxID_ANY, int lastId = wxID_ANY, wxObject *userData = NULL) diff --git a/tests/events/evthandler.cpp b/tests/events/evthandler.cpp index 0f49b1a179..863f879de8 100644 --- a/tests/events/evthandler.cpp +++ b/tests/events/evthandler.cpp @@ -309,6 +309,19 @@ void EvtHandlerTestCase::BindFunctor() handler.Bind( MyEventType, functor, 0, 0 ); handler.Unbind( MyEventType, functor, 0, 0 ); + + // Test that a temporary functor is working as well. Note that Unbind will + // fail because a functor can only be unbound if it is the same instance as + // in Bind! + + handler.Bind( MyEventType, MyFunctor() ); + CPPUNIT_ASSERT( !handler.Unbind( MyEventType, MyFunctor() )); + + handler.Bind( MyEventType, MyFunctor(), 0 ); + CPPUNIT_ASSERT( !handler.Unbind( MyEventType, MyFunctor(), 0 )); + + handler.Bind( MyEventType, MyFunctor(), 0, 0 ); + CPPUNIT_ASSERT( !handler.Unbind( MyEventType, MyFunctor(), 0, 0 )); } void EvtHandlerTestCase::BindMethod() -- 2.45.2