LMMS
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:
94
99
105 {
106 auto& holder = getSharedObjectHolder();
107 const SpinLock::ScopedLockType sl (holder.lock);
108
109 if (--(holder.refCount) == 0)
110 holder.sharedInstance = nullptr;
111 }
112
114 operator SharedObjectType*() const noexcept { return sharedObject; }
115
117 SharedObjectType& get() const noexcept { return *sharedObject; }
118
120 SharedObjectType& getObject() const noexcept { return *sharedObject; }
121
123 SharedObjectType* operator->() const noexcept { return sharedObject; }
124
127
128private:
130 {
132 std::unique_ptr<SharedObjectType> sharedInstance;
134 };
135
137 {
138 static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { nullptr };
139 return *reinterpret_cast<SharedObjectHolder*> (holder);
140 }
141
142 SharedObjectType* sharedObject;
143
145 {
146 auto& holder = getSharedObjectHolder();
147 const SpinLock::ScopedLockType sl (holder.lock);
148
149 if (++(holder.refCount) == 1)
150 holder.sharedInstance.reset (new SharedObjectType());
151
152 sharedObject = holder.sharedInstance.get();
153 }
154
155 // There's no need to assign to a SharedResourcePointer because every
156 // instance of the class is exactly the same!
157 SharedResourcePointer& operator= (const SharedResourcePointer&) = delete;
158
160};
161
162} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
~SharedResourcePointer()
Definition juce_SharedResourcePointer.h:104
SharedResourcePointer(const SharedResourcePointer &)
Definition juce_SharedResourcePointer.h:95
int getReferenceCount() const noexcept
Definition juce_SharedResourcePointer.h:126
SharedObjectType & getObject() const noexcept
Definition juce_SharedResourcePointer.h:120
Direct2DFactories * sharedObject
Definition juce_SharedResourcePointer.h:142
SharedResourcePointer()
Definition juce_SharedResourcePointer.h:90
SharedObjectType & get() const noexcept
Definition juce_SharedResourcePointer.h:117
static SharedObjectHolder & getSharedObjectHolder() noexcept
Definition juce_SharedResourcePointer.h:136
void initialise()
Definition juce_SharedResourcePointer.h:144
SharedObjectType * operator->() const noexcept
Definition juce_SharedResourcePointer.h:123
Definition juce_SpinLock.h:42
GenericScopedLock< SpinLock > ScopedLockType
Definition juce_SpinLock.h:73
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition juce_LeakedObjectDetector.h:138
Definition carla_juce.cpp:31
Definition juce_SharedResourcePointer.h:130
std::unique_ptr< SharedObjectType > sharedInstance
Definition juce_SharedResourcePointer.h:132
int refCount
Definition juce_SharedResourcePointer.h:133
SpinLock lock
Definition juce_SharedResourcePointer.h:131
#define const
Definition zconf.h:137