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
This commit is contained in:
Václav Slavík
2013-07-21 10:05:03 +00:00
parent a0bb0df5d7
commit cbc8576a9b
2 changed files with 75 additions and 1 deletions

View File

@@ -1470,6 +1470,39 @@ private:
const ParamType2 m_param2;
};
// This is a version for calling any functors
template <typename T>
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<T*>(this), method, x1, x2)
);
}
template <typename T>
void CallAfter(const T& fn)
{
QueueEvent(new wxAsyncMethodCallEventFunctor<T>(this, fn));
}
#endif // wxHAS_CALL_AFTER

View File

@@ -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<T>(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<typename T, typename T1, ...>
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<typename T>
void CallAfter(const T& functor);
/**
Processes an event, searching event tables and calling zero or more suitable
event handler function(s).