OpenShot Audio Library | OpenShotAudio: juce_OptionalScopedPointer.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_OptionalScopedPointer.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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), shouldDelete (takeOwnership)
53 {
54 }
55
64 OptionalScopedPointer (OptionalScopedPointer& objectToTransferFrom)
65 : object (objectToTransferFrom.release()),
66 shouldDelete (objectToTransferFrom.shouldDelete)
67 {
68 }
69
78 OptionalScopedPointer& operator= (OptionalScopedPointer& objectToTransferFrom)
79 {
80 if (object != objectToTransferFrom.object)
81 {
82 reset();
83 object.reset (objectToTransferFrom.object.release());
84 }
85
86 shouldDelete = objectToTransferFrom.shouldDelete;
87 return *this;
88 }
89
94 ~OptionalScopedPointer()
95 {
96 reset();
97 }
98
99 //==============================================================================
101 inline operator ObjectType*() const noexcept { return object.get(); }
102
104 inline ObjectType* get() const noexcept { return object.get(); }
105
107 inline ObjectType& operator*() const noexcept { return *object; }
108
110 inline ObjectType* operator->() const noexcept { return object.get(); }
111
112 //==============================================================================
116 ObjectType* release() noexcept { return object.release(); }
117
121 void reset()
122 {
123 if (! shouldDelete)
124 object.release();
125 else
126 object.reset();
127 }
128
130 void clear() { reset(); }
131
139 void set (ObjectType* newObject, bool takeOwnership)
140 {
141 if (object.get() != newObject)
142 {
143 reset();
144 object.reset (newObject);
145 }
146
147 shouldDelete = takeOwnership;
148 }
149
151 void setOwned (ObjectType* newObject)
152 {
153 set (newObject, true);
154 }
155
157 void setNonOwned (ObjectType* newObject)
158 {
159 set (newObject, false);
160 }
161
165 bool willDeleteObject() const noexcept { return shouldDelete; }
166
167 //==============================================================================
171 void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept
172 {
173 object.swapWith (other.object);
174 std::swap (shouldDelete, other.shouldDelete);
175 }
176
177private:
178 //==============================================================================
179 std::unique_ptr<ObjectType> object;
180 bool shouldDelete = false;
181};
182
183} // namespace juce
juce::OptionalScopedPointer
Definition: juce_OptionalScopedPointer.h:38
juce::OptionalScopedPointer::reset
void reset()
Definition: juce_OptionalScopedPointer.h:121
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Definition: juce_OptionalScopedPointer.h:51
juce::OptionalScopedPointer::setNonOwned
void setNonOwned(ObjectType *newObject)
Definition: juce_OptionalScopedPointer.h:157
juce::OptionalScopedPointer::set
void set(ObjectType *newObject, bool takeOwnership)
Definition: juce_OptionalScopedPointer.h:139
juce::OptionalScopedPointer::clear
void clear()
Definition: juce_OptionalScopedPointer.h:130
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer()=default
juce::OptionalScopedPointer::get
ObjectType * get() const noexcept
Definition: juce_OptionalScopedPointer.h:104
juce::OptionalScopedPointer::~OptionalScopedPointer
~OptionalScopedPointer()
Definition: juce_OptionalScopedPointer.h:94
juce::OptionalScopedPointer::operator*
ObjectType & operator*() const noexcept
Definition: juce_OptionalScopedPointer.h:107
juce::OptionalScopedPointer::operator=
OptionalScopedPointer & operator=(OptionalScopedPointer &objectToTransferFrom)
Definition: juce_OptionalScopedPointer.h:78
juce::OptionalScopedPointer::release
ObjectType * release() noexcept
Definition: juce_OptionalScopedPointer.h:116
juce::OptionalScopedPointer::setOwned
void setOwned(ObjectType *newObject)
Definition: juce_OptionalScopedPointer.h:151
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(OptionalScopedPointer &objectToTransferFrom)
Definition: juce_OptionalScopedPointer.h:64
juce::OptionalScopedPointer::swapWith
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
Definition: juce_OptionalScopedPointer.h:171
juce::OptionalScopedPointer::willDeleteObject
bool willDeleteObject() const noexcept
Definition: juce_OptionalScopedPointer.h:165
juce::OptionalScopedPointer::operator->
ObjectType * operator->() const noexcept
Definition: juce_OptionalScopedPointer.h:110