LMMS
Loading...
Searching...
No Matches
SharedResourcePointer.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2019 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_SHAREDRESOURCEPOINTER_H_INCLUDED
27#define WATER_SHAREDRESOURCEPOINTER_H_INCLUDED
28
30#include "../threads/SpinLock.h"
31
32#include "CarlaScopeUtils.hpp"
33
34namespace water {
35
36//==============================================================================
88template <typename SharedObjectType>
90{
91public:
103
104 SharedResourcePointer(const char* const v1, const char* const v2)
106 {
107 initialise_v2(v1, v2);
108 }
109
115
121 {
123 const SpinLock::ScopedLockType sl (holder.lock);
124
125 if (--(holder.refCount) == 0)
126 holder.sharedInstance = nullptr;
127 }
128
130 operator SharedObjectType*() const noexcept { return sharedObject; }
131
133 SharedObjectType& get() const noexcept { return *sharedObject; }
134
138 SharedObjectType& getObject() const noexcept { return *sharedObject; }
139 SharedObjectType* getPointer() const noexcept { return sharedObject; }
140
141 SharedObjectType* operator->() const noexcept { return sharedObject; }
142
143private:
145 {
147 CarlaScopedPointer<SharedObjectType> sharedInstance;
149 };
150
152 {
153 static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { nullptr };
154 return *reinterpret_cast<SharedObjectHolder*> (holder);
155 }
156
157 SharedObjectType* sharedObject;
158
160 {
162 const SpinLock::ScopedLockType sl (holder.lock);
163
164 if (++(holder.refCount) == 1)
165 holder.sharedInstance = new SharedObjectType();
166
168 }
169
170 void initialise_v2(const char* const v1, const char* const v2)
171 {
173 const SpinLock::ScopedLockType sl (holder.lock);
174
175 if (++(holder.refCount) == 1)
176 holder.sharedInstance = new SharedObjectType(v1, v2);
177
179 }
180
181 // There's no need to assign to a SharedResourcePointer because every
182 // instance of the class is exactly the same!
184};
185
186}
187
188#endif // WATER_SHAREDRESOURCEPOINTER_H_INCLUDED
#define noexcept
Definition DistrhoDefines.h:72
#define nullptr
Definition DistrhoDefines.h:75
ReferenceCountedObject()
Definition ReferenceCountedObject.h:107
SharedObjectType * sharedObject
Definition SharedResourcePointer.h:157
void initialise_v2(const char *const v1, const char *const v2)
Definition SharedResourcePointer.h:170
SharedResourcePointer(const char *const v1, const char *const v2)
Definition SharedResourcePointer.h:104
SharedObjectType * getPointer() const noexcept
Definition SharedResourcePointer.h:139
static SharedObjectHolder & getSharedObjectHolder() noexcept
Definition SharedResourcePointer.h:151
SharedResourcePointer(const SharedResourcePointer &)
Definition SharedResourcePointer.h:110
SharedResourcePointer()
Definition SharedResourcePointer.h:98
SharedResourcePointer & operator=(const SharedResourcePointer &) WATER_DELETED_FUNCTION
SharedObjectType & get() const noexcept
Definition SharedResourcePointer.h:133
void initialise()
Definition SharedResourcePointer.h:159
~SharedResourcePointer()
Definition SharedResourcePointer.h:120
SharedObjectType * operator->() const noexcept
Definition SharedResourcePointer.h:141
SharedObjectType & getObject() const noexcept
Definition SharedResourcePointer.h:138
Definition SpinLock.h:48
GenericScopedLock< SpinLock > ScopedLockType
Definition SpinLock.h:96
static void v2(register WDL_FFT_REAL *a)
Definition fft.c:1099
#define WATER_DELETED_FUNCTION
Definition AudioSampleBuffer.h:33
Definition SharedResourcePointer.h:145
CarlaScopedPointer< SharedObjectType > sharedInstance
Definition SharedResourcePointer.h:147
SpinLock lock
Definition SharedResourcePointer.h:146
int refCount
Definition SharedResourcePointer.h:148
#define const
Definition zconf.h:137