Bug 1617327 - Part 1: Add getter_Transfers to UniquePtrExtensions. r=KrisWright

This adds a `getter_Transfers` implementation for `UniquePtr` suitable as a drop-in replacement for the `nsAutoPtr` variant.

Differential Revision: https://phabricator.services.mozilla.com/D63744

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Eric Rahm 2020-02-24 23:27:56 +00:00
Родитель 46909417e6
Коммит fd0824a8e6
1 изменённых файлов: 26 добавлений и 0 удалений

Просмотреть файл

@ -9,6 +9,7 @@
#ifndef mozilla_UniquePtrExtensions_h #ifndef mozilla_UniquePtrExtensions_h
#define mozilla_UniquePtrExtensions_h #define mozilla_UniquePtrExtensions_h
#include "mozilla/Attributes.h"
#include "mozilla/fallible.h" #include "mozilla/fallible.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
@ -121,6 +122,31 @@ using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
using UniqueFileHandle = using UniqueFileHandle =
UniquePtr<detail::FileHandleType, detail::FileHandleDeleter>; UniquePtr<detail::FileHandleType, detail::FileHandleDeleter>;
// Helper for passing a UniquePtr to an old-style function that uses raw
// pointers for out params. Example usage:
//
// void AllocateFoo(Foo** out) { *out = new Foo(); }
// UniquePtr<Foo> foo;
// AllocateFoo(getter_Transfers(foo));
template <typename T, typename D>
auto getter_Transfers(UniquePtr<T, D>& up) {
class MOZ_TEMPORARY_CLASS UniquePtrGetterTransfers {
public:
using Ptr = UniquePtr<T, D>;
explicit UniquePtrGetterTransfers(Ptr& p) : mPtr(p) {}
~UniquePtrGetterTransfers() { mPtr.reset(mRawPtr); }
operator typename Ptr::ElementType**() { return &mRawPtr; }
typename Ptr::ElementType*& operator*() { return mRawPtr; }
private:
Ptr& mPtr;
typename Ptr::Pointer mRawPtr = nullptr;
};
return UniquePtrGetterTransfers(up);
}
} // namespace mozilla } // namespace mozilla
#endif // mozilla_UniquePtrExtensions_h #endif // mozilla_UniquePtrExtensions_h