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_SharedResourcePointer.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//==============================================================================
80template <typename SharedObjectType>
82{
83public:
91
94
97
103
105 operator SharedObjectType*() const noexcept { return sharedObject.get(); }
106
108 SharedObjectType& get() const noexcept { return *sharedObject; }
109
111 SharedObjectType& getObject() const noexcept { return *sharedObject; }
112
114 SharedObjectType* operator->() const noexcept { return sharedObject.get(); }
115
117 SharedObjectType& operator*() const noexcept { return *sharedObject; }
118
119 #ifndef DOXYGEN
120 [[deprecated ("If you are relying on this function please inform the JUCE team as we are planing on removing this in a subsequent release")]]
121 int getReferenceCount() const noexcept { return (int) sharedObject.use_count(); }
122 #endif
123
126 {
127 if (auto sharedPtr = weak().lock())
128 return SharedResourcePointer { std::move (sharedPtr) };
129
130 return {};
131 }
132
133private:
135 : sharedObject (std::move (other))
136 {
137 jassert (sharedObject != nullptr);
138 }
139
140 class Weak
141 {
142 public:
144 {
145 const SpinLock::ScopedLockType lock { mutex };
146 return ptr.lock();
147 }
148
150 {
151 const SpinLock::ScopedLockType lock { mutex };
152
153 if (auto locked = ptr.lock())
154 return locked;
155
157 ptr = shared;
158 return shared;
159 }
160
161 private:
162 SpinLock mutex;
164 };
165
166 inline static Weak& weak()
167 {
168 static Weak weak;
169 return weak;
170 }
171
172 std::shared_ptr<SharedObjectType> sharedObject = weak().lockOrCreate();
173
174 // There's no need to assign to a SharedResourcePointer because every
175 // instance of the class is exactly the same!
176 SharedResourcePointer& operator= (const SharedResourcePointer&) = delete;
177 SharedResourcePointer& operator= (SharedResourcePointer&&) noexcept = delete;
178
180};
181
182} // namespace juce
A smart-pointer that automatically creates and manages the lifetime of a shared static instance of a ...
SharedResourcePointer(SharedResourcePointer &&) noexcept=default
Move constructor.
SharedObjectType & getObject() const noexcept
Returns a reference to the shared object.
SharedResourcePointer()=default
Creates an instance of the shared object.
SharedResourcePointer(const SharedResourcePointer &)=default
Copy constructor.
SharedObjectType & operator*() const noexcept
Returns a reference to the shared object.
SharedObjectType & get() const noexcept
Returns a reference to the shared object.
static std::optional< SharedResourcePointer > getSharedObjectWithoutCreating()
Returns the SharedResourcePointer if one already exists, or a null optional otherwise.
SharedObjectType * operator->() const noexcept
Returns a pointer to the shared object.
GenericScopedLock< SpinLock > ScopedLockType
Provides the type of scoped lock to use for locking a SpinLock.
T get(T... args)
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define jassert(expression)
Platform-independent assertion macro.
T lock(T... args)
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
IPtr< I > shared(I *p)
Assigning shared object to an IPtr.
T use_count(T... args)