Browse Source

Added static method Timer::callAfterDelay() to invoke a one-shot lambda function

tags/2021-05-28
jules 8 years ago
parent
commit
39284e1d0f
2 changed files with 31 additions and 0 deletions
  1. +26
    -0
      modules/juce_events/timers/juce_Timer.cpp
  2. +5
    -0
      modules/juce_events/timers/juce_Timer.h

+ 26
- 0
modules/juce_events/timers/juce_Timer.cpp View File

@@ -348,3 +348,29 @@ void JUCE_CALLTYPE Timer::callPendingTimersSynchronously()
if (TimerThread::instance != nullptr)
TimerThread::instance->callTimersSynchronously();
}
#if JUCE_COMPILER_SUPPORTS_LAMBDAS
struct LambdaInvoker : private Timer
{
LambdaInvoker (int milliseconds, std::function<void()> f) : function (f)
{
startTimer (milliseconds);
}
void timerCallback() override
{
auto f = function;
delete this;
f();
}
std::function<void()> function;
JUCE_DECLARE_NON_COPYABLE (LambdaInvoker)
};
void JUCE_CALLTYPE Timer::callAfterDelay (int milliseconds, std::function<void()> f)
{
new LambdaInvoker (milliseconds, f);
}
#endif

+ 5
- 0
modules/juce_events/timers/juce_Timer.h View File

@@ -121,6 +121,11 @@ public:
*/
int getTimerInterval() const noexcept { return timerPeriodMs; }
//==============================================================================
#if JUCE_COMPILER_SUPPORTS_LAMBDAS
/** Invokes a lambda after a given number of milliseconds. */
static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function<void()> functionToCall);
#endif
//==============================================================================
/** For internal use only: invokes any timers that need callbacks.


Loading…
Cancel
Save