OpenShot Audio Library | OpenShotAudio: juce_ScopedPointer.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_ScopedPointer.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23#ifndef DOXYGEN
24
25namespace juce
26{
27
28//==============================================================================
32template <class ObjectType>
33 class [[deprecated]] ScopedPointer
34{
35public:
36 //==============================================================================
37 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
38 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
39
40 inline ScopedPointer() {}
41
42 inline ScopedPointer (decltype (nullptr)) noexcept {}
43
44 inline ScopedPointer (ObjectType* objectToTakePossessionOf) noexcept
45 : object (objectToTakePossessionOf)
46 {
47 }
48
49 ScopedPointer (ScopedPointer& objectToTransferFrom) noexcept
50 : object (objectToTransferFrom.release())
51 {
52 }
53
54 inline ~ScopedPointer() { reset(); }
55
56 ScopedPointer& operator= (ScopedPointer& objectToTransferFrom)
57 {
58 if (this != objectToTransferFrom.getAddress())
59 {
60 // Two ScopedPointers should never be able to refer to the same object - if
61 // this happens, you must have done something dodgy!
62 jassert (object == nullptr || object != objectToTransferFrom.object);
63 reset (objectToTransferFrom.release());
64 }
65
66 return *this;
67 }
68
69 ScopedPointer& operator= (ObjectType* newObjectToTakePossessionOf)
70 {
71 reset (newObjectToTakePossessionOf);
72 return *this;
73 }
74
75 ScopedPointer (ScopedPointer&& other) noexcept : object (other.object)
76 {
77 other.object = nullptr;
78 }
79
80 ScopedPointer& operator= (ScopedPointer&& other) noexcept
81 {
82 reset (other.release());
83 return *this;
84 }
85
86 //==============================================================================
87 inline operator ObjectType*() const noexcept { return object; }
88 inline ObjectType* get() const noexcept { return object; }
89 inline ObjectType& operator*() const noexcept { return *object; }
90 inline ObjectType* operator->() const noexcept { return object; }
91
92 void reset()
93 {
94 auto* oldObject = object;
95 object = {};
96 ContainerDeletePolicy<ObjectType>::destroy (oldObject);
97 }
98
99 void reset (ObjectType* newObject)
100 {
101 if (object != newObject)
102 {
103 auto* oldObject = object;
104 object = newObject;
105 ContainerDeletePolicy<ObjectType>::destroy (oldObject);
106 }
107 else
108 {
109 // You're trying to reset this ScopedPointer to itself! This will work here as ScopedPointer does an equality check
110 // but be aware that std::unique_ptr won't do this and you could end up with some nasty, subtle bugs!
111 jassert (newObject == nullptr);
112 }
113 }
114
115 void reset (ScopedPointer& newObject)
116 {
117 reset (newObject.release());
118 }
119
120 ObjectType* release() noexcept { auto* o = object; object = {}; return o; }
121
122 //==============================================================================
123 void swapWith (ScopedPointer<ObjectType>& other) noexcept
124 {
125 // Two ScopedPointers should never be able to refer to the same object - if
126 // this happens, you must have done something dodgy!
127 jassert (object != other.object || this == other.getAddress() || object == nullptr);
128
129 std::swap (object, other.object);
130 }
131
132 inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
133
134private:
135 //==============================================================================
136 ObjectType* object = nullptr;
137
138 const ScopedPointer* getAddress() const noexcept { return this; } // Used internally to avoid the & operator
139
140 #if ! JUCE_MSVC // (MSVC can't deal with multiple copy constructors)
141 ScopedPointer (const ScopedPointer&) = delete;
142 ScopedPointer& operator= (const ScopedPointer&) = delete;
143 #endif
144
145 JUCE_END_IGNORE_WARNINGS_MSVC
146 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
147};
148
149//==============================================================================
150JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
151JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
152
153template <typename ObjectType1, typename ObjectType2>
154bool operator== (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
155{
156 return pointer1 == pointer2.get();
157}
158
159template <typename ObjectType1, typename ObjectType2>
160bool operator!= (ObjectType1* pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
161{
162 return pointer1 != pointer2.get();
163}
164
165template <typename ObjectType1, typename ObjectType2>
166bool operator== (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
167{
168 return pointer1.get() == pointer2;
169}
170
171template <typename ObjectType1, typename ObjectType2>
172bool operator!= (const ScopedPointer<ObjectType1>& pointer1, ObjectType2* pointer2) noexcept
173{
174 return pointer1.get() != pointer2;
175}
176
177template <typename ObjectType1, typename ObjectType2>
178bool operator== (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
179{
180 return pointer1.get() == pointer2.get();
181}
182
183template <typename ObjectType1, typename ObjectType2>
184bool operator!= (const ScopedPointer<ObjectType1>& pointer1, const ScopedPointer<ObjectType2>& pointer2) noexcept
185{
186 return pointer1.get() != pointer2.get();
187}
188
189template <class ObjectType>
190bool operator== (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
191{
192 return pointer.get() == nullptr;
193}
194
195template <class ObjectType>
196bool operator!= (decltype (nullptr), const ScopedPointer<ObjectType>& pointer) noexcept
197{
198 return pointer.get() != nullptr;
199}
200
201template <class ObjectType>
202bool operator== (const ScopedPointer<ObjectType>& pointer, decltype (nullptr)) noexcept
203{
204 return pointer.get() == nullptr;
205}
206
207template <class ObjectType>
208bool 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.
215template <typename Type>
216void deleteAndZero (ScopedPointer<Type>&) { static_assert (sizeof (Type) == 12345,
217 "Attempt to call deleteAndZero() on a ScopedPointer"); }
218
219JUCE_END_IGNORE_WARNINGS_GCC_LIKE
220JUCE_END_IGNORE_WARNINGS_MSVC
221
222} // namespace juce
223
224#endif
juce::ScopedPointer
Definition juce_ScopedPointer.h:34
juce::ContainerDeletePolicy
Definition juce_ContainerDeletePolicy.h:41