OpenShot Audio Library | OpenShotAudio: juce_OptionalScopedPointer.h Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_OptionalScopedPointer.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
23namespace juce
24{
25
26//==============================================================================
36template <class ObjectType>
37 class OptionalScopedPointer
38{
39public:
40 //==============================================================================
42 OptionalScopedPointer() = default;
43
51 OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
52 : object (objectToHold),
53 shouldDelete (takeOwnership)
54 {
55 }
56
65 OptionalScopedPointer (OptionalScopedPointer&& other) noexcept
66 : object (std::move (other.object)),
67 shouldDelete (std::move (other.shouldDelete))
68 {
69 }
70
72 explicit OptionalScopedPointer (std::unique_ptr<ObjectType>&& ptr) noexcept
73 : OptionalScopedPointer (ptr.release(), true)
74 {
75 }
76
78 explicit OptionalScopedPointer (ObjectType& ref) noexcept
79 : OptionalScopedPointer (std::addressof (ref), false)
80 {
81 }
82
91 OptionalScopedPointer& operator= (OptionalScopedPointer&& other) noexcept
92 {
93 swapWith (other);
94 other.reset();
95 return *this;
96 }
97
102 ~OptionalScopedPointer() noexcept
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
147 void set (ObjectType* newObject, bool takeOwnership)
148 {
149 if (object.get() != newObject)
150 {
151 reset();
152 object.reset (newObject);
153 }
154
155 shouldDelete = takeOwnership;
156 }
157
159 void setOwned (ObjectType* newObject)
160 {
161 set (newObject, true);
162 }
163
165 void setNonOwned (ObjectType* newObject)
166 {
167 set (newObject, false);
168 }
169
173 bool willDeleteObject() const noexcept { return shouldDelete; }
174
175 //==============================================================================
179 void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept
180 {
181 std::swap (other.object, object);
182 std::swap (other.shouldDelete, shouldDelete);
183 }
184
185private:
186 //==============================================================================
187 std::unique_ptr<ObjectType> object;
188 bool shouldDelete = false;
189};
190
191} // namespace juce
juce::OptionalScopedPointer
Definition juce_OptionalScopedPointer.h:38
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Definition juce_OptionalScopedPointer.h:51
juce::OptionalScopedPointer::setNonOwned
void setNonOwned(ObjectType *newObject)
Definition juce_OptionalScopedPointer.h:165
juce::OptionalScopedPointer::set
void set(ObjectType *newObject, bool takeOwnership)
Definition juce_OptionalScopedPointer.h:147
juce::OptionalScopedPointer::operator=
OptionalScopedPointer & operator=(OptionalScopedPointer &&other) noexcept
Definition juce_OptionalScopedPointer.h:91
juce::OptionalScopedPointer::clear
void clear()
Definition juce_OptionalScopedPointer.h:138
juce::OptionalScopedPointer::reset
void reset() noexcept
Definition juce_OptionalScopedPointer.h:129
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(std::unique_ptr< ObjectType > &&ptr) noexcept
Definition juce_OptionalScopedPointer.h:72
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer()=default
juce::OptionalScopedPointer::get
ObjectType * get() const noexcept
Definition juce_OptionalScopedPointer.h:112
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(ObjectType &ref) noexcept
Definition juce_OptionalScopedPointer.h:78
juce::OptionalScopedPointer::operator*
ObjectType & operator*() const noexcept
Definition juce_OptionalScopedPointer.h:115
juce::OptionalScopedPointer::release
ObjectType * release() noexcept
Definition juce_OptionalScopedPointer.h:124
juce::OptionalScopedPointer::setOwned
void setOwned(ObjectType *newObject)
Definition juce_OptionalScopedPointer.h:159
juce::OptionalScopedPointer::swapWith
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
Definition juce_OptionalScopedPointer.h:179
juce::OptionalScopedPointer::willDeleteObject
bool willDeleteObject() const noexcept
Definition juce_OptionalScopedPointer.h:173
juce::OptionalScopedPointer::operator->
ObjectType * operator->() const noexcept
Definition juce_OptionalScopedPointer.h:118
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(OptionalScopedPointer &&other) noexcept
Definition juce_OptionalScopedPointer.h:65
juce::OptionalScopedPointer::~OptionalScopedPointer
~OptionalScopedPointer() noexcept
Definition juce_OptionalScopedPointer.h:102