2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_OPTIONALSCOPEDPOINTER_JUCEHEADER__
27 #define __JUCE_OPTIONALSCOPEDPOINTER_JUCEHEADER__
29 #include "juce_ScopedPointer.h"
32 //==============================================================================
34 Holds a pointer to an object which can optionally be deleted when this pointer
37 This acts in many ways like a ScopedPointer, but allows you to specify whether or
38 not the object is deleted.
42 template <class ObjectType
>
43 class OptionalScopedPointer
46 //==============================================================================
47 /** Creates an empty OptionalScopedPointer. */
48 OptionalScopedPointer() : shouldDelete (false) {}
50 /** Creates an OptionalScopedPointer to point to a given object, and specifying whether
51 the OptionalScopedPointer will delete it.
53 If takeOwnership is true, then the OptionalScopedPointer will act like a ScopedPointer,
54 deleting the object when it is itself deleted. If this parameter is false, then the
55 OptionalScopedPointer just holds a normal pointer to the object, and won't delete it.
57 OptionalScopedPointer (ObjectType
* objectToHold
, bool takeOwnership
)
58 : object (objectToHold
), shouldDelete (takeOwnership
)
62 /** Takes ownership of the object that another OptionalScopedPointer holds.
64 Like a normal ScopedPointer, the objectToTransferFrom object will become null,
65 as ownership of the managed object is transferred to this object.
67 The flag to indicate whether or not to delete the managed object is also
68 copied from the source object.
70 OptionalScopedPointer (OptionalScopedPointer
& objectToTransferFrom
)
71 : object (objectToTransferFrom
.release()),
72 shouldDelete (objectToTransferFrom
.shouldDelete
)
76 /** Takes ownership of the object that another OptionalScopedPointer holds.
78 Like a normal ScopedPointer, the objectToTransferFrom object will become null,
79 as ownership of the managed object is transferred to this object.
81 The ownership flag that says whether or not to delete the managed object is also
82 copied from the source object.
84 OptionalScopedPointer
& operator= (OptionalScopedPointer
& objectToTransferFrom
)
86 if (object
!= objectToTransferFrom
.object
)
89 object
= objectToTransferFrom
.object
;
92 shouldDelete
= objectToTransferFrom
.shouldDelete
;
96 /** The destructor may or may not delete the object that is being held, depending on the
97 takeOwnership flag that was specified when the object was first passed into an
98 OptionalScopedPointer constructor.
100 ~OptionalScopedPointer()
105 //==============================================================================
106 /** Returns the object that this pointer is managing. */
107 inline operator ObjectType
*() const noexcept
{ return object
; }
109 /** Returns the object that this pointer is managing. */
110 inline ObjectType
& operator*() const noexcept
{ return *object
; }
112 /** Lets you access methods and properties of the object that this pointer is holding. */
113 inline ObjectType
* operator->() const noexcept
{ return object
; }
115 //==============================================================================
116 /** Removes the current object from this OptionalScopedPointer without deleting it.
117 This will return the current object, and set this OptionalScopedPointer to a null pointer.
119 ObjectType
* release() noexcept
{ return object
.release(); }
121 /** Resets this pointer to null, possibly deleting the object that it holds, if it has
130 //==============================================================================
131 /** Swaps this object with another OptionalScopedPointer.
132 The two objects simply exchange their states.
134 void swapWith (OptionalScopedPointer
<ObjectType
>& other
) noexcept
136 object
.swapWith (other
.object
);
137 std::swap (shouldDelete
, other
.shouldDelete
);
141 //==============================================================================
142 ScopedPointer
<ObjectType
> object
;
147 #endif // __JUCE_OPTIONALSCOPEDPOINTER_JUCEHEADER__