OpenShot Library | OpenShotAudio  0.2.2
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 #ifndef DOXYGEN
28 
29 namespace juce
30 {
31 
32 //==============================================================================
33 /**
34  This class is deprecated. You should use std::unique_ptr instead.
35 */
36 template <class ObjectType>
37 class ScopedPointer
38 {
39 public:
40  //==============================================================================
41  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
42  JUCE_DEPRECATED_ATTRIBUTE inline ScopedPointer() = default;
43 
44  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
45  JUCE_DEPRECATED_ATTRIBUTE inline ScopedPointer (decltype (nullptr)) noexcept {}
46 
47  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
48  JUCE_DEPRECATED_ATTRIBUTE inline ScopedPointer (ObjectType* objectToTakePossessionOf) noexcept
49  : object (objectToTakePossessionOf)
50  {
51  }
52 
53  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
54  ScopedPointer (ScopedPointer& objectToTransferFrom) noexcept
55  : object (objectToTransferFrom.release())
56  {
57  }
58 
59  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
60  JUCE_DEPRECATED_ATTRIBUTE inline ~ScopedPointer() { reset(); }
61 
62  ScopedPointer& operator= (ScopedPointer& objectToTransferFrom)
63  {
64  if (this != objectToTransferFrom.getAddress())
65  {
66  // Two ScopedPointers should never be able to refer to the same object - if
67  // this happens, you must have done something dodgy!
68  jassert (object == nullptr || object != objectToTransferFrom.object);
69  reset (objectToTransferFrom.release());
70  }
71 
72  return *this;
73  }
74 
75  ScopedPointer& operator= (ObjectType* newObjectToTakePossessionOf)
76  {
77  reset (newObjectToTakePossessionOf);
78  return *this;
79  }
80 
81  ScopedPointer (ScopedPointer&& other) noexcept : object (other.object)
82  {
83  other.object = nullptr;
84  }
85 
86  ScopedPointer& operator= (ScopedPointer&& other) noexcept
87  {
88  reset (other.release());
89  return *this;
90  }
91 
92  //==============================================================================
93  inline operator ObjectType*() const noexcept { return object; }
94  inline ObjectType* get() const noexcept { return object; }
95  inline ObjectType& operator*() const noexcept { return *object; }
96  inline ObjectType* operator->() const noexcept { return object; }
97 
98  void reset()
99  {
100  auto* oldObject = object;
101  object = {};
102  ContainerDeletePolicy<ObjectType>::destroy (oldObject);
103  }
104 
105  void reset (ObjectType* newObject)
106  {
107  if (object != newObject)
108  {
109  auto* oldObject = object;
110  object = newObject;
111  ContainerDeletePolicy<ObjectType>::destroy (oldObject);
112  }
113  else
114  {
115  // You're trying to reset this ScopedPointer to itself! This will work here as ScopedPointer does an equality check
116  // but be aware that std::unique_ptr won't do this and you could end up with some nasty, subtle bugs!
117  jassert (newObject == nullptr);
118  }
119  }
120 
121  void reset (ScopedPointer& newObject)
122  {
123  reset (newObject.release());
124  }
125 
126  ObjectType* release() noexcept { auto* o = object; object = {}; return o; }
127 
128  //==============================================================================
129  void swapWith (ScopedPointer<ObjectType>& other) noexcept
130  {
131  // Two ScopedPointers should never be able to refer to the same object - if
132  // this happens, you must have done something dodgy!
133  jassert (object != other.object || this == other.getAddress() || object == nullptr);
134 
135  std::swap (object, other.object);
136  }
137 
138  inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
139 
140 private:
141  //==============================================================================
142  ObjectType* object = nullptr;
143 
144  const ScopedPointer* getAddress() const noexcept { return this; } // Used internally to avoid the & operator
145 
146  #if ! JUCE_MSVC // (MSVC can't deal with multiple copy constructors)
147  ScopedPointer (const ScopedPointer&) = delete;
148  ScopedPointer& operator= (const ScopedPointer&) = delete;
149  #endif
150 };
151 
152 //==============================================================================
153 template <typename ObjectType1, typename ObjectType2>
154 bool operator== (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
155 {
156  return pointer1 == pointer2.get();
157 }
158 
159 template <typename ObjectType1, typename ObjectType2>
160 bool operator!= (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
161 {
162  return pointer1 != pointer2.get();
163 }
164 
165 template <typename ObjectType1, typename ObjectType2>
166 bool operator== (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
167 {
168  return pointer1.get() == pointer2;
169 }
170 
171 template <typename ObjectType1, typename ObjectType2>
172 bool operator!= (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
173 {
174  return pointer1.get() != pointer2;
175 }
176 
177 template <typename ObjectType1, typename ObjectType2>
178 bool operator== (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
179 {
180  return pointer1.get() == pointer2.get();
181 }
182 
183 template <typename ObjectType1, typename ObjectType2>
184 bool operator!= (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
185 {
186  return pointer1.get() != pointer2.get();
187 }
188 
189 template <class ObjectType>
190 bool operator== (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
191 {
192  return pointer.get() == nullptr;
193 }
194 
195 template <class ObjectType>
196 bool operator!= (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
197 {
198  return pointer.get() != nullptr;
199 }
200 
201 template <class ObjectType>
202 bool operator== (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
203 {
204  return pointer.get() == nullptr;
205 }
206 
207 template <class ObjectType>
208 bool operator!= (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
209 {
210  return pointer.get() != nullptr;
211 }
212 
213 //==============================================================================
214 // NB: This is just here to prevent any silly attempts to call deleteAndZero() on a ScopedPointer.
215 template <typename Type>
216 void deleteAndZero (ScopedPointer<Type>&) { static_assert (sizeof (Type) == 12345,
217  "Attempt to call deleteAndZero() on a ScopedPointer"); }
218 
219 } // namespace juce
220 
221 #endif
222 
223 /** @}*/