Bug 1710145 - Deduction guide RefPtr(already_AddRefed<T>) -> RefPtr<T> - r=kmag

Differential Revision: https://phabricator.services.mozilla.com/D114809
This commit is contained in:
Gerald Squelart 2021-05-11 21:55:21 +00:00
parent e726c98cf2
commit 5cbf88670f
2 changed files with 17 additions and 3 deletions

View File

@ -606,4 +606,13 @@ RefPtr<T> MakeRefPtr(Args&&... aArgs) {
} // namespace mozilla
/**
* Deduction guide to allow simple `RefPtr` definitions from an
* already_AddRefed<T> without repeating the type, e.g.:
*
* RefPtr ptr = MakeAndAddRef<SomeType>(...);
*/
template <typename T>
RefPtr(already_AddRefed<T>) -> RefPtr<T>;
#endif /* mozilla_RefPtr_h */

View File

@ -7,6 +7,8 @@
#include "mozilla/RefPtr.h"
#include "mozilla/RefCounted.h"
#include <type_traits>
using mozilla::RefCounted;
class Foo : public RefCounted<Foo> {
@ -59,8 +61,10 @@ int main() {
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
{
RefPtr<Foo> f1 = NewFoo();
RefPtr<Foo> f2(NewFoo());
RefPtr f1 = NewFoo();
static_assert(std::is_same_v<decltype(f1), RefPtr<Foo>>);
RefPtr f2(NewFoo());
static_assert(std::is_same_v<decltype(f2), RefPtr<Foo>>);
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
}
MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed);
@ -109,7 +113,8 @@ int main() {
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
{
RefPtr<Foo> f = GetNullFoo();
RefPtr f = GetNullFoo();
static_assert(std::is_same_v<decltype(f), RefPtr<Foo>>);
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
}
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);