OpenShot Library | OpenShotAudio  0.2.1
juce_OptionalScopedPointer.h
1 
2 /** @weakgroup juce_core-memory
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Holds a pointer to an object which can optionally be deleted when this pointer
33  goes out of scope.
34 
35  This acts in many ways like a std::unique_ptr, but allows you to specify whether or
36  not the object is deleted.
37 
38  @tags{Core}
39 */
40 template <class ObjectType>
42 {
43 public:
44  //==============================================================================
45  /** Creates an empty OptionalScopedPointer. */
46  OptionalScopedPointer() = default;
47 
48  /** Creates an OptionalScopedPointer to point to a given object, and specifying whether
49  the OptionalScopedPointer will delete it.
50 
51  If takeOwnership is true, then the OptionalScopedPointer will act like a std::unique_ptr,
52  deleting the object when it is itself deleted. If this parameter is false, then the
53  OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
54  */
55  OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
56  : object (objectToHold), shouldDelete (takeOwnership)
57  {
58  }
59 
60  /** Takes ownership of the object that another OptionalScopedPointer holds.
61 
62  Like a normal std::unique_ptr, the objectToTransferFrom object will become null,
63  as ownership of the managed object is transferred to this object.
64 
65  The flag to indicate whether or not to delete the managed object is also
66  copied from the source object.
67  */
69  : object (objectToTransferFrom.release()),
70  shouldDelete (objectToTransferFrom.shouldDelete)
71  {
72  }
73 
74  /** Takes ownership of the object that another OptionalScopedPointer holds.
75 
76  Like a normal std::unique_ptr, the objectToTransferFrom object will become null,
77  as ownership of the managed object is transferred to this object.
78 
79  The ownership flag that says whether or not to delete the managed object is also
80  copied from the source object.
81  */
83  {
84  if (object != objectToTransferFrom.object)
85  {
86  reset();
87  object.reset (objectToTransferFrom.object.release());
88  }
89 
90  shouldDelete = objectToTransferFrom.shouldDelete;
91  return *this;
92  }
93 
94  /** The destructor may or may not delete the object that is being held, depending on the
95  takeOwnership flag that was specified when the object was first passed into an
96  OptionalScopedPointer constructor.
97  */
99  {
100  reset();
101  }
102 
103  //==============================================================================
104  /** Returns the object that this pointer is managing. */
105  inline operator ObjectType*() const noexcept { return object.get(); }
106 
107  /** Returns the object that this pointer is managing. */
108  inline ObjectType* get() const noexcept { return object.get(); }
109 
110  /** Returns the object that this pointer is managing. */
111  inline ObjectType& operator*() const noexcept { return *object; }
112 
113  /** Lets you access methods and properties of the object that this pointer is holding. */
114  inline ObjectType* operator->() const noexcept { return object.get(); }
115 
116  //==============================================================================
117  /** Removes the current object from this OptionalScopedPointer without deleting it.
118  This will return the current object, and set this OptionalScopedPointer to a null pointer.
119  */
120  ObjectType* release() noexcept { return object.release(); }
121 
122  /** Resets this pointer to null, possibly deleting the object that it holds, if it has
123  ownership of it.
124  */
125  void reset()
126  {
127  if (! shouldDelete)
128  object.release();
129  else
130  object.reset();
131  }
132 
133  /** Does the same thing as reset(). */
134  void clear() { reset(); }
135 
136  /** Makes this OptionalScopedPointer point at a new object, specifying whether the
137  OptionalScopedPointer will take ownership of the object.
138 
139  If takeOwnership is true, then the OptionalScopedPointer will act like a std::unique_ptr,
140  deleting the object when it is itself deleted. If this parameter is false, then the
141  OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
142  */
143  void set (ObjectType* newObject, bool takeOwnership)
144  {
145  if (object.get() != newObject)
146  {
147  reset();
148  object.reset (newObject);
149  }
150 
151  shouldDelete = takeOwnership;
152  }
153 
154  /** Makes this OptionalScopedPointer point at a new object, and take ownership of that object. */
155  void setOwned (ObjectType* newObject)
156  {
157  set (newObject, true);
158  }
159 
160  /** Makes this OptionalScopedPointer point at a new object, but will not take ownership of that object. */
161  void setNonOwned (ObjectType* newObject)
162  {
163  set (newObject, false);
164  }
165 
166  /** Returns true if the target object will be deleted when this pointer
167  object is deleted.
168  */
169  bool willDeleteObject() const noexcept { return shouldDelete; }
170 
171  //==============================================================================
172  /** Swaps this object with another OptionalScopedPointer.
173  The two objects simply exchange their states.
174  */
176  {
177  object.swapWith (other.object);
178  std::swap (shouldDelete, other.shouldDelete);
179  }
180 
181 private:
182  //==============================================================================
183  std::unique_ptr<ObjectType> object;
184  bool shouldDelete = false;
185 };
186 
187 } // namespace juce
188 
189 /** @}*/
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...
OptionalScopedPointer(OptionalScopedPointer &objectToTransferFrom)
Takes ownership of the object that another OptionalScopedPointer holds.
void reset()
Resets this pointer to null, possibly deleting the object that it holds, if it has ownership of it...
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Creates an OptionalScopedPointer to point to a given object, and specifying whether the OptionalScope...
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.
Holds a pointer to an object which can optionally be deleted when this pointer goes out of scope...
ObjectType & operator*() const noexcept
Returns the object that this pointer is managing.
OptionalScopedPointer()=default
Creates an empty OptionalScopedPointer.
OptionalScopedPointer & operator=(OptionalScopedPointer &objectToTransferFrom)
Takes ownership of the object that another OptionalScopedPointer holds.
~OptionalScopedPointer()
The destructor may or may not delete the object that is being held, depending on the takeOwnership fl...
void setNonOwned(ObjectType *newObject)
Makes this OptionalScopedPointer point at a new object, but will not take ownership of that object...
void clear()
Does the same thing as reset().
ObjectType * operator->() const noexcept
Lets you access methods and properties of the object that this pointer is holding.