OpenShot Library | libopenshot-audio  0.2.0
juce_ScopedPointer.h
1 
2 /** @weakgroup juce_core-memory
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  This class is deprecated. You should use std::unique_ptr instead.
33 
34 
35  A ScopedPointer holds a pointer that is automatically deleted when the ScopedPointer
36  goes out of scope.
37 
38  Once a pointer has been passed to a ScopedPointer, it will make sure that the pointer
39  gets deleted when the ScopedPointer is deleted. Using the ScopedPointer on the stack or
40  as member variables is a good way to use RAII to avoid accidentally leaking dynamically
41  created objects.
42 
43  A ScopedPointer can be used in pretty much the same way that you'd use a normal pointer
44  to an object. If you use the assignment operator to assign a different object to a
45  ScopedPointer, the old one will be automatically deleted.
46 
47  Important note: The class is designed to hold a pointer to an object, NOT to an array!
48  It calls delete on its payload, not delete[], so do not give it an array to hold! For
49  that kind of purpose, you should be using HeapBlock or Array instead.
50 
51  A const ScopedPointer is guaranteed not to lose ownership of its object or change the
52  object to which it points during its lifetime. This means that making a copy of a const
53  ScopedPointer is impossible, as that would involve the new copy taking ownership from the
54  old one.
55 
56  If you need to get a pointer out of a ScopedPointer without it being deleted, you
57  can use the release() method.
58 
59  @tags{Core}
60 */
61 template <class ObjectType>
63 {
64 public:
65  //==============================================================================
66  /** Creates a ScopedPointer containing a null pointer. */
67  inline ScopedPointer() = default;
68 
69  /** Creates a ScopedPointer containing a null pointer. */
70  inline ScopedPointer (decltype (nullptr)) noexcept {}
71 
72  /** Creates a ScopedPointer that owns the specified object. */
73  inline ScopedPointer (ObjectType* objectToTakePossessionOf) noexcept
74  : object (objectToTakePossessionOf)
75  {
76  }
77 
78  /** Creates a ScopedPointer that takes its pointer from another ScopedPointer.
79 
80  Because a pointer can only belong to one ScopedPointer, this transfers
81  the pointer from the other object to this one, and the other object is reset to
82  be a null pointer.
83  */
84  ScopedPointer (ScopedPointer& objectToTransferFrom) noexcept
85  : object (objectToTransferFrom.release())
86  {
87  }
88 
89  /** Destructor.
90  If the ScopedPointer currently refers to an object, it'll be deleted.
91  */
92  inline ~ScopedPointer() { reset(); }
93 
94  /** Changes this ScopedPointer to point to a new object.
95 
96  Because a pointer can only belong to one ScopedPointer, this transfers
97  the pointer from the other object to this one, and the other object is reset to
98  be a null pointer.
99 
100  If this ScopedPointer already points to an object, that object
101  will first be deleted.
102  */
103  ScopedPointer& operator= (ScopedPointer& objectToTransferFrom)
104  {
105  if (this != objectToTransferFrom.getAddress())
106  {
107  // Two ScopedPointers should never be able to refer to the same object - if
108  // this happens, you must have done something dodgy!
109  jassert (object == nullptr || object != objectToTransferFrom.object);
110  reset (objectToTransferFrom.release());
111  }
112 
113  return *this;
114  }
115 
116  /** Changes this ScopedPointer to point to a new object.
117  If this ScopedPointer already points to an object, that object will first be deleted.
118  The pointer that you pass in may be a nullptr.
119  */
120  ScopedPointer& operator= (ObjectType* newObjectToTakePossessionOf)
121  {
122  reset (newObjectToTakePossessionOf);
123  return *this;
124  }
125 
126  /** Take ownership of another ScopedPointer */
127  ScopedPointer (ScopedPointer&& other) noexcept : object (other.object)
128  {
129  other.object = nullptr;
130  }
131 
132  /** Take ownership of another ScopedPointer */
134  {
135  reset (other.release());
136  return *this;
137  }
138 
139  //==============================================================================
140  /** Returns the object that this ScopedPointer refers to. */
141  inline operator ObjectType*() const noexcept { return object; }
142 
143  /** Returns the object that this ScopedPointer refers to. */
144  inline ObjectType* get() const noexcept { return object; }
145 
146  /** Returns the object that this ScopedPointer refers to. */
147  inline ObjectType& operator*() const noexcept { return *object; }
148 
149  /** Lets you access methods and properties of the object that this ScopedPointer refers to. */
150  inline ObjectType* operator->() const noexcept { return object; }
151 
152  //==============================================================================
153  /** Clears this pointer, deleting the object it points to if there is one. */
154  void reset()
155  {
156  auto* oldObject = object;
157  object = {};
159  }
160 
161  /** Sets this pointer to a new object, deleting the old object that it was previously pointing to if there was one. */
162  void reset (ObjectType* newObject)
163  {
164  if (object != newObject)
165  {
166  auto* oldObject = object;
167  object = newObject;
169  }
170  else
171  {
172  // You're trying to reset this ScopedPointer to itself! This will work here as ScopedPointer does an equality check
173  // but be aware that std::unique_ptr won't do this and you could end up with some nasty, subtle bugs!
174  jassert (newObject == nullptr);
175  }
176  }
177 
178  /** Sets this pointer to a new object, deleting the old object that it was previously pointing to if there was one. */
179  void reset (ScopedPointer& newObject)
180  {
181  reset (newObject.release());
182  }
183 
184  /** Detaches and returns the current object from this ScopedPointer without deleting it.
185  This will return the current object, and set the ScopedPointer to a null pointer.
186  */
187  ObjectType* release() noexcept { auto* o = object; object = {}; return o; }
188 
189  //==============================================================================
190  /** Swaps this object with that of another ScopedPointer.
191  The two objects simply exchange their pointers.
192  */
193  void swapWith (ScopedPointer<ObjectType>& other) noexcept
194  {
195  // Two ScopedPointers should never be able to refer to the same object - if
196  // this happens, you must have done something dodgy!
197  jassert (object != other.object || this == other.getAddress() || object == nullptr);
198 
199  std::swap (object, other.object);
200  }
201 
202  /** If the pointer is non-null, this will attempt to return a new copy of the object that is pointed to.
203  If the pointer is null, this will safely return a nullptr.
204  */
205  inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
206 
207 private:
208  //==============================================================================
209  ObjectType* object = nullptr;
210 
211  const ScopedPointer* getAddress() const noexcept { return this; } // Used internally to avoid the & operator
212 
213  #if ! JUCE_MSVC // (MSVC can't deal with multiple copy constructors)
214  ScopedPointer (const ScopedPointer&) = delete;
215  ScopedPointer& operator= (const ScopedPointer&) = delete;
216  #endif
217 };
218 
219 //==============================================================================
220 /** Compares a ScopedPointer with another pointer. */
221 template <typename ObjectType1, typename ObjectType2>
222 bool operator== (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
223 {
224  return pointer1 == pointer2.get();
225 }
226 
227 /** Compares a ScopedPointer with another pointer. */
228 template <typename ObjectType1, typename ObjectType2>
229 bool operator!= (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
230 {
231  return pointer1 != pointer2.get();
232 }
233 
234 /** Compares a ScopedPointer with another pointer. */
235 template <typename ObjectType1, typename ObjectType2>
236 bool operator== (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
237 {
238  return pointer1.get() == pointer2;
239 }
240 
241 /** Compares a ScopedPointer with another pointer. */
242 template <typename ObjectType1, typename ObjectType2>
243 bool operator!= (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
244 {
245  return pointer1.get() != pointer2;
246 }
247 
248 /** Compares a ScopedPointer with another pointer. */
249 template <typename ObjectType1, typename ObjectType2>
250 bool operator== (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
251 {
252  return pointer1.get() == pointer2.get();
253 }
254 
255 /** Compares a ScopedPointer with another pointer. */
256 template <typename ObjectType1, typename ObjectType2>
257 bool operator!= (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
258 {
259  return pointer1.get() != pointer2.get();
260 }
261 
262 /** Compares a ScopedPointer with a nullptr. */
263 template <class ObjectType>
264 bool operator== (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
265 {
266  return pointer.get() == nullptr;
267 }
268 
269 /** Compares a ScopedPointer with a nullptr. */
270 template <class ObjectType>
271 bool operator!= (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
272 {
273  return pointer.get() != nullptr;
274 }
275 
276 /** Compares a ScopedPointer with a nullptr. */
277 template <class ObjectType>
278 bool operator== (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
279 {
280  return pointer.get() == nullptr;
281 }
282 
283 /** Compares a ScopedPointer with a nullptr. */
284 template <class ObjectType>
285 bool operator!= (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
286 {
287  return pointer.get() != nullptr;
288 }
289 
290 //==============================================================================
291 #ifndef DOXYGEN
292 // NB: This is just here to prevent any silly attempts to call deleteAndZero() on a ScopedPointer.
293 template <typename Type>
294 void deleteAndZero (ScopedPointer<Type>&) { static_assert (sizeof (Type) == 12345,
295  "Attempt to call deleteAndZero() on a ScopedPointer"); }
296 #endif
297 
298 } // namespace juce
299 
300 /** @}*/
juce::ScopedPointer::reset
void reset(ObjectType *newObject)
Sets this pointer to a new object, deleting the old object that it was previously pointing to if ther...
Definition: juce_ScopedPointer.h:162
juce::ScopedPointer::operator*
ObjectType & operator*() const noexcept
Returns the object that this ScopedPointer refers to.
Definition: juce_ScopedPointer.h:147
juce::ScopedPointer::release
ObjectType * release() noexcept
Detaches and returns the current object from this ScopedPointer without deleting it.
Definition: juce_ScopedPointer.h:187
juce::ScopedPointer::reset
void reset(ScopedPointer &newObject)
Sets this pointer to a new object, deleting the old object that it was previously pointing to if ther...
Definition: juce_ScopedPointer.h:179
juce::ScopedPointer::reset
void reset()
Clears this pointer, deleting the object it points to if there is one.
Definition: juce_ScopedPointer.h:154
juce::ScopedPointer::ScopedPointer
ScopedPointer(decltype(nullptr)) noexcept
Creates a ScopedPointer containing a null pointer.
Definition: juce_ScopedPointer.h:70
juce::ScopedPointer::ScopedPointer
ScopedPointer(ScopedPointer &objectToTransferFrom) noexcept
Creates a ScopedPointer that takes its pointer from another ScopedPointer.
Definition: juce_ScopedPointer.h:84
juce::ScopedPointer::operator->
ObjectType * operator->() const noexcept
Lets you access methods and properties of the object that this ScopedPointer refers to.
Definition: juce_ScopedPointer.h:150
juce::ScopedPointer::ScopedPointer
ScopedPointer()=default
Creates a ScopedPointer containing a null pointer.
juce::ScopedPointer
This class is deprecated.
Definition: juce_ScopedPointer.h:62
juce::ScopedPointer::createCopy
ObjectType * createCopy() const
If the pointer is non-null, this will attempt to return a new copy of the object that is pointed to.
Definition: juce_ScopedPointer.h:205
juce::ScopedPointer::~ScopedPointer
~ScopedPointer()
Destructor.
Definition: juce_ScopedPointer.h:92
juce::ContainerDeletePolicy
Used by container classes as an indirect way to delete an object of a particular type.
Definition: juce_ContainerDeletePolicy.h:44
juce::ScopedPointer::operator=
ScopedPointer & operator=(ScopedPointer &objectToTransferFrom)
Changes this ScopedPointer to point to a new object.
Definition: juce_ScopedPointer.h:103
juce::ScopedPointer::ScopedPointer
ScopedPointer(ScopedPointer &&other) noexcept
Take ownership of another ScopedPointer.
Definition: juce_ScopedPointer.h:127
juce::ScopedPointer::swapWith
void swapWith(ScopedPointer< ObjectType > &other) noexcept
Swaps this object with that of another ScopedPointer.
Definition: juce_ScopedPointer.h:193
juce::ScopedPointer::get
ObjectType * get() const noexcept
Returns the object that this ScopedPointer refers to.
Definition: juce_ScopedPointer.h:144
juce::ScopedPointer::ScopedPointer
ScopedPointer(ObjectType *objectToTakePossessionOf) noexcept
Creates a ScopedPointer that owns the specified object.
Definition: juce_ScopedPointer.h:73