webdriver: Implement Fullscreen command support (#100)
[gecko.git] / mfbt / UniquePtr.h
blob7e1035bc6e0bded5a0c1a2c108f10a60395d0a83
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Smart pointer managing sole ownership of a resource. */
9 #ifndef mozilla_UniquePtr_h
10 #define mozilla_UniquePtr_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/Compiler.h"
15 #include "mozilla/Move.h"
16 #include "mozilla/Pair.h"
17 #include "mozilla/TypeTraits.h"
19 namespace mozilla {
21 template<typename T> class DefaultDelete;
22 template<typename T, class D = DefaultDelete<T>> class UniquePtr;
24 } // namespace mozilla
26 namespace mozilla {
28 namespace detail {
30 struct HasPointerTypeHelper
32 template <class U> static double Test(...);
33 template <class U> static char Test(typename U::pointer* = 0);
36 template <class T>
37 class HasPointerType : public IntegralConstant<bool, sizeof(HasPointerTypeHelper::Test<T>(0)) == 1>
41 template <class T, class D, bool = HasPointerType<D>::value>
42 struct PointerTypeImpl
44 typedef typename D::pointer Type;
47 template <class T, class D>
48 struct PointerTypeImpl<T, D, false>
50 typedef T* Type;
53 template <class T, class D>
54 struct PointerType
56 typedef typename PointerTypeImpl<T, typename RemoveReference<D>::Type>::Type Type;
59 } // namespace detail
61 /**
62 * UniquePtr is a smart pointer that wholly owns a resource. Ownership may be
63 * transferred out of a UniquePtr through explicit action, but otherwise the
64 * resource is destroyed when the UniquePtr is destroyed.
66 * UniquePtr is similar to C++98's std::auto_ptr, but it improves upon auto_ptr
67 * in one crucial way: it's impossible to copy a UniquePtr. Copying an auto_ptr
68 * obviously *can't* copy ownership of its singly-owned resource. So what
69 * happens if you try to copy one? Bizarrely, ownership is implicitly
70 * *transferred*, preserving single ownership but breaking code that assumes a
71 * copy of an object is identical to the original. (This is why auto_ptr is
72 * prohibited in STL containers.)
74 * UniquePtr solves this problem by being *movable* rather than copyable.
75 * Instead of passing a |UniquePtr u| directly to the constructor or assignment
76 * operator, you pass |Move(u)|. In doing so you indicate that you're *moving*
77 * ownership out of |u|, into the target of the construction/assignment. After
78 * the transfer completes, |u| contains |nullptr| and may be safely destroyed.
79 * This preserves single ownership but also allows UniquePtr to be moved by
80 * algorithms that have been made move-safe. (Note: if |u| is instead a
81 * temporary expression, don't use |Move()|: just pass the expression, because
82 * it's already move-ready. For more information see Move.h.)
84 * UniquePtr is also better than std::auto_ptr in that the deletion operation is
85 * customizable. An optional second template parameter specifies a class that
86 * (through its operator()(T*)) implements the desired deletion policy. If no
87 * policy is specified, mozilla::DefaultDelete<T> is used -- which will either
88 * |delete| or |delete[]| the resource, depending whether the resource is an
89 * array. Custom deletion policies ideally should be empty classes (no member
90 * fields, no member fields in base classes, no virtual methods/inheritance),
91 * because then UniquePtr can be just as efficient as a raw pointer.
93 * Use of UniquePtr proceeds like so:
95 * UniquePtr<int> g1; // initializes to nullptr
96 * g1.reset(new int); // switch resources using reset()
97 * g1 = nullptr; // clears g1, deletes the int
99 * UniquePtr<int> g2(new int); // owns that int
100 * int* p = g2.release(); // g2 leaks its int -- still requires deletion
101 * delete p; // now freed
103 * struct S { int x; S(int x) : x(x) {} };
104 * UniquePtr<S> g3, g4(new S(5));
105 * g3 = Move(g4); // g3 owns the S, g4 cleared
106 * S* p = g3.get(); // g3 still owns |p|
107 * assert(g3->x == 5); // operator-> works (if .get() != nullptr)
108 * assert((*g3).x == 5); // also operator* (again, if not cleared)
109 * Swap(g3, g4); // g4 now owns the S, g3 cleared
110 * g3.swap(g4); // g3 now owns the S, g4 cleared
111 * UniquePtr<S> g5(Move(g3)); // g5 owns the S, g3 cleared
112 * g5.reset(); // deletes the S, g5 cleared
114 * struct FreePolicy { void operator()(void* p) { free(p); } };
115 * UniquePtr<int, FreePolicy> g6(static_cast<int*>(malloc(sizeof(int))));
116 * int* ptr = g6.get();
117 * g6 = nullptr; // calls free(ptr)
119 * Now, carefully note a few things you *can't* do:
121 * UniquePtr<int> b1;
122 * b1 = new int; // BAD: can only assign another UniquePtr
123 * int* ptr = b1; // BAD: no auto-conversion to pointer, use get()
125 * UniquePtr<int> b2(b1); // BAD: can't copy a UniquePtr
126 * UniquePtr<int> b3 = b1; // BAD: can't copy-assign a UniquePtr
128 * (Note that changing a UniquePtr to store a direct |new| expression is
129 * permitted, but usually you should use MakeUnique, defined at the end of this
130 * header.)
132 * A few miscellaneous notes:
134 * UniquePtr, when not instantiated for an array type, can be move-constructed
135 * and move-assigned, not only from itself but from "derived" UniquePtr<U, E>
136 * instantiations where U converts to T and E converts to D. If you want to use
137 * this, you're going to have to specify a deletion policy for both UniquePtr
138 * instantations, and T pretty much has to have a virtual destructor. In other
139 * words, this doesn't work:
141 * struct Base { virtual ~Base() {} };
142 * struct Derived : Base {};
144 * UniquePtr<Base> b1;
145 * // BAD: DefaultDelete<Base> and DefaultDelete<Derived> don't interconvert
146 * UniquePtr<Derived> d1(Move(b));
148 * UniquePtr<Base> b2;
149 * UniquePtr<Derived, DefaultDelete<Base>> d2(Move(b2)); // okay
151 * UniquePtr is specialized for array types. Specializing with an array type
152 * creates a smart-pointer version of that array -- not a pointer to such an
153 * array.
155 * UniquePtr<int[]> arr(new int[5]);
156 * arr[0] = 4;
158 * What else is different? Deletion of course uses |delete[]|. An operator[]
159 * is provided. Functionality that doesn't make sense for arrays is removed.
160 * The constructors and mutating methods only accept array pointers (not T*, U*
161 * that converts to T*, or UniquePtr<U[]> or UniquePtr<U>) or |nullptr|.
163 * It's perfectly okay for a function to return a UniquePtr. This transfers
164 * the UniquePtr's sole ownership of the data, to the fresh UniquePtr created
165 * in the calling function, that will then solely own that data. Such functions
166 * can return a local variable UniquePtr, |nullptr|, |UniquePtr(ptr)| where
167 * |ptr| is a |T*|, or a UniquePtr |Move()|'d from elsewhere.
169 * UniquePtr will commonly be a member of a class, with lifetime equivalent to
170 * that of that class. If you want to expose the related resource, you could
171 * expose a raw pointer via |get()|, but ownership of a raw pointer is
172 * inherently unclear. So it's better to expose a |const UniquePtr&| instead.
173 * This prohibits mutation but still allows use of |get()| when needed (but
174 * operator-> is preferred). Of course, you can only use this smart pointer as
175 * long as the enclosing class instance remains live -- no different than if you
176 * exposed the |get()| raw pointer.
178 * To pass a UniquePtr-managed resource as a pointer, use a |const UniquePtr&|
179 * argument. To specify an inout parameter (where the method may or may not
180 * take ownership of the resource, or reset it), or to specify an out parameter
181 * (where simply returning a |UniquePtr| isn't possible), use a |UniquePtr&|
182 * argument. To unconditionally transfer ownership of a UniquePtr
183 * into a method, use a |UniquePtr| argument. To conditionally transfer
184 * ownership of a resource into a method, should the method want it, use a
185 * |UniquePtr&&| argument.
187 template<typename T, class D>
188 class UniquePtr
190 public:
191 typedef T ElementType;
192 typedef D DeleterType;
193 typedef typename detail::PointerType<T, DeleterType>::Type Pointer;
195 private:
196 Pair<Pointer, DeleterType> mTuple;
198 Pointer& ptr() { return mTuple.first(); }
199 const Pointer& ptr() const { return mTuple.first(); }
201 DeleterType& del() { return mTuple.second(); }
202 const DeleterType& del() const { return mTuple.second(); }
204 public:
206 * Construct a UniquePtr containing |nullptr|.
208 constexpr UniquePtr()
209 : mTuple(static_cast<Pointer>(nullptr), DeleterType())
211 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
212 static_assert(!IsReference<D>::value, "must provide a deleter instance");
216 * Construct a UniquePtr containing |aPtr|.
218 explicit UniquePtr(Pointer aPtr)
219 : mTuple(aPtr, DeleterType())
221 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
222 static_assert(!IsReference<D>::value, "must provide a deleter instance");
225 UniquePtr(Pointer aPtr,
226 typename Conditional<IsReference<D>::value,
228 const D&>::Type aD1)
229 : mTuple(aPtr, aD1)
232 // If you encounter an error with MSVC10 about RemoveReference below, along
233 // the lines that "more than one partial specialization matches the template
234 // argument list": don't use UniquePtr<T, reference to function>! Ideally
235 // you should make deletion use the same function every time, using a
236 // deleter policy:
238 // // BAD, won't compile with MSVC10, deleter doesn't need to be a
239 // // variable at all
240 // typedef void (&FreeSignature)(void*);
241 // UniquePtr<int, FreeSignature> ptr((int*) malloc(sizeof(int)), free);
243 // // GOOD, compiles with MSVC10, deletion behavior statically known and
244 // // optimizable
245 // struct DeleteByFreeing
246 // {
247 // void operator()(void* aPtr) { free(aPtr); }
248 // };
250 // If deletion really, truly, must be a variable: you might be able to work
251 // around this with a deleter class that contains the function reference.
252 // But this workaround is untried and untested, because variable deletion
253 // behavior really isn't something you should use.
254 UniquePtr(Pointer aPtr,
255 typename RemoveReference<D>::Type&& aD2)
256 : mTuple(aPtr, Move(aD2))
258 static_assert(!IsReference<D>::value,
259 "rvalue deleter can't be stored by reference");
262 UniquePtr(UniquePtr&& aOther)
263 : mTuple(aOther.release(), Forward<DeleterType>(aOther.get_deleter()))
266 MOZ_IMPLICIT
267 UniquePtr(decltype(nullptr))
268 : mTuple(nullptr, DeleterType())
270 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
271 static_assert(!IsReference<D>::value, "must provide a deleter instance");
274 template<typename U, class E>
275 MOZ_IMPLICIT
276 UniquePtr(UniquePtr<U, E>&& aOther,
277 typename EnableIf<IsConvertible<typename UniquePtr<U, E>::Pointer,
278 Pointer>::value &&
279 !IsArray<U>::value &&
280 (IsReference<D>::value
281 ? IsSame<D, E>::value
282 : IsConvertible<E, D>::value),
283 int>::Type aDummy = 0)
284 : mTuple(aOther.release(), Forward<E>(aOther.get_deleter()))
288 ~UniquePtr() { reset(nullptr); }
290 UniquePtr& operator=(UniquePtr&& aOther)
292 reset(aOther.release());
293 get_deleter() = Forward<DeleterType>(aOther.get_deleter());
294 return *this;
297 template<typename U, typename E>
298 UniquePtr& operator=(UniquePtr<U, E>&& aOther)
300 static_assert(IsConvertible<typename UniquePtr<U, E>::Pointer,
301 Pointer>::value,
302 "incompatible UniquePtr pointees");
303 static_assert(!IsArray<U>::value,
304 "can't assign from UniquePtr holding an array");
306 reset(aOther.release());
307 get_deleter() = Forward<E>(aOther.get_deleter());
308 return *this;
311 UniquePtr& operator=(decltype(nullptr))
313 reset(nullptr);
314 return *this;
317 T& operator*() const { return *get(); }
318 Pointer operator->() const
320 MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr");
321 return get();
324 explicit operator bool() const { return get() != nullptr; }
326 Pointer get() const { return ptr(); }
328 DeleterType& get_deleter() { return del(); }
329 const DeleterType& get_deleter() const { return del(); }
331 MOZ_MUST_USE Pointer release()
333 Pointer p = ptr();
334 ptr() = nullptr;
335 return p;
338 void reset(Pointer aPtr = Pointer())
340 Pointer old = ptr();
341 ptr() = aPtr;
342 if (old != nullptr) {
343 get_deleter()(old);
347 void swap(UniquePtr& aOther)
349 mTuple.swap(aOther.mTuple);
352 UniquePtr(const UniquePtr& aOther) = delete; // construct using Move()!
353 void operator=(const UniquePtr& aOther) = delete; // assign using Move()!
356 // In case you didn't read the comment by the main definition (you should!): the
357 // UniquePtr<T[]> specialization exists to manage array pointers. It deletes
358 // such pointers using delete[], it will reject construction and modification
359 // attempts using U* or U[]. Otherwise it works like the normal UniquePtr.
360 template<typename T, class D>
361 class UniquePtr<T[], D>
363 public:
364 typedef T* Pointer;
365 typedef T ElementType;
366 typedef D DeleterType;
368 private:
369 Pair<Pointer, DeleterType> mTuple;
371 public:
373 * Construct a UniquePtr containing nullptr.
375 constexpr UniquePtr()
376 : mTuple(static_cast<Pointer>(nullptr), DeleterType())
378 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
379 static_assert(!IsReference<D>::value, "must provide a deleter instance");
383 * Construct a UniquePtr containing |aPtr|.
385 explicit UniquePtr(Pointer aPtr)
386 : mTuple(aPtr, DeleterType())
388 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
389 static_assert(!IsReference<D>::value, "must provide a deleter instance");
392 // delete[] knows how to handle *only* an array of a single class type. For
393 // delete[] to work correctly, it must know the size of each element, the
394 // fields and base classes of each element requiring destruction, and so on.
395 // So forbid all overloads which would end up invoking delete[] on a pointer
396 // of the wrong type.
397 template<typename U>
398 UniquePtr(U&& aU,
399 typename EnableIf<IsPointer<U>::value &&
400 IsConvertible<U, Pointer>::value,
401 int>::Type aDummy = 0)
402 = delete;
404 UniquePtr(Pointer aPtr,
405 typename Conditional<IsReference<D>::value,
407 const D&>::Type aD1)
408 : mTuple(aPtr, aD1)
411 // If you encounter an error with MSVC10 about RemoveReference below, along
412 // the lines that "more than one partial specialization matches the template
413 // argument list": don't use UniquePtr<T[], reference to function>! See the
414 // comment by this constructor in the non-T[] specialization above.
415 UniquePtr(Pointer aPtr,
416 typename RemoveReference<D>::Type&& aD2)
417 : mTuple(aPtr, Move(aD2))
419 static_assert(!IsReference<D>::value,
420 "rvalue deleter can't be stored by reference");
423 // Forbidden for the same reasons as stated above.
424 template<typename U, typename V>
425 UniquePtr(U&& aU, V&& aV,
426 typename EnableIf<IsPointer<U>::value &&
427 IsConvertible<U, Pointer>::value,
428 int>::Type aDummy = 0)
429 = delete;
431 UniquePtr(UniquePtr&& aOther)
432 : mTuple(aOther.release(), Forward<DeleterType>(aOther.get_deleter()))
435 MOZ_IMPLICIT
436 UniquePtr(decltype(nullptr))
437 : mTuple(nullptr, DeleterType())
439 static_assert(!IsPointer<D>::value, "must provide a deleter instance");
440 static_assert(!IsReference<D>::value, "must provide a deleter instance");
443 ~UniquePtr() { reset(nullptr); }
445 UniquePtr& operator=(UniquePtr&& aOther)
447 reset(aOther.release());
448 get_deleter() = Forward<DeleterType>(aOther.get_deleter());
449 return *this;
452 UniquePtr& operator=(decltype(nullptr))
454 reset();
455 return *this;
458 explicit operator bool() const { return get() != nullptr; }
460 T& operator[](decltype(sizeof(int)) aIndex) const { return get()[aIndex]; }
461 Pointer get() const { return mTuple.first(); }
463 DeleterType& get_deleter() { return mTuple.second(); }
464 const DeleterType& get_deleter() const { return mTuple.second(); }
466 MOZ_MUST_USE Pointer release()
468 Pointer p = mTuple.first();
469 mTuple.first() = nullptr;
470 return p;
473 void reset(Pointer aPtr = Pointer())
475 Pointer old = mTuple.first();
476 mTuple.first() = aPtr;
477 if (old != nullptr) {
478 mTuple.second()(old);
482 void reset(decltype(nullptr))
484 Pointer old = mTuple.first();
485 mTuple.first() = nullptr;
486 if (old != nullptr) {
487 mTuple.second()(old);
491 template<typename U>
492 void reset(U) = delete;
494 void swap(UniquePtr& aOther) { mTuple.swap(aOther.mTuple); }
496 UniquePtr(const UniquePtr& aOther) = delete; // construct using Move()!
497 void operator=(const UniquePtr& aOther) = delete; // assign using Move()!
501 * A default deletion policy using plain old operator delete.
503 * Note that this type can be specialized, but authors should beware of the risk
504 * that the specialization may at some point cease to match (either because it
505 * gets moved to a different compilation unit or the signature changes). If the
506 * non-specialized (|delete|-based) version compiles for that type but does the
507 * wrong thing, bad things could happen.
509 * This is a non-issue for types which are always incomplete (i.e. opaque handle
510 * types), since |delete|-ing such a type will always trigger a compilation
511 * error.
513 template<typename T>
514 class DefaultDelete
516 public:
517 constexpr DefaultDelete() {}
519 template<typename U>
520 MOZ_IMPLICIT DefaultDelete(const DefaultDelete<U>& aOther,
521 typename EnableIf<mozilla::IsConvertible<U*, T*>::value,
522 int>::Type aDummy = 0)
525 void operator()(T* aPtr) const
527 static_assert(sizeof(T) > 0, "T must be complete");
528 delete aPtr;
532 /** A default deletion policy using operator delete[]. */
533 template<typename T>
534 class DefaultDelete<T[]>
536 public:
537 constexpr DefaultDelete() {}
539 void operator()(T* aPtr) const
541 static_assert(sizeof(T) > 0, "T must be complete");
542 delete[] aPtr;
545 template<typename U>
546 void operator()(U* aPtr) const = delete;
549 template<typename T, class D>
550 void
551 Swap(UniquePtr<T, D>& aX, UniquePtr<T, D>& aY)
553 aX.swap(aY);
556 template<typename T, class D, typename U, class E>
557 bool
558 operator==(const UniquePtr<T, D>& aX, const UniquePtr<U, E>& aY)
560 return aX.get() == aY.get();
563 template<typename T, class D, typename U, class E>
564 bool
565 operator!=(const UniquePtr<T, D>& aX, const UniquePtr<U, E>& aY)
567 return aX.get() != aY.get();
570 template<typename T, class D>
571 bool
572 operator==(const UniquePtr<T, D>& aX, decltype(nullptr))
574 return !aX;
577 template<typename T, class D>
578 bool
579 operator==(decltype(nullptr), const UniquePtr<T, D>& aX)
581 return !aX;
584 template<typename T, class D>
585 bool
586 operator!=(const UniquePtr<T, D>& aX, decltype(nullptr))
588 return bool(aX);
591 template<typename T, class D>
592 bool
593 operator!=(decltype(nullptr), const UniquePtr<T, D>& aX)
595 return bool(aX);
598 // No operator<, operator>, operator<=, operator>= for now because simplicity.
600 namespace detail {
602 template<typename T>
603 struct UniqueSelector
605 typedef UniquePtr<T> SingleObject;
608 template<typename T>
609 struct UniqueSelector<T[]>
611 typedef UniquePtr<T[]> UnknownBound;
614 template<typename T, decltype(sizeof(int)) N>
615 struct UniqueSelector<T[N]>
617 typedef UniquePtr<T[N]> KnownBound;
620 } // namespace detail
623 * MakeUnique is a helper function for allocating new'd objects and arrays,
624 * returning a UniquePtr containing the resulting pointer. The semantics of
625 * MakeUnique<Type>(...) are as follows.
627 * If Type is an array T[n]:
628 * Disallowed, deleted, no overload for you!
629 * If Type is an array T[]:
630 * MakeUnique<T[]>(size_t) is the only valid overload. The pointer returned
631 * is as if by |new T[n]()|, which value-initializes each element. (If T
632 * isn't a class type, this will zero each element. If T is a class type,
633 * then roughly speaking, each element will be constructed using its default
634 * constructor. See C++11 [dcl.init]p7 for the full gory details.)
635 * If Type is non-array T:
636 * The arguments passed to MakeUnique<T>(...) are forwarded into a
637 * |new T(...)| call, initializing the T as would happen if executing
638 * |T(...)|.
640 * There are various benefits to using MakeUnique instead of |new| expressions.
642 * First, MakeUnique eliminates use of |new| from code entirely. If objects are
643 * only created through UniquePtr, then (assuming all explicit release() calls
644 * are safe, including transitively, and no type-safety casting funniness)
645 * correctly maintained ownership of the UniquePtr guarantees no leaks are
646 * possible. (This pays off best if a class is only ever created through a
647 * factory method on the class, using a private constructor.)
649 * Second, initializing a UniquePtr using a |new| expression requires repeating
650 * the name of the new'd type, whereas MakeUnique in concert with the |auto|
651 * keyword names it only once:
653 * UniquePtr<char> ptr1(new char()); // repetitive
654 * auto ptr2 = MakeUnique<char>(); // shorter
656 * Of course this assumes the reader understands the operation MakeUnique
657 * performs. In the long run this is probably a reasonable assumption. In the
658 * short run you'll have to use your judgment about what readers can be expected
659 * to know, or to quickly look up.
661 * Third, a call to MakeUnique can be assigned directly to a UniquePtr. In
662 * contrast you can't assign a pointer into a UniquePtr without using the
663 * cumbersome reset().
665 * UniquePtr<char> p;
666 * p = new char; // ERROR
667 * p.reset(new char); // works, but fugly
668 * p = MakeUnique<char>(); // preferred
670 * (And third, although not relevant to Mozilla: MakeUnique is exception-safe.
671 * An exception thrown after |new T| succeeds will leak that memory, unless the
672 * pointer is assigned to an object that will manage its ownership. UniquePtr
673 * ably serves this function.)
676 template<typename T, typename... Args>
677 typename detail::UniqueSelector<T>::SingleObject
678 MakeUnique(Args&&... aArgs)
680 return UniquePtr<T>(new T(Forward<Args>(aArgs)...));
683 template<typename T>
684 typename detail::UniqueSelector<T>::UnknownBound
685 MakeUnique(decltype(sizeof(int)) aN)
687 typedef typename RemoveExtent<T>::Type ArrayType;
688 return UniquePtr<T>(new ArrayType[aN]());
691 template<typename T, typename... Args>
692 typename detail::UniqueSelector<T>::KnownBound
693 MakeUnique(Args&&... aArgs) = delete;
695 } // namespace mozilla
697 #endif /* mozilla_UniquePtr_h */