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
23namespace juce
24{
25
26//==============================================================================
76template <class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
78{
79public:
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
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
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
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
184 {
185 if (sharedPointer != nullptr)
186 sharedPointer->clearPointer();
187 }
188
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
201private:
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
ReferencedType * get() const noexcept
int getNumActiveWeakReferences() const noexcept
SharedRef getSharedPointer(ObjectType *object)
ObjectType * operator->() const noexcept
WeakReference()=default
ObjectType * get() const noexcept
WeakReference(WeakReference &&other) noexcept
WeakReference & operator=(const WeakReference &other)
WeakReference(const WeakReference &other) noexcept
bool wasObjectDeleted() const noexcept
WeakReference(ObjectType *object)