OpenShot Audio Library | OpenShotAudio  0.6.0
juce_LockingAsyncUpdater.cpp
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 
23 namespace juce
24 {
25 
26 class LockingAsyncUpdater::Impl : public CallbackMessage
27 {
28 public:
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 
74 private:
75  CriticalSection mutex;
76  std::function<void()> callback;
77  bool deliver = false;
78 };
79 
80 //==============================================================================
81 LockingAsyncUpdater::LockingAsyncUpdater (std::function<void()> callbackToUse)
82  : impl (new Impl (std::move (callbackToUse))) {}
83 
85  : impl (std::exchange (other.impl, nullptr)) {}
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
LockingAsyncUpdater & operator=(LockingAsyncUpdater &&other) noexcept
LockingAsyncUpdater(std::function< void()> callbackToUse)