openshot-audio  0.1.4
juce_WeakReference.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_WEAKREFERENCE_H_INCLUDED
30 #define JUCE_WEAKREFERENCE_H_INCLUDED
31 
32 
33 //==============================================================================
81 template <class ObjectType, class ReferenceCountingType = ReferenceCountedObject>
83 {
84 public:
86  inline WeakReference() noexcept {}
87 
89  WeakReference (ObjectType* const object) : holder (getRef (object)) {}
90 
92  WeakReference (const WeakReference& other) noexcept : holder (other.holder) {}
93 
95  WeakReference& operator= (const WeakReference& other) { holder = other.holder; return *this; }
96 
98  WeakReference& operator= (ObjectType* const newObject) { holder = getRef (newObject); return *this; }
99 
100  #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
101  WeakReference (WeakReference&& other) noexcept : holder (static_cast <SharedRef&&> (other.holder)) {}
102  WeakReference& operator= (WeakReference&& other) noexcept { holder = static_cast <SharedRef&&> (other.holder); return *this; }
103  #endif
104 
106  ObjectType* get() const noexcept { return holder != nullptr ? holder->get() : nullptr; }
107 
109  operator ObjectType*() const noexcept { return get(); }
110 
112  ObjectType* operator->() noexcept { return get(); }
113 
115  const ObjectType* operator->() const noexcept { return get(); }
116 
124  bool wasObjectDeleted() const noexcept { return holder != nullptr && holder->get() == nullptr; }
125 
126  bool operator== (ObjectType* const object) const noexcept { return get() == object; }
127  bool operator!= (ObjectType* const object) const noexcept { return get() != object; }
128 
129  //==============================================================================
134  class SharedPointer : public ReferenceCountingType
135  {
136  public:
137  explicit SharedPointer (ObjectType* const obj) noexcept : owner (obj) {}
138 
139  inline ObjectType* get() const noexcept { return owner; }
140  void clearPointer() noexcept { owner = nullptr; }
141 
142  private:
143  ObjectType* volatile owner;
144 
146  };
147 
149 
150  //==============================================================================
156  class Master
157  {
158  public:
160 
162  {
163  // You must remember to call clear() in your source object's destructor! See the notes
164  // for the WeakReference class for an example of how to do this.
165  jassert (sharedPointer == nullptr || sharedPointer->get() == nullptr);
166  }
167 
171  SharedPointer* getSharedPointer (ObjectType* const object)
172  {
173  if (sharedPointer == nullptr)
174  {
175  sharedPointer = new SharedPointer (object);
176  }
177  else
178  {
179  // You're trying to create a weak reference to an object that has already been deleted!!
180  jassert (sharedPointer->get() != nullptr);
181  }
182 
183  return sharedPointer;
184  }
185 
191  {
192  if (sharedPointer != nullptr)
193  sharedPointer->clearPointer();
194  }
195 
196  private:
197  SharedRef sharedPointer;
198 
200  };
201 
202 private:
203  SharedRef holder;
204 
205  static inline SharedPointer* getRef (ObjectType* const o)
206  {
207  return (o != nullptr) ? o->masterReference.getSharedPointer (o) : nullptr;
208  }
209 };
210 
211 
212 #endif // JUCE_WEAKREFERENCE_H_INCLUDED
void clear() noexcept
Definition: juce_WeakReference.h:190
WeakReference() noexcept
Definition: juce_WeakReference.h:86
#define noexcept
Definition: juce_CompilerSupport.h:141
Definition: juce_WeakReference.h:134
~Master() noexcept
Definition: juce_WeakReference.h:161
Definition: juce_WeakReference.h:156
bool operator==(ObjectType *const object) const noexcept
Definition: juce_WeakReference.h:126
ObjectType * operator->() noexcept
Definition: juce_WeakReference.h:112
WeakReference(ObjectType *const object)
Definition: juce_WeakReference.h:89
ReferencedType * get() const noexcept
Definition: juce_ReferenceCountedObject.h:339
SharedPointer(ObjectType *const obj) noexcept
Definition: juce_WeakReference.h:137
Definition: juce_WeakReference.h:82
const ObjectType * operator->() const noexcept
Definition: juce_WeakReference.h:115
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
WeakReference & operator=(const WeakReference &other)
Definition: juce_WeakReference.h:95
ReferenceCountedObjectPtr< SharedPointer > SharedRef
Definition: juce_WeakReference.h:148
WeakReference(const WeakReference &other) noexcept
Definition: juce_WeakReference.h:92
Master() noexcept
Definition: juce_WeakReference.h:159
bool wasObjectDeleted() const noexcept
Definition: juce_WeakReference.h:124
bool operator!=(ObjectType *const object) const noexcept
Definition: juce_WeakReference.h:127
SharedPointer * getSharedPointer(ObjectType *const object)
Definition: juce_WeakReference.h:171
void clearPointer() noexcept
Definition: juce_WeakReference.h:140