openshot-audio  0.1.6
juce_ScopedPointer.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_SCOPEDPOINTER_H_INCLUDED
30 #define JUCE_SCOPEDPOINTER_H_INCLUDED
31 
32 //==============================================================================
69 template <class ObjectType>
71 {
72 public:
73  //==============================================================================
75  inline ScopedPointer() noexcept : object (nullptr)
76  {
77  }
78 
80  inline ScopedPointer (ObjectType* const objectToTakePossessionOf) noexcept
81  : object (objectToTakePossessionOf)
82  {
83  }
84 
91  ScopedPointer (ScopedPointer& objectToTransferFrom) noexcept
92  : object (objectToTransferFrom.object)
93  {
94  objectToTransferFrom.object = nullptr;
95  }
96 
101 
111  ScopedPointer& operator= (ScopedPointer& objectToTransferFrom)
112  {
113  if (this != objectToTransferFrom.getAddress())
114  {
115  // Two ScopedPointers should never be able to refer to the same object - if
116  // this happens, you must have done something dodgy!
117  jassert (object == nullptr || object != objectToTransferFrom.object);
118 
119  ObjectType* const oldObject = object;
120  object = objectToTransferFrom.object;
121  objectToTransferFrom.object = nullptr;
123  }
124 
125  return *this;
126  }
127 
135  ScopedPointer& operator= (ObjectType* const newObjectToTakePossessionOf)
136  {
137  if (object != newObjectToTakePossessionOf)
138  {
139  ObjectType* const oldObject = object;
140  object = newObjectToTakePossessionOf;
142  }
143 
144  return *this;
145  }
146 
147  #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
149  : object (other.object)
150  {
151  other.object = nullptr;
152  }
153 
155  {
156  object = other.object;
157  other.object = nullptr;
158  return *this;
159  }
160  #endif
161 
162  //==============================================================================
164  inline operator ObjectType*() const noexcept { return object; }
165 
167  inline ObjectType* get() const noexcept { return object; }
168 
170  inline ObjectType& operator*() const noexcept { return *object; }
171 
173  inline ObjectType* operator->() const noexcept { return object; }
174 
175  //==============================================================================
179  ObjectType* release() noexcept { ObjectType* const o = object; object = nullptr; return o; }
180 
181  //==============================================================================
186  {
187  // Two ScopedPointers should never be able to refer to the same object - if
188  // this happens, you must have done something dodgy!
189  jassert (object != other.object || this == other.getAddress() || object == nullptr);
190 
191  std::swap (object, other.object);
192  }
193 
197  inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
198 
199 private:
200  //==============================================================================
201  ObjectType* object;
202 
203  // (Required as an alternative to the overloaded & operator).
204  const ScopedPointer* getAddress() const noexcept { return this; }
205 
206  #if ! JUCE_MSVC // (MSVC can't deal with multiple copy constructors)
207  /* The copy constructors are private to stop people accidentally copying a const ScopedPointer
208  (the compiler would let you do so by implicitly casting the source to its raw object pointer).
209 
210  A side effect of this is that in a compiler that doesn't support C++11, you may hit an
211  error when you write something like this:
212 
213  ScopedPointer<MyClass> m = new MyClass(); // Compile error: copy constructor is private.
214 
215  Even though the compiler would normally ignore the assignment here, it can't do so when the
216  copy constructor is private. It's very easy to fix though - just write it like this:
217 
218  ScopedPointer<MyClass> m (new MyClass()); // Compiles OK
219 
220  It's probably best to use the latter form when writing your object declarations anyway, as
221  this is a better representation of the code that you actually want the compiler to produce.
222  */
224  #endif
225 };
226 
227 //==============================================================================
231 template <class ObjectType>
232 bool operator== (const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
233 {
234  return static_cast<ObjectType*> (pointer1) == pointer2;
235 }
236 
240 template <class ObjectType>
241 bool operator!= (const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
242 {
243  return static_cast<ObjectType*> (pointer1) != pointer2;
244 }
245 
246 //==============================================================================
247 #ifndef DOXYGEN
248 // NB: This is just here to prevent any silly attempts to call deleteAndZero() on a ScopedPointer.
249 template <typename Type>
250 void deleteAndZero (ScopedPointer<Type>&) { static_jassert (sizeof (Type) == 12345); }
251 #endif
252 
253 #endif // JUCE_SCOPEDPOINTER_H_INCLUDED
ObjectType * release() noexcept
Definition: juce_ScopedPointer.h:179
~ScopedPointer()
Definition: juce_ScopedPointer.h:100
#define static_jassert(expression)
Definition: juce_PlatformDefs.h:165
#define noexcept
Definition: juce_CompilerSupport.h:141
ObjectType & operator*() const noexcept
Definition: juce_ScopedPointer.h:170
ScopedPointer(ObjectType *const objectToTakePossessionOf) noexcept
Definition: juce_ScopedPointer.h:80
void deleteAndZero(ScopedPointer< Type > &)
Definition: juce_ScopedPointer.h:250
ObjectType * operator->() const noexcept
Definition: juce_ScopedPointer.h:173
ScopedPointer(ScopedPointer &objectToTransferFrom) noexcept
Definition: juce_ScopedPointer.h:91
void swapWith(ScopedPointer< ObjectType > &other) noexcept
Definition: juce_ScopedPointer.h:185
ObjectType * createCopy() const
Definition: juce_ScopedPointer.h:197
Definition: juce_ScopedPointer.h:70
ScopedPointer() noexcept
Definition: juce_ScopedPointer.h:75
Type * createCopyIfNotNull(const Type *objectToCopy)
Definition: juce_Memory.h:65
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
bool operator!=(const ScopedPointer< ObjectType > &pointer1, ObjectType *const pointer2) noexcept
Definition: juce_ScopedPointer.h:241
bool operator==(const ScopedPointer< ObjectType > &pointer1, ObjectType *const pointer2) noexcept
Definition: juce_ScopedPointer.h:232
ScopedPointer & operator=(ScopedPointer &objectToTransferFrom)
Definition: juce_ScopedPointer.h:111
static void destroy(ObjectType *object)
Definition: juce_ContainerDeletePolicy.h:46