OpenShot Audio Library | OpenShotAudio  0.6.0
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 
23 namespace juce
24 {
25 
26 //==============================================================================
36 template <class ObjectType>
38 {
39 public:
40  //==============================================================================
42  OptionalScopedPointer() = default;
43 
51  OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
52  : object (objectToHold),
53  shouldDelete (takeOwnership)
54  {
55  }
56 
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 
92  {
93  swapWith (other);
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 
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  //==============================================================================
180  {
181  std::swap (other.object, object);
182  std::swap (other.shouldDelete, shouldDelete);
183  }
184 
185 private:
186  //==============================================================================
187  std::unique_ptr<ObjectType> object;
188  bool shouldDelete = false;
189 };
190 
191 } // namespace juce
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
void setNonOwned(ObjectType *newObject)
void set(ObjectType *newObject, bool takeOwnership)
OptionalScopedPointer(std::unique_ptr< ObjectType > &&ptr) noexcept
OptionalScopedPointer & operator=(OptionalScopedPointer &&other) noexcept
ObjectType & operator*() const noexcept
OptionalScopedPointer(ObjectType &ref) noexcept
ObjectType * get() const noexcept
void setOwned(ObjectType *newObject)
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
ObjectType * operator->() const noexcept
OptionalScopedPointer(OptionalScopedPointer &&other) noexcept