OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_WeakReference.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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//==============================================================================
76 template <class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
77 class WeakReference
78{
79 public:
81 inline WeakReference() = default;
82
84 WeakReference (ObjectType* object) : holder (getRef (object)) {}
85
87 WeakReference (const WeakReference& other) noexcept : holder (other.holder) {}
88
90 WeakReference (WeakReference&& other) noexcept : holder (std::move (other.holder)) {}
91
93 WeakReference& operator= (const WeakReference& other) { holder = other.holder; return *this; }
94
96 WeakReference& operator= (ObjectType* newObject) { holder = getRef (newObject); return *this; }
97
99 WeakReference& operator= (WeakReference&& other) noexcept { holder = std::move (other.holder); return *this; }
100
102 ObjectType* get() const noexcept { return holder != nullptr ? holder->get() : nullptr; }
103
105 operator ObjectType*() const noexcept { return get(); }
106
108 ObjectType* operator->() const noexcept { return get(); }
109
117 bool wasObjectDeleted() const noexcept { return holder != nullptr && holder->get() == nullptr; }
118
119 bool operator== (ObjectType* object) const noexcept { return get() == object; }
120 bool operator!= (ObjectType* object) const noexcept { return get() != object; }
121
122 //==============================================================================
127 class SharedPointer : public ReferenceCountingType
128 {
129 public:
130 explicit SharedPointer (ObjectType* obj) noexcept : owner (obj) {}
131
132 inline ObjectType* get() const noexcept { return owner; }
133 void clearPointer() noexcept { owner = nullptr; }
134
135 private:
136 ObjectType* owner;
137
138 JUCE_DECLARE_NON_COPYABLE (SharedPointer)
139 };
140
141 using SharedRef = ReferenceCountedObjectPtr<SharedPointer>;
142
143 //==============================================================================
149 class Master
150 {
151 public:
152 Master() = default;
153
154 ~Master() noexcept
155 {
156 // You must remember to call clear() in your source object's destructor! See the notes
157 // for the WeakReference class for an example of how to do this.
158 jassert (sharedPointer == nullptr || sharedPointer->get() == nullptr);
159 }
160
164 SharedRef getSharedPointer (ObjectType* object)
165 {
166 if (sharedPointer == nullptr)
167 {
168 sharedPointer = *new SharedPointer (object);
169 }
170 else
171 {
172 // You're trying to create a weak reference to an object that has already been deleted!!
173 jassert (sharedPointer->get() != nullptr);
174 }
175
176 return sharedPointer;
177 }
178
183 void clear() noexcept
184 {
185 if (sharedPointer != nullptr)
186 sharedPointer->clearPointer();
187 }
188
190 int getNumActiveWeakReferences() const noexcept
191 {
192 return sharedPointer == nullptr ? 0 : (sharedPointer->getReferenceCount() - 1);
193 }
194
195 private:
196 SharedRef sharedPointer;
197
198 JUCE_DECLARE_NON_COPYABLE (Master)
199 };
200
201 private:
202 SharedRef holder;
203
204 static inline SharedRef getRef (ObjectType* o)
205 {
206 if (o != nullptr)
207 return o->masterReference.getSharedPointer (o);
208
209 return {};
210 }
211};
212
213
214//==============================================================================
234#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class) \
235 struct WeakRefMaster : public juce::WeakReference<Class>::Master { ~WeakRefMaster() { this->clear(); } }; \
236 WeakRefMaster masterReference; \
237 friend class juce::WeakReference<Class>; \
238
239
240} // namespace juce
juce::ReferenceCountedObjectPtr< SharedPointer >
juce::ReferenceCountedObjectPtr::get
ReferencedType * get() const noexcept
Definition: juce_ReferenceCountedObject.h:381
juce::WeakReference::Master
Definition: juce_WeakReference.h:150
juce::WeakReference::Master::getNumActiveWeakReferences
int getNumActiveWeakReferences() const noexcept
Definition: juce_WeakReference.h:190
juce::WeakReference::Master::clear
void clear() noexcept
Definition: juce_WeakReference.h:183
juce::WeakReference::Master::getSharedPointer
SharedRef getSharedPointer(ObjectType *object)
Definition: juce_WeakReference.h:164
juce::WeakReference::SharedPointer
Definition: juce_WeakReference.h:128
juce::WeakReference
Definition: juce_WeakReference.h:78
juce::WeakReference::operator->
ObjectType * operator->() const noexcept
Definition: juce_WeakReference.h:108
juce::WeakReference::WeakReference
WeakReference()=default
juce::WeakReference::get
ObjectType * get() const noexcept
Definition: juce_WeakReference.h:102
juce::WeakReference::WeakReference
WeakReference(WeakReference &&other) noexcept
Definition: juce_WeakReference.h:90
juce::WeakReference::operator=
WeakReference & operator=(const WeakReference &other)
Definition: juce_WeakReference.h:93
juce::WeakReference::WeakReference
WeakReference(const WeakReference &other) noexcept
Definition: juce_WeakReference.h:87
juce::WeakReference::wasObjectDeleted
bool wasObjectDeleted() const noexcept
Definition: juce_WeakReference.h:117
juce::WeakReference::WeakReference
WeakReference(ObjectType *object)
Definition: juce_WeakReference.h:84