openshot-audio  0.1.5
juce_SharedResourcePointer.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED
30 #define JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED
31 
32 
33 //==============================================================================
85 template <typename SharedObjectType>
87 {
88 public:
96  {
97  initialise();
98  }
99 
101  {
102  initialise();
103  }
104 
110  {
111  SharedObjectHolder& holder = getSharedObjectHolder();
112  const SpinLock::ScopedLockType sl (holder.lock);
113 
114  if (--(holder.refCount) == 0)
115  holder.sharedInstance = nullptr;
116  }
117 
119  operator SharedObjectType*() const noexcept { return sharedObject; }
120 
122  SharedObjectType& get() const noexcept { return *sharedObject; }
123 
127  SharedObjectType& getObject() const noexcept { return *sharedObject; }
128 
129  SharedObjectType* operator->() const noexcept { return sharedObject; }
130 
131 private:
132  struct SharedObjectHolder : public ReferenceCountedObject
133  {
134  SpinLock lock;
135  ScopedPointer<SharedObjectType> sharedInstance;
136  int refCount;
137  };
138 
139  static SharedObjectHolder& getSharedObjectHolder() noexcept
140  {
141  static void* holder [(sizeof (SharedObjectHolder) + sizeof(void*) - 1) / sizeof(void*)] = { 0 };
142  return *reinterpret_cast<SharedObjectHolder*> (holder);
143  }
144 
145  SharedObjectType* sharedObject;
146 
147  void initialise()
148  {
149  SharedObjectHolder& holder = getSharedObjectHolder();
150  const SpinLock::ScopedLockType sl (holder.lock);
151 
152  if (++(holder.refCount) == 1)
153  holder.sharedInstance = new SharedObjectType();
154 
155  sharedObject = holder.sharedInstance;
156  }
157 
158  // There's no need to assign to a SharedResourcePointer because every
159  // instance of the class is exactly the same!
161 
163 };
164 
165 
166 #endif // JUCE_SHAREDRESOURCEPOINTER_H_INCLUDED
#define noexcept
Definition: juce_CompilerSupport.h:141
Definition: juce_ScopedLock.h:59
SharedResourcePointer(const SharedResourcePointer &)
Definition: juce_SharedResourcePointer.h:100
SharedResourcePointer()
Definition: juce_SharedResourcePointer.h:95
Definition: juce_SpinLock.h:46
~SharedResourcePointer()
Definition: juce_SharedResourcePointer.h:109
SharedObjectType & getObject() const noexcept
Definition: juce_SharedResourcePointer.h:127
Definition: juce_ReferenceCountedObject.h:65
Definition: juce_ScopedPointer.h:70
#define JUCE_DELETED_FUNCTION
Definition: juce_CompilerSupport.h:133
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition: juce_LeakedObjectDetector.h:141
SharedObjectType * operator->() const noexcept
Definition: juce_SharedResourcePointer.h:129
Definition: juce_SharedResourcePointer.h:86