OpenShot Library | libopenshot-audio 0.2.0
juce_OptionalScopedPointer.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
27namespace juce
28{
29
30//==============================================================================
31/**
32 Holds a pointer to an object which can optionally be deleted when this pointer
33 goes out of scope.
34
35 This acts in many ways like a ScopedPointer, but allows you to specify whether or
36 not the object is deleted.
37
38 @see ScopedPointer
39
40 @tags{Core}
41*/
42template <class ObjectType>
44{
45public:
46 //==============================================================================
47 /** Creates an empty OptionalScopedPointer. */
49
50 /** Creates an OptionalScopedPointer to point to a given object, and specifying whether
51 the OptionalScopedPointer will delete it.
52
53 If takeOwnership is true, then the OptionalScopedPointer will act like a ScopedPointer,
54 deleting the object when it is itself deleted. If this parameter is false, then the
55 OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
56 */
58 : object (objectToHold), shouldDelete (takeOwnership)
59 {
60 }
61
62 /** Takes ownership of the object that another OptionalScopedPointer holds.
63
64 Like a normal ScopedPointer, the objectToTransferFrom object will become null,
65 as ownership of the managed object is transferred to this object.
66
67 The flag to indicate whether or not to delete the managed object is also
68 copied from the source object.
69 */
75
76 /** Takes ownership of the object that another OptionalScopedPointer holds.
77
78 Like a normal ScopedPointer, the objectToTransferFrom object will become null,
79 as ownership of the managed object is transferred to this object.
80
81 The ownership flag that says whether or not to delete the managed object is also
82 copied from the source object.
83 */
85 {
86 if (object != objectToTransferFrom.object)
87 {
88 reset();
89 object.reset (objectToTransferFrom.object.release());
90 }
91
92 shouldDelete = objectToTransferFrom.shouldDelete;
93 return *this;
94 }
95
96 /** The destructor may or may not delete the object that is being held, depending on the
97 takeOwnership flag that was specified when the object was first passed into an
98 OptionalScopedPointer constructor.
99 */
101 {
102 reset();
103 }
104
105 //==============================================================================
106 /** Returns the object that this pointer is managing. */
107 inline operator ObjectType*() const noexcept { return object.get(); }
108
109 /** Returns the object that this pointer is managing. */
110 inline ObjectType* get() const noexcept { return object.get(); }
111
112 /** Returns the object that this pointer is managing. */
113 inline ObjectType& operator*() const noexcept { return *object; }
114
115 /** Lets you access methods and properties of the object that this pointer is holding. */
116 inline ObjectType* operator->() const noexcept { return object.get(); }
117
118 //==============================================================================
119 /** Removes the current object from this OptionalScopedPointer without deleting it.
120 This will return the current object, and set this OptionalScopedPointer to a null pointer.
121 */
122 ObjectType* release() noexcept { return object.release(); }
123
124 /** Resets this pointer to null, possibly deleting the object that it holds, if it has
125 ownership of it.
126 */
127 void reset()
128 {
129 if (! shouldDelete)
130 object.release();
131 }
132
133 /** Does the same thing as reset(). */
134 void clear() { reset(); }
135
136 /** Makes this OptionalScopedPointer point at a new object, specifying whether the
137 OptionalScopedPointer will take ownership of the object.
138
139 If takeOwnership is true, then the OptionalScopedPointer will act like a ScopedPointer,
140 deleting the object when it is itself deleted. If this parameter is false, then the
141 OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
142 */
144 {
145 if (object.get() != newObject)
146 {
147 reset();
148 object.reset (newObject);
149 }
150
151 shouldDelete = takeOwnership;
152 }
153
154 /** Makes this OptionalScopedPointer point at a new object, and take ownership of that object. */
156 {
157 set (newObject, true);
158 }
159
160 /** Makes this OptionalScopedPointer point at a new object, but will not take ownership of that object. */
162 {
163 set (newObject, false);
164 }
165
166 /** Returns true if the target object will be deleted when this pointer
167 object is deleted.
168 */
169 bool willDeleteObject() const noexcept { return shouldDelete; }
170
171 //==============================================================================
172 /** Swaps this object with another OptionalScopedPointer.
173 The two objects simply exchange their states.
174 */
176 {
177 object.swapWith (other.object);
178 std::swap (shouldDelete, other.shouldDelete);
179 }
180
181private:
182 //==============================================================================
184 bool shouldDelete = false;
185
186 // This is here to avoid people accidentally taking a second owned copy of
187 // a scoped pointer, which is almost certainly not what you intended to do!
188 // If you hit a problem with this, you probably meant to say
189 // myPointer.setOwned (myScopedPointer.release())
190 void setOwned (const ScopedPointer<ObjectType>&) = delete;
191};
192
193} // namespace juce
194
195/** @}*/
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
void swapWith(OtherArrayType &otherArray) noexcept
This swaps the contents of this array with those of another array.
Definition juce_Array.h:578
Holds a pointer to an object which can optionally be deleted when this pointer goes out of scope.
void reset()
Resets this pointer to null, possibly deleting the object that it holds, if it has ownership of it.
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Creates an OptionalScopedPointer to point to a given object, and specifying whether the OptionalScope...
void setNonOwned(ObjectType *newObject)
Makes this OptionalScopedPointer point at a new object, but will not take ownership of that object.
void set(ObjectType *newObject, bool takeOwnership)
Makes this OptionalScopedPointer point at a new object, specifying whether the OptionalScopedPointer ...
void clear()
Does the same thing as reset().
OptionalScopedPointer()=default
Creates an empty OptionalScopedPointer.
ObjectType * get() const noexcept
Returns the object that this pointer is managing.
~OptionalScopedPointer()
The destructor may or may not delete the object that is being held, depending on the takeOwnership fl...
ObjectType & operator*() const noexcept
Returns the object that this pointer is managing.
OptionalScopedPointer & operator=(OptionalScopedPointer &objectToTransferFrom)
Takes ownership of the object that another OptionalScopedPointer holds.
ObjectType * release() noexcept
Removes the current object from this OptionalScopedPointer without deleting it.
void setOwned(ObjectType *newObject)
Makes this OptionalScopedPointer point at a new object, and take ownership of that object.
OptionalScopedPointer(OptionalScopedPointer &objectToTransferFrom)
Takes ownership of the object that another OptionalScopedPointer holds.
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
Swaps this object with another OptionalScopedPointer.
bool willDeleteObject() const noexcept
Returns true if the target object will be deleted when this pointer object is deleted.
ObjectType * operator->() const noexcept
Lets you access methods and properties of the object that this pointer is holding.