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 /* Useful extensions to UniquePtr. */
9 #ifndef mozilla_UniquePtrExtensions_h
10 #define mozilla_UniquePtrExtensions_h
12 #include "mozilla/fallible.h"
13 #include "mozilla/UniquePtr.h"
18 * MakeUniqueFallible works exactly like MakeUnique, except that the memory
19 * allocation performed is done fallibly, i.e. it can return nullptr.
21 template <typename T
, typename
... Args
>
22 typename
detail::UniqueSelector
<T
>::SingleObject
MakeUniqueFallible(
24 return UniquePtr
<T
>(new (fallible
) T(std::forward
<Args
>(aArgs
)...));
28 typename
detail::UniqueSelector
<T
>::UnknownBound
MakeUniqueFallible(
29 decltype(sizeof(int)) aN
) {
30 typedef typename RemoveExtent
<T
>::Type ArrayType
;
31 return UniquePtr
<T
>(new (fallible
) ArrayType
[aN
]());
34 template <typename T
, typename
... Args
>
35 typename
detail::UniqueSelector
<T
>::KnownBound
MakeUniqueFallible(
36 Args
&&... aArgs
) = delete;
42 void operator()(const void* ptr
) { free(const_cast<void*>(ptr
)); }
48 using UniqueFreePtr
= UniquePtr
<T
, detail::FreePolicy
<T
>>;
50 } // namespace mozilla
52 #endif // mozilla_UniquePtrExtensions_h