JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_WeakReference.h
Go to the documentation of this file.
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 //==============================================================================
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
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
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
188 {
189 return sharedPointer == nullptr ? 0 : (sharedPointer->getReferenceCount() - 1);
190 }
191
192 private:
193 SharedRef sharedPointer;
194
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
Returns the object that this pointer references.
This class is embedded inside an object to which you want to attach WeakReference pointers.
int getNumActiveWeakReferences() const noexcept
Returns the number of WeakReferences that are out there pointing to this object.
void clear() noexcept
The object that owns this master pointer should call this before it gets destroyed,...
SharedRef getSharedPointer(ObjectType *object)
The first call to this method will create an internal object that is shared by all weak references to...
This class is used internally by the WeakReference class - don't use it directly in your code!
This class acts as a pointer which will automatically become null if the object to which it points is...
ObjectType * operator->() const noexcept
Returns the object that this pointer refers to, or null if the object no longer exists.
WeakReference()=default
Creates a null WeakReference.
ObjectType * get() const noexcept
Returns the object that this pointer refers to, or null if the object no longer exists.
WeakReference(WeakReference &&other) noexcept
Move constructor.
WeakReference & operator=(const WeakReference &other)
Copies another pointer to this one.
WeakReference(const WeakReference &other) noexcept
Creates a copy of another WeakReference.
bool wasObjectDeleted() const noexcept
This returns true if this reference has been pointing at an object, but that object has since been de...
WeakReference(ObjectType *object)
Creates a WeakReference that points at the given object.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88