JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LockingAsyncUpdater.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27{
28public:
29 explicit Impl (std::function<void()> cb)
30 : callback (std::move (cb)) {}
31
32 void clear()
33 {
34 const ScopedLock lock (mutex);
35 deliver = false;
36 callback = nullptr;
37 }
38
39 void trigger()
40 {
41 {
42 const ScopedLock lock (mutex);
43
44 if (deliver)
45 return;
46
47 deliver = true;
48 }
49
50 if (! post())
51 cancel();
52 }
53
54 void cancel()
55 {
56 const ScopedLock lock (mutex);
57 deliver = false;
58 }
59
60 bool isPending()
61 {
62 const ScopedLock lock (mutex);
63 return deliver;
64 }
65
66 void messageCallback() override
67 {
68 const ScopedLock lock (mutex);
69
70 if (std::exchange (deliver, false))
71 NullCheckedInvocation::invoke (callback);
72 }
73
74private:
75 CriticalSection mutex;
76 std::function<void()> callback;
77 bool deliver = false;
78};
79
80//==============================================================================
82 : impl (new Impl (std::move (callbackToUse))) {}
83
86
88{
89 LockingAsyncUpdater temp { std::move (other) };
90 std::swap (temp.impl, impl);
91 return *this;
92}
93
95{
96 if (impl != nullptr)
97 impl->clear();
98}
99
101{
102 if (impl != nullptr)
103 impl->trigger();
104 else
105 jassertfalse; // moved-from!
106}
107
109{
110 if (impl != nullptr)
111 impl->cancel();
112 else
113 jassertfalse; // moved-from!
114}
115
117{
118 if (impl != nullptr)
119 impl->messageCallback();
120 else
121 jassertfalse; // moved-from!
122}
123
125{
126 if (impl != nullptr)
127 return impl->isPending();
128
129 jassertfalse; // moved-from!
130 return false;
131}
132
133} // namespace juce
A message that invokes a callback method when it gets delivered.
Automatically locks and unlocks a mutex object.
void messageCallback() override
Called when the message is delivered.
A bit like an AsyncUpdater, but guarantees that after cancelPendingUpdate() returns,...
bool isUpdatePending() const noexcept
Returns true if there's an update callback in the pipeline.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
LockingAsyncUpdater & operator=(LockingAsyncUpdater &&other) noexcept
Move assignment operator.
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.
void handleUpdateNowIfNeeded()
If an update has been triggered and is pending, this will invoke it synchronously.
LockingAsyncUpdater(std::function< void()> callbackToUse)
Creates a LockingAsyncUpdater object that will call the provided callback on the main thread when tri...
T exchange(T... args)
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T swap(T... args)