openshot-audio  0.1.7
juce_OptionalScopedPointer.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_OPTIONALSCOPEDPOINTER_H_INCLUDED
30 #define JUCE_OPTIONALSCOPEDPOINTER_H_INCLUDED
31 
32 
33 //==============================================================================
43 template <class ObjectType>
45 {
46 public:
47  //==============================================================================
49  OptionalScopedPointer() : shouldDelete (false) {}
50 
58  OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
59  : object (objectToHold), shouldDelete (takeOwnership)
60  {
61  }
62 
72  : object (objectToTransferFrom.release()),
73  shouldDelete (objectToTransferFrom.shouldDelete)
74  {
75  }
76 
86  {
87  if (object != objectToTransferFrom.object)
88  {
89  clear();
90  object = objectToTransferFrom.object;
91  }
92 
93  shouldDelete = objectToTransferFrom.shouldDelete;
94  return *this;
95  }
96 
102  {
103  clear();
104  }
105 
106  //==============================================================================
108  inline operator ObjectType*() const noexcept { return object; }
109 
111  inline ObjectType* get() const noexcept { return object; }
112 
114  inline ObjectType& operator*() const noexcept { return *object; }
115 
117  inline ObjectType* operator->() const noexcept { return object; }
118 
119  //==============================================================================
123  ObjectType* release() noexcept { return object.release(); }
124 
128  void clear()
129  {
130  if (! shouldDelete)
131  object.release();
132  }
133 
141  void set (ObjectType* newObject, bool takeOwnership)
142  {
143  if (object != newObject)
144  {
145  clear();
146  object = newObject;
147  }
148 
149  shouldDelete = takeOwnership;
150  }
151 
153  void setOwned (ObjectType* newObject)
154  {
155  set (newObject, true);
156  }
157 
159  void setNonOwned (ObjectType* newObject)
160  {
161  set (newObject, false);
162  }
163 
167  bool willDeleteObject() const noexcept { return shouldDelete; }
168 
169  //==============================================================================
174  {
175  object.swapWith (other.object);
176  std::swap (shouldDelete, other.shouldDelete);
177  }
178 
179 private:
180  //==============================================================================
182  bool shouldDelete;
183 };
184 
185 
186 #endif // JUCE_OPTIONALSCOPEDPOINTER_H_INCLUDED
ObjectType * operator->() const noexcept
Definition: juce_OptionalScopedPointer.h:117
#define noexcept
Definition: juce_CompilerSupport.h:141
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Definition: juce_OptionalScopedPointer.h:58
Definition: juce_OptionalScopedPointer.h:44
void setOwned(ObjectType *newObject)
Definition: juce_OptionalScopedPointer.h:153
OptionalScopedPointer & operator=(OptionalScopedPointer &objectToTransferFrom)
Definition: juce_OptionalScopedPointer.h:85
void setNonOwned(ObjectType *newObject)
Definition: juce_OptionalScopedPointer.h:159
OptionalScopedPointer(OptionalScopedPointer &objectToTransferFrom)
Definition: juce_OptionalScopedPointer.h:71
Definition: juce_ScopedPointer.h:70
ObjectType * release() noexcept
Definition: juce_OptionalScopedPointer.h:123
ObjectType & operator*() const noexcept
Definition: juce_OptionalScopedPointer.h:114
bool willDeleteObject() const noexcept
Definition: juce_OptionalScopedPointer.h:167
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
Definition: juce_OptionalScopedPointer.h:173
void clear()
Definition: juce_OptionalScopedPointer.h:128
~OptionalScopedPointer()
Definition: juce_OptionalScopedPointer.h:101
OptionalScopedPointer()
Definition: juce_OptionalScopedPointer.h:49