OpenShot Audio Library | OpenShotAudio  0.3.3
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 
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), shouldDelete (takeOwnership)
53  {
54  }
55 
65  : object (objectToTransferFrom.release()),
66  shouldDelete (objectToTransferFrom.shouldDelete)
67  {
68  }
69 
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 
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  //==============================================================================
172  {
173  object.swapWith (other.object);
174  std::swap (shouldDelete, other.shouldDelete);
175  }
176 
177 private:
178  //==============================================================================
179  std::unique_ptr<ObjectType> object;
180  bool shouldDelete = false;
181 };
182 
183 } // namespace juce
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
void setNonOwned(ObjectType *newObject)
void set(ObjectType *newObject, bool takeOwnership)
ObjectType & operator*() const noexcept
ObjectType * get() const noexcept
void setOwned(ObjectType *newObject)
OptionalScopedPointer(OptionalScopedPointer &objectToTransferFrom)
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
ObjectType * operator->() const noexcept
OptionalScopedPointer & operator=(OptionalScopedPointer &objectToTransferFrom)