From 29cf6ecf04ac06597498699bdaa819ea5b282693 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 4 Feb 2025 12:53:19 +0000 Subject: [PATCH] MessageManager: Update callAsync to take an arbitrary callable instead of a function --- .../messages/juce_MessageManager.cpp | 12 ----------- .../messages/juce_MessageManager.h | 20 ++++++++++++++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/modules/juce_events/messages/juce_MessageManager.cpp b/modules/juce_events/messages/juce_MessageManager.cpp index 68b5f68716..f0ddf6127d 100644 --- a/modules/juce_events/messages/juce_MessageManager.cpp +++ b/modules/juce_events/messages/juce_MessageManager.cpp @@ -204,18 +204,6 @@ void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* func return nullptr; } -bool MessageManager::callAsync (std::function fn) -{ - struct AsyncCallInvoker final : public MessageBase - { - AsyncCallInvoker (std::function f) : callback (std::move (f)) {} - void messageCallback() override { callback(); } - std::function callback; - }; - - return (new AsyncCallInvoker (std::move (fn)))->post(); -} - //============================================================================== void MessageManager::deliverBroadcastMessage (const String& value) { diff --git a/modules/juce_events/messages/juce_MessageManager.h b/modules/juce_events/messages/juce_MessageManager.h index 0dda096e2c..904bb227e9 100644 --- a/modules/juce_events/messages/juce_MessageManager.h +++ b/modules/juce_events/messages/juce_MessageManager.h @@ -103,10 +103,24 @@ public: //============================================================================== /** Asynchronously invokes a function or C++11 lambda on the message thread. - @returns true if the message was successfully posted to the message queue, - or false otherwise. + @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 functionToCall); + template + static bool callAsync (Function&& function) + { + using NonRef = std::remove_cv_t>; + + 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.