Bug 1518618 - Add custom classes to the selectors for matches, attributes and pseudoc...
[gecko.git] / mfbt / UniquePtrExtensions.h
blob763a805a44f349a3702c46a21676948ec20e54f1
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"
15 namespace mozilla {
17 /**
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(
23 Args&&... aArgs) {
24 return UniquePtr<T>(new (fallible) T(std::forward<Args>(aArgs)...));
27 template <typename T>
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;
38 namespace detail {
40 template <typename T>
41 struct FreePolicy {
42 void operator()(const void* ptr) { free(const_cast<void*>(ptr)); }
45 } // namespace detail
47 template <typename T>
48 using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
50 } // namespace mozilla
52 #endif // mozilla_UniquePtrExtensions_h