DISTRHO Plugin Framework
 All Classes Functions Variables Modules Pages
d_scopedpointer.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DISTRHO_SCOPED_POINTER_HPP_INCLUDED
18 #define DISTRHO_SCOPED_POINTER_HPP_INCLUDED
19 
20 #include "../DistrhoUtils.hpp"
21 
22 #include <algorithm>
23 
24 START_NAMESPACE_DISTRHO
25 
26 // -----------------------------------------------------------------------
27 // The following code was based from juce-core ScopedPointer class
28 // Copyright (C) 2013 Raw Material Software Ltd.
29 
30 /**
31  Used by container classes as an indirect way to delete an object of a
32  particular type.
33 
34  The generic implementation of this class simply calls 'delete', but you can
35  create a specialised version of it for a particular class if you need to
36  delete that type of object in a more appropriate way.
37 */
38 template<typename ObjectType>
40 {
41  static void destroy(ObjectType* const object)
42  {
43  delete object;
44  }
45 };
46 
47 //==============================================================================
48 /**
49  This class holds a pointer which is automatically deleted when this object goes
50  out of scope.
51 
52  Once a pointer has been passed to a ScopedPointer, it will make sure that the pointer
53  gets deleted when the ScopedPointer is deleted. Using the ScopedPointer on the stack or
54  as member variables is a good way to use RAII to avoid accidentally leaking dynamically
55  created objects.
56 
57  A ScopedPointer can be used in pretty much the same way that you'd use a normal pointer
58  to an object. If you use the assignment operator to assign a different object to a
59  ScopedPointer, the old one will be automatically deleted.
60 
61  A const ScopedPointer is guaranteed not to lose ownership of its object or change the
62  object to which it points during its lifetime. This means that making a copy of a const
63  ScopedPointer is impossible, as that would involve the new copy taking ownership from the
64  old one.
65 
66  If you need to get a pointer out of a ScopedPointer without it being deleted, you
67  can use the release() method.
68 
69  Something to note is the main difference between this class and the std::auto_ptr class,
70  which is that ScopedPointer provides a cast-to-object operator, wheras std::auto_ptr
71  requires that you always call get() to retrieve the pointer. The advantages of providing
72  the cast is that you don't need to call get(), so can use the ScopedPointer in pretty much
73  exactly the same way as a raw pointer. The disadvantage is that the compiler is free to
74  use the cast in unexpected and sometimes dangerous ways - in particular, it becomes difficult
75  to return a ScopedPointer as the result of a function. To avoid this causing errors,
76  ScopedPointer contains an overloaded constructor that should cause a syntax error in these
77  circumstances, but it does mean that instead of returning a ScopedPointer from a function,
78  you'd need to return a raw pointer (or use a std::auto_ptr instead).
79 */
80 template<class ObjectType>
82 {
83 public:
84  //==============================================================================
85  /** Creates a ScopedPointer containing a null pointer. */
86  ScopedPointer() noexcept
87  : object(nullptr) {}
88 
89  /** Creates a ScopedPointer that owns the specified object. */
90  ScopedPointer(ObjectType* const objectToTakePossessionOf) noexcept
91  : object(objectToTakePossessionOf) {}
92 
93  /** Creates a ScopedPointer that takes its pointer from another ScopedPointer.
94 
95  Because a pointer can only belong to one ScopedPointer, this transfers
96  the pointer from the other object to this one, and the other object is reset to
97  be a null pointer.
98  */
99  ScopedPointer(ScopedPointer& objectToTransferFrom) noexcept
100  : object(objectToTransferFrom.object)
101  {
102  objectToTransferFrom.object = nullptr;
103  }
104 
105  /** Destructor.
106  This will delete the object that this ScopedPointer currently refers to.
107  */
109  {
111  }
112 
113  /** Changes this ScopedPointer to point to a new object.
114 
115  Because a pointer can only belong to one ScopedPointer, this transfers
116  the pointer from the other object to this one, and the other object is reset to
117  be a null pointer.
118 
119  If this ScopedPointer already points to an object, that object
120  will first be deleted.
121  */
122  ScopedPointer& operator=(ScopedPointer& objectToTransferFrom)
123  {
124  if (this != objectToTransferFrom.getAddress())
125  {
126  // Two ScopedPointers should never be able to refer to the same object - if
127  // this happens, you must have done something dodgy!
128  DISTRHO_SAFE_ASSERT_RETURN(object == nullptr || object != objectToTransferFrom.object, *this);
129 
130  ObjectType* const oldObject = object;
131  object = objectToTransferFrom.object;
132  objectToTransferFrom.object = nullptr;
134  }
135 
136  return *this;
137  }
138 
139  /** Changes this ScopedPointer to point to a new object.
140 
141  If this ScopedPointer already points to an object, that object
142  will first be deleted.
143 
144  The pointer that you pass in may be a nullptr.
145  */
146  ScopedPointer& operator=(ObjectType* const newObjectToTakePossessionOf)
147  {
148  if (object != newObjectToTakePossessionOf)
149  {
150  ObjectType* const oldObject = object;
151  object = newObjectToTakePossessionOf;
153  }
154 
155  return *this;
156  }
157 
158  //==============================================================================
159  /** Returns the object that this ScopedPointer refers to. */
160  operator ObjectType*() const noexcept { return object; }
161 
162  /** Returns the object that this ScopedPointer refers to. */
163  ObjectType* get() const noexcept { return object; }
164 
165  /** Returns the object that this ScopedPointer refers to. */
166  ObjectType& operator*() const noexcept { return *object; }
167 
168  /** Lets you access methods and properties of the object that this ScopedPointer refers to. */
169  ObjectType* operator->() const noexcept { return object; }
170 
171  //==============================================================================
172  /** Removes the current object from this ScopedPointer without deleting it.
173  This will return the current object, and set the ScopedPointer to a null pointer.
174  */
175  ObjectType* release() noexcept { ObjectType* const o = object; object = nullptr; return o; }
176 
177  //==============================================================================
178  /** Swaps this object with that of another ScopedPointer.
179  The two objects simply exchange their pointers.
180  */
181  void swapWith(ScopedPointer<ObjectType>& other) noexcept
182  {
183  // Two ScopedPointers should never be able to refer to the same object - if
184  // this happens, you must have done something dodgy!
185  DISTRHO_SAFE_ASSERT_RETURN(object != other.object || this == other.getAddress() || object == nullptr,);
186 
187  std::swap(object, other.object);
188  }
189 
190 private:
191  //==============================================================================
192  ObjectType* object;
193 
194  // (Required as an alternative to the overloaded & operator).
195  const ScopedPointer* getAddress() const noexcept { return this; }
196 
197 #ifndef _MSC_VER // (MSVC can't deal with multiple copy constructors)
198  /* The copy constructors are private to stop people accidentally copying a const ScopedPointer
199  (the compiler would let you do so by implicitly casting the source to its raw object pointer).
200 
201  A side effect of this is that in a compiler that doesn't support C++11, you may hit an
202  error when you write something like this:
203 
204  ScopedPointer<MyClass> m = new MyClass(); // Compile error: copy constructor is private.
205 
206  Even though the compiler would normally ignore the assignment here, it can't do so when the
207  copy constructor is private. It's very easy to fix though - just write it like this:
208 
209  ScopedPointer<MyClass> m (new MyClass()); // Compiles OK
210 
211  It's probably best to use the latter form when writing your object declarations anyway, as
212  this is a better representation of the code that you actually want the compiler to produce.
213  */
214 # ifdef DISTRHO_PROPER_CPP11_SUPPORT
215  ScopedPointer(const ScopedPointer&) = delete;
216  ScopedPointer& operator=(const ScopedPointer&) = delete;
217 # else
220 # endif
221 #endif
222 };
223 
224 //==============================================================================
225 /** Compares a ScopedPointer with another pointer.
226  This can be handy for checking whether this is a null pointer.
227 */
228 template<class ObjectType>
229 bool operator==(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
230 {
231  return static_cast<ObjectType*>(pointer1) == pointer2;
232 }
233 
234 /** Compares a ScopedPointer with another pointer.
235  This can be handy for checking whether this is a null pointer.
236 */
237 template<class ObjectType>
238 bool operator!=(const ScopedPointer<ObjectType>& pointer1, ObjectType* const pointer2) noexcept
239 {
240  return static_cast<ObjectType*>(pointer1) != pointer2;
241 }
242 
243 // -----------------------------------------------------------------------
244 
245 END_NAMESPACE_DISTRHO
246 
247 #endif // DISTRHO_SCOPED_POINTER_HPP_INCLUDED
ObjectType * release() noexcept
Definition: d_scopedpointer.hpp:175
~ScopedPointer()
Definition: d_scopedpointer.hpp:108
ObjectType & operator*() const noexcept
Definition: d_scopedpointer.hpp:166
ScopedPointer(ObjectType *const objectToTakePossessionOf) noexcept
Definition: d_scopedpointer.hpp:90
ObjectType * operator->() const noexcept
Definition: d_scopedpointer.hpp:169
ScopedPointer(ScopedPointer &objectToTransferFrom) noexcept
Definition: d_scopedpointer.hpp:99
void swapWith(ScopedPointer< ObjectType > &other) noexcept
Definition: d_scopedpointer.hpp:181
Definition: d_scopedpointer.hpp:81
ScopedPointer() noexcept
Definition: d_scopedpointer.hpp:86
Definition: d_scopedpointer.hpp:39
ScopedPointer & operator=(ObjectType *const newObjectToTakePossessionOf)
Definition: d_scopedpointer.hpp:146
ScopedPointer & operator=(ScopedPointer &objectToTransferFrom)
Definition: d_scopedpointer.hpp:122