JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_OptionalScopedPointer.h
Go to the documentation of this file.
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
23namespace juce
24{
25
26//==============================================================================
36template <class ObjectType>
38{
39public:
40 //==============================================================================
43
52 : object (objectToHold),
53 shouldDelete (takeOwnership)
54 {
55 }
56
66 : object (std::move (other.object)),
67 shouldDelete (std::move (other.shouldDelete))
68 {
69 }
70
73 : OptionalScopedPointer (ptr.release(), true)
74 {
75 }
76
78 explicit OptionalScopedPointer (ObjectType& ref) noexcept
80 {
81 }
82
92 {
94 other.reset();
95 return *this;
96 }
97
103 {
104 reset();
105 }
106
107 //==============================================================================
109 operator ObjectType*() const noexcept { return object.get(); }
110
112 ObjectType* get() const noexcept { return object.get(); }
113
115 ObjectType& operator*() const noexcept { return *object; }
116
118 ObjectType* operator->() const noexcept { return object.get(); }
119
120 //==============================================================================
124 ObjectType* release() noexcept { return object.release(); }
125
129 void reset() noexcept
130 {
131 if (! shouldDelete)
132 object.release();
133 else
134 object.reset();
135 }
136
138 void clear() { reset(); }
139
148 {
149 if (object.get() != newObject)
150 {
151 reset();
152 object.reset (newObject);
153 }
154
155 shouldDelete = takeOwnership;
156 }
157
160 {
161 set (newObject, true);
162 }
163
166 {
167 set (newObject, false);
168 }
169
173 bool willDeleteObject() const noexcept { return shouldDelete; }
174
175 //==============================================================================
180 {
181 std::swap (other.object, object);
182 std::swap (other.shouldDelete, shouldDelete);
183 }
184
185private:
186 //==============================================================================
188 bool shouldDelete = false;
189};
190
191} // namespace juce
T addressof(T... args)
Holds a pointer to an object which can optionally be deleted when this pointer goes out of scope.
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 ...
OptionalScopedPointer & operator=(OptionalScopedPointer &&other) noexcept
Takes ownership of the object that another OptionalScopedPointer holds.
void clear()
Does the same thing as reset().
void reset() noexcept
Resets this pointer to null, possibly deleting the object that it holds, if it has ownership of it.
OptionalScopedPointer(std::unique_ptr< ObjectType > &&ptr) noexcept
Takes ownership of the object owned by ptr.
OptionalScopedPointer()=default
Creates an empty OptionalScopedPointer.
ObjectType * get() const noexcept
Returns the object that this pointer is managing.
OptionalScopedPointer(ObjectType &ref) noexcept
Points to the same object as ref, but does not take ownership.
ObjectType & operator*() const noexcept
Returns the object that this pointer is managing.
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.
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.
OptionalScopedPointer(OptionalScopedPointer &&other) noexcept
Takes ownership of the object that another OptionalScopedPointer holds.
~OptionalScopedPointer() noexcept
The destructor may or may not delete the object that is being held, depending on the takeOwnership fl...
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T swap(T... args)