OpenShot Audio Library | OpenShotAudio 0.4.0
Loading...
Searching...
No Matches
juce_WeakReference.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 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
108 ObjectType* operator->() const noexcept { return get(); }
109
117 bool wasObjectDeleted() const noexcept { return holder != nullptr && holder->get() == nullptr; }
118
119 //==============================================================================
124 class SharedPointer : public ReferenceCountingType
125 {
126 public:
127 explicit SharedPointer (ObjectType* obj) noexcept : owner (obj) {}
128
129 inline ObjectType* get() const noexcept { return owner; }
130 void clearPointer() noexcept { owner = nullptr; }
131
132 private:
133 ObjectType* owner;
134
135 JUCE_DECLARE_NON_COPYABLE (SharedPointer)
136 };
137
139
140 //==============================================================================
146 class Master
147 {
148 public:
149 Master() = default;
150
151 ~Master() noexcept
152 {
153 // You must remember to call clear() in your source object's destructor! See the notes
154 // for the WeakReference class for an example of how to do this.
155 jassert (sharedPointer == nullptr || sharedPointer->get() == nullptr);
156 }
157
161 SharedRef getSharedPointer (ObjectType* object)
162 {
163 if (sharedPointer == nullptr)
164 {
165 sharedPointer = *new SharedPointer (object);
166 }
167 else
168 {
169 // You're trying to create a weak reference to an object that has already been deleted!!
170 jassert (sharedPointer->get() != nullptr);
171 }
172
173 return sharedPointer;
174 }
175
180 void clear() noexcept
181 {
182 if (sharedPointer != nullptr)
183 sharedPointer->clearPointer();
184 }
185
187 int getNumActiveWeakReferences() const noexcept
188 {
189 return sharedPointer == nullptr ? 0 : (sharedPointer->getReferenceCount() - 1);
190 }
191
192 private:
193 SharedRef sharedPointer;
194
195 JUCE_DECLARE_NON_COPYABLE (Master)
196 };
197
198private:
199 SharedRef holder;
200
201 static SharedRef getRef (ObjectType* o)
202 {
203 if (o != nullptr)
204 return o->masterReference.getSharedPointer (o);
205
206 return {};
207 }
208};
209
210
211//==============================================================================
231#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class) \
232 struct WeakRefMaster : public juce::WeakReference<Class>::Master { ~WeakRefMaster() { this->clear(); } }; \
233 WeakRefMaster masterReference; \
234 friend class juce::WeakReference<Class>; \
235
236
237} // 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)