Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / mfbt / AlreadyAddRefed.h
blob900874c6fa0cafd42c5fcc1e4cb781410508b8da
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 /* Typed temporary pointers for reference-counted smart pointers. */
9 #ifndef AlreadyAddRefed_h
10 #define AlreadyAddRefed_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/Move.h"
16 namespace mozilla {
18 struct unused_t;
20 } // namespace mozilla
22 /**
23 * already_AddRefed cooperates with reference counting smart pointers to enable
24 * you to assign in a pointer _without_ |AddRef|ing it. You might want to use
25 * this as a return type from a function that returns an already |AddRef|ed
26 * pointer.
28 * TODO Move already_AddRefed to namespace mozilla. This has not yet been done
29 * because of the sheer number of usages of already_AddRefed.
31 template<class T>
32 struct already_AddRefed
35 * We want to allow returning nullptr from functions returning
36 * already_AddRefed<T>, for simplicity. But we also don't want to allow
37 * returning raw T*, instead preferring creation of already_AddRefed<T> from
38 * a reference counting smart pointer.
40 * We address the latter requirement by making the (T*) constructor explicit.
41 * But |return nullptr| won't consider an explicit constructor, so we need
42 * another constructor to handle it. Plain old (decltype(nullptr)) doesn't
43 * cut it, because if nullptr is emulated as __null (with type int or long),
44 * passing nullptr to an int/long parameter triggers compiler warnings. We
45 * need a type that no one can pass accidentally; a pointer-to-member-function
46 * (where no such function exists) does the trick nicely.
48 * That handles the return-value case. What about for locals, argument types,
49 * and so on? |already_AddRefed<T>(nullptr)| considers both overloads (and
50 * the (already_AddRefed<T>&&) overload as well!), so there's an ambiguity.
51 * We can target true nullptr using decltype(nullptr), but we can't target
52 * emulated nullptr the same way, because passing __null to an int/long
53 * parameter triggers compiler warnings. So just give up on this, and provide
54 * this behavior through the default constructor.
56 * We can revert to simply explicit (T*) and implicit (decltype(nullptr)) when
57 * nullptr no longer needs to be emulated to support the ancient b2g compiler.
58 * (The () overload could also be removed, if desired, if we changed callers.)
60 already_AddRefed() : mRawPtr(nullptr) {}
62 // The return and argument types here are arbitrarily selected so no
63 // corresponding member function exists.
64 typedef void (already_AddRefed::* MatchNullptr)(double, float);
65 MOZ_IMPLICIT already_AddRefed(MatchNullptr aRawPtr) : mRawPtr(nullptr) {}
67 explicit already_AddRefed(T* aRawPtr) : mRawPtr(aRawPtr) {}
69 // Disallowed. Use move semantics instead.
70 already_AddRefed(const already_AddRefed<T>& aOther) = delete;
72 already_AddRefed(already_AddRefed<T>&& aOther) : mRawPtr(aOther.take()) {}
74 /**
75 * This helper is useful in cases like
77 * already_AddRefed<BaseClass>
78 * Foo()
79 * {
80 * nsRefPtr<SubClass> x = ...;
81 * return x.forget();
82 * }
84 * The autoconversion allows one to omit the idiom
86 * nsRefPtr<BaseClass> y = x.forget();
87 * return y.forget();
89 * Note that nsRefPtr is the XPCOM reference counting smart pointer class.
91 template <typename U>
92 already_AddRefed(already_AddRefed<U>&& aOther) : mRawPtr(aOther.take()) {}
94 ~already_AddRefed() { MOZ_ASSERT(!mRawPtr); }
96 // Specialize the unused operator<< for already_AddRefed, to allow
97 // nsCOMPtr<nsIFoo> foo;
98 // unused << foo.forget();
99 // Note that nsCOMPtr is the XPCOM reference counting smart pointer class.
100 friend void operator<<(const mozilla::unused_t& aUnused,
101 const already_AddRefed<T>& aRhs)
103 auto mutableAlreadyAddRefed = const_cast<already_AddRefed<T>*>(&aRhs);
104 aUnused << mutableAlreadyAddRefed->take();
107 MOZ_WARN_UNUSED_RESULT T* take()
109 T* rawPtr = mRawPtr;
110 mRawPtr = nullptr;
111 return rawPtr;
115 * This helper provides a static_cast replacement for already_AddRefed, so
116 * if you have
118 * already_AddRefed<Parent> F();
120 * you can write
122 * already_AddRefed<Child>
123 * G()
125 * return F().downcast<Child>();
128 template<class U>
129 already_AddRefed<U> downcast()
131 U* tmp = static_cast<U*>(mRawPtr);
132 mRawPtr = nullptr;
133 return already_AddRefed<U>(tmp);
136 private:
137 T* MOZ_OWNING_REF mRawPtr;
140 #endif // AlreadyAddRefed_h