OpenShot Audio Library | OpenShotAudio: juce_SharedResourcePointer.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_SharedResourcePointer.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//==============================================================================
80template <typename SharedObjectType>
81 class SharedResourcePointer
82{
83public:
90 SharedResourcePointer() = default;
91
93 SharedResourcePointer (const SharedResourcePointer&) = default;
94
96 SharedResourcePointer (SharedResourcePointer&&) noexcept = default;
97
102 ~SharedResourcePointer() = default;
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
125 static std::optional<SharedResourcePointer> getSharedObjectWithoutCreating()
126 {
127 if (auto sharedPtr = weak().lock())
128 return SharedResourcePointer { std::move (sharedPtr) };
129
130 return {};
131 }
132
133private:
134 explicit SharedResourcePointer (std::shared_ptr<SharedObjectType>&& other) noexcept
135 : sharedObject (std::move (other))
136 {
137 jassert (sharedObject != nullptr);
138 }
139
140 class Weak
141 {
142 public:
143 std::shared_ptr<SharedObjectType> lock()
144 {
145 const SpinLock::ScopedLockType lock { mutex };
146 return ptr.lock();
147 }
148
149 std::shared_ptr<SharedObjectType> lockOrCreate()
150 {
151 const SpinLock::ScopedLockType lock { mutex };
152
153 if (auto locked = ptr.lock())
154 return locked;
155
156 const std::shared_ptr<SharedObjectType> shared (new SharedObjectType());
157 ptr = shared;
158 return shared;
159 }
160
161 private:
162 SpinLock mutex;
163 std::weak_ptr<SharedObjectType> ptr;
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
179 JUCE_LEAK_DETECTOR (SharedResourcePointer)
180};
181
182} // namespace juce
juce::SharedResourcePointer
Definition juce_SharedResourcePointer.h:82
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer(SharedResourcePointer &&) noexcept=default
juce::SharedResourcePointer::getObject
SharedObjectType & getObject() const noexcept
Definition juce_SharedResourcePointer.h:111
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer()=default
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer(const SharedResourcePointer &)=default
juce::SharedResourcePointer::operator*
SharedObjectType & operator*() const noexcept
Definition juce_SharedResourcePointer.h:117
juce::SharedResourcePointer::get
SharedObjectType & get() const noexcept
Definition juce_SharedResourcePointer.h:108
juce::SharedResourcePointer::getSharedObjectWithoutCreating
static std::optional< SharedResourcePointer > getSharedObjectWithoutCreating()
Definition juce_SharedResourcePointer.h:125
juce::SharedResourcePointer::operator->
SharedObjectType * operator->() const noexcept
Definition juce_SharedResourcePointer.h:114
juce::SpinLock::ScopedLockType
GenericScopedLock< SpinLock > ScopedLockType
Definition juce_SpinLock.h:73