OpenShot Audio Library | OpenShotAudio: juce_CallbackListenerList.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0 |

Loading...
Searching...
No Matches
juce_CallbackListenerList.h
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 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::detail
27{
28
29template <typename... Ts>
30constexpr bool isValueOrLvalueReferenceToConst()
31{
32 return (( (! std::is_reference_v<Ts>)
33 || (std::is_lvalue_reference_v<Ts> && std::is_const_v<std::remove_reference_t<Ts>>)) && ...);
34}
35
36template <typename... Args>
37class CallbackListenerList
38{
39public:
40 static_assert (isValueOrLvalueReferenceToConst<Args...>(),
41 "CallbackListenerList can only forward values or const lvalue references");
42
43 using Callback = std::function<void (Args...)>;
44
45 ErasedScopeGuard addListener (Callback callback)
46 {
47 jassert (callback != nullptr);
48
49 const auto it = callbacks.insert (callbacks.end(), std::move (callback));
50 listeners.add (&*it);
51
52 return ErasedScopeGuard { [this, it]
53 {
54 listeners.remove (&*it);
55 callbacks.erase (it);
56 } };
57 }
58
59 void call (Args... args)
60 {
61 listeners.call ([&] (auto& l) { l (std::forward<Args> (args)...); });
62 }
63
64private:
65 std::list<Callback> callbacks;
66 ListenerList<Callback> listeners;
67};
68
69} // namespace juce::detail