1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

MessageManager: Update callAsync to take an arbitrary callable instead of a function

This commit is contained in:
reuk 2025-02-04 12:53:19 +00:00
parent 19edd53842
commit 29cf6ecf04
No known key found for this signature in database
2 changed files with 17 additions and 15 deletions

View file

@ -204,18 +204,6 @@ void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* func
return nullptr;
}
bool MessageManager::callAsync (std::function<void()> fn)
{
struct AsyncCallInvoker final : public MessageBase
{
AsyncCallInvoker (std::function<void()> f) : callback (std::move (f)) {}
void messageCallback() override { callback(); }
std::function<void()> callback;
};
return (new AsyncCallInvoker (std::move (fn)))->post();
}
//==============================================================================
void MessageManager::deliverBroadcastMessage (const String& value)
{

View file

@ -103,10 +103,24 @@ public:
//==============================================================================
/** Asynchronously invokes a function or C++11 lambda on the message thread.
@param function the function to call, which should have no arguments
@returns true if the message was successfully posted to the message queue,
or false otherwise.
*/
static bool callAsync (std::function<void()> functionToCall);
template <typename Function>
static bool callAsync (Function&& function)
{
using NonRef = std::remove_cv_t<std::remove_reference_t<Function>>;
struct AsyncCallInvoker final : public MessageBase
{
explicit AsyncCallInvoker (NonRef f) : fn (std::move (f)) {}
void messageCallback() override { fn(); }
NonRef fn;
};
return (new AsyncCallInvoker { std::move (function) })->post();
}
/** Calls a function using the message-thread.