Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / base / DarwinObjectPtr.h
blob3d5842553b64974add815b6dc3a99f5d3a3751e8
1 /*
2 * Copyright (C) 2014-2021 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 // Adapted from
27 // https://github.com/WebKit/WebKit/blob/ad340677c00a4b4b6a299c93a6e18cd073b3f4e9/Source/WTF/wtf/OSObjectPtr.h
29 #ifndef mozilla_DarwinObjectPtr_h
30 #define mozilla_DarwinObjectPtr_h
32 #include <os/object.h>
33 #include <utility>
34 #include "mozilla/Attributes.h"
36 // Because ARC enablement is a compile-time choice, and we compile this header
37 // both ways, we need a separate copy of our code when ARC is enabled.
38 #if __has_feature(objc_arc)
39 # define AdoptDarwinObject AdoptDarwinObjectArc
40 # define RetainDarwinObject RetainDarwinObjectArc
41 # define ReleaseDarwinObject ReleaseDarwinObjectArc
42 #endif
44 namespace mozilla {
46 template <typename>
47 class DarwinObjectPtr;
48 template <typename T>
49 [[nodiscard]] DarwinObjectPtr<T> AdoptDarwinObject(T);
51 template <typename T>
52 static inline void RetainDarwinObject(T aPtr) {
53 #if !__has_feature(objc_arc)
54 os_retain(aPtr);
55 #endif
58 template <typename T>
59 static inline void ReleaseDarwinObject(T aPtr) {
60 #if !__has_feature(objc_arc)
61 os_release(aPtr);
62 #endif
65 template <typename T>
66 class DarwinObjectPtr {
67 public:
68 DarwinObjectPtr() : mPtr(nullptr) {}
70 ~DarwinObjectPtr() {
71 if (mPtr) {
72 ReleaseDarwinObject(mPtr);
76 T get() const { return mPtr; }
78 explicit operator bool() const { return mPtr; }
79 bool operator!() const { return !mPtr; }
81 DarwinObjectPtr(const DarwinObjectPtr& aOther) : mPtr(aOther.mPtr) {
82 if (mPtr) {
83 RetainDarwinObject(mPtr);
87 DarwinObjectPtr(DarwinObjectPtr&& aOther) : mPtr(std::move(aOther.mPtr)) {
88 aOther.mPtr = nullptr;
91 MOZ_IMPLICIT DarwinObjectPtr(T aPtr) : mPtr(std::move(aPtr)) {
92 if (mPtr) {
93 RetainDarwinObject(mPtr);
97 DarwinObjectPtr& operator=(const DarwinObjectPtr& aOther) {
98 DarwinObjectPtr ptr = aOther;
99 swap(ptr);
100 return *this;
103 DarwinObjectPtr& operator=(DarwinObjectPtr&& aOther) {
104 DarwinObjectPtr ptr = std::move(aOther);
105 swap(ptr);
106 return *this;
109 DarwinObjectPtr& operator=(std::nullptr_t) {
110 if (mPtr) {
111 ReleaseDarwinObject(mPtr);
113 mPtr = nullptr;
114 return *this;
117 DarwinObjectPtr& operator=(T aOther) {
118 DarwinObjectPtr ptr = std::move(aOther);
119 swap(ptr);
120 return *this;
123 void swap(DarwinObjectPtr& aOther) { std::swap(mPtr, aOther.mPtr); }
125 [[nodiscard]] T forget() { return std::exchange(mPtr, nullptr); }
127 friend DarwinObjectPtr AdoptDarwinObject<T>(T);
129 private:
130 struct AdoptDarwinObjectTag {};
131 DarwinObjectPtr(AdoptDarwinObjectTag, T aPtr) : mPtr(std::move(aPtr)) {}
133 T mPtr;
136 template <typename T>
137 inline DarwinObjectPtr<T> AdoptDarwinObject(T aPtr) {
138 return DarwinObjectPtr<T>{typename DarwinObjectPtr<T>::AdoptDarwinObjectTag{},
139 std::move(aPtr)};
142 } // namespace mozilla
144 #endif