Bug 1608587 [wpt PR 21137] - Update wpt metadata, a=testonly
[gecko.git] / mfbt / UniquePtrExtensions.h
bloba10d5fc39591b440df3c20c87989fd8e9b7d1085
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 #ifdef XP_WIN
16 # include <cstdint>
17 #endif
19 namespace mozilla {
21 /**
22 * MakeUniqueFallible works exactly like MakeUnique, except that the memory
23 * allocation performed is done fallibly, i.e. it can return nullptr.
25 template <typename T, typename... Args>
26 typename detail::UniqueSelector<T>::SingleObject MakeUniqueFallible(
27 Args&&... aArgs) {
28 return UniquePtr<T>(new (fallible) T(std::forward<Args>(aArgs)...));
31 template <typename T>
32 typename detail::UniqueSelector<T>::UnknownBound MakeUniqueFallible(
33 decltype(sizeof(int)) aN) {
34 typedef typename RemoveExtent<T>::Type ArrayType;
35 return UniquePtr<T>(new (fallible) ArrayType[aN]());
38 template <typename T, typename... Args>
39 typename detail::UniqueSelector<T>::KnownBound MakeUniqueFallible(
40 Args&&... aArgs) = delete;
42 namespace detail {
44 template <typename T>
45 struct FreePolicy {
46 void operator()(const void* ptr) { free(const_cast<void*>(ptr)); }
49 #if defined(XP_WIN)
50 // Can't include <windows.h> to get the actual definition of HANDLE
51 // because of namespace pollution.
52 typedef void* FileHandleType;
53 #elif defined(XP_UNIX)
54 typedef int FileHandleType;
55 #else
56 # error "Unsupported OS?"
57 #endif
59 struct FileHandleHelper {
60 MOZ_IMPLICIT FileHandleHelper(FileHandleType aHandle) : mHandle(aHandle) {}
62 MOZ_IMPLICIT constexpr FileHandleHelper(std::nullptr_t)
63 : mHandle(kInvalidHandle) {}
65 bool operator!=(std::nullptr_t) const {
66 #ifdef XP_WIN
67 // Windows uses both nullptr and INVALID_HANDLE_VALUE (-1 cast to
68 // HANDLE) in different situations, but nullptr is more reliably
69 // null while -1 is also valid input to some calls that take
70 // handles. So class considers both to be null (since neither
71 // should be closed) but default-constructs as nullptr.
72 if (mHandle == (void*)-1) {
73 return false;
75 #endif
76 return mHandle != kInvalidHandle;
79 operator FileHandleType() const { return mHandle; }
81 #ifdef XP_WIN
82 // NSPR uses an integer type for PROsfd, so this conversion is
83 // provided for working with it without needing reinterpret casts
84 // everywhere.
85 operator std::intptr_t() const {
86 return reinterpret_cast<std::intptr_t>(mHandle);
88 #endif
90 // When there's only one user-defined conversion operator, the
91 // compiler will use that to derive equality, but that doesn't work
92 // when the conversion is ambiguoug (the XP_WIN case above).
93 bool operator==(const FileHandleHelper& aOther) const {
94 return mHandle == aOther.mHandle;
97 private:
98 FileHandleType mHandle;
100 #ifdef XP_WIN
101 // See above for why this is nullptr. (Also, INVALID_HANDLE_VALUE
102 // can't be expressed as a constexpr.)
103 static constexpr FileHandleType kInvalidHandle = nullptr;
104 #else
105 static constexpr FileHandleType kInvalidHandle = -1;
106 #endif
109 struct FileHandleDeleter {
110 typedef FileHandleHelper pointer;
111 MFBT_API void operator()(FileHandleHelper aHelper);
114 } // namespace detail
116 template <typename T>
117 using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
119 // A RAII class for the OS construct used for open files and similar
120 // objects: a file descriptor on Unix or a handle on Windows.
121 using UniqueFileHandle =
122 UniquePtr<detail::FileHandleType, detail::FileHandleDeleter>;
124 } // namespace mozilla
126 #endif // mozilla_UniquePtrExtensions_h