From: Václav Slavík Date: Sun, 21 Jul 2013 10:05:03 +0000 (+0000) Subject: Add functor-taking overload of CallAfter(). X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/cbc8576a9b0102e84d4410cf7592587001ad47dc Add functor-taking overload of CallAfter(). This is a generalization of the existing method-calling overloads. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74574 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/event.h b/include/wx/event.h index 874d42e274..1bad23740b 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -1470,6 +1470,39 @@ private: const ParamType2 m_param2; }; +// This is a version for calling any functors +template +class wxAsyncMethodCallEventFunctor : public wxAsyncMethodCallEvent +{ +public: + typedef T FunctorType; + + wxAsyncMethodCallEventFunctor(wxObject *object, const FunctorType& fn) + : wxAsyncMethodCallEvent(object), + m_fn(fn) + { + } + + wxAsyncMethodCallEventFunctor(const wxAsyncMethodCallEventFunctor& other) + : wxAsyncMethodCallEvent(other), + m_fn(other.m_fn) + { + } + + virtual wxEvent *Clone() const + { + return new wxAsyncMethodCallEventFunctor(*this); + } + + virtual void Execute() + { + m_fn(); + } + +private: + FunctorType m_fn; +}; + #endif // wxHAS_CALL_AFTER @@ -3394,6 +3427,12 @@ public: static_cast(this), method, x1, x2) ); } + + template + void CallAfter(const T& fn) + { + QueueEvent(new wxAsyncMethodCallEventFunctor(this, fn)); + } #endif // wxHAS_CALL_AFTER diff --git a/interface/wx/event.h b/interface/wx/event.h index b9cc77bfe6..74655ab7c1 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -525,7 +525,9 @@ public: @param x1 The (optional) first parameter to pass to the method. @param x2 The (optional) second parameter to pass to the method. - Note that currently only up to 2 arguments can be passed. + Note that currently only up to 2 arguments can be passed. For more + complicated needs, you can use the CallAfter(const T& fn) overload + that can call any functor. @note This method is not available with Visual C++ before version 8 (Visual Studio 2005) as earlier versions of the compiler don't @@ -536,6 +538,39 @@ public: template void CallAfter(void (T::*method)(T1, ...), T1 x1, ...); + /** + Asynchronously call the given functor. + + Calling this function on an object schedules an asynchronous call to + the functor specified as CallAfter() argument at a (slightly) later + time. This is useful when processing some events as certain actions + typically can't be performed inside their handlers, e.g. you shouldn't + show a modal dialog from a mouse click event handler as this would + break the mouse capture state -- but you can call a function showing + this message dialog after the current event handler completes. + + Notice that it is safe to use CallAfter() from other, non-GUI, + threads, but that the method will be always called in the main, GUI, + thread context. + + This overload is particularly useful in combination with C++11 lambdas: + @code + wxGetApp().CallAfter([]{ + wxBell(); + }); + @endcode + + @param functor The functor to call. + + @note This method is not available with Visual C++ before version 8 + (Visual Studio 2005) as earlier versions of the compiler don't + have the required support for C++ templates to implement it. + + @since 2.9.6 + */ + template + void CallAfter(const T& functor); + /** Processes an event, searching event tables and calling zero or more suitable event handler function(s).