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
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.
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>
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
47 class DarwinObjectPtr
;
49 [[nodiscard
]] DarwinObjectPtr
<T
> AdoptDarwinObject(T
);
52 static inline void RetainDarwinObject(T aPtr
) {
53 #if !__has_feature(objc_arc)
59 static inline void ReleaseDarwinObject(T aPtr
) {
60 #if !__has_feature(objc_arc)
66 class DarwinObjectPtr
{
68 DarwinObjectPtr() : mPtr(nullptr) {}
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
) {
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
)) {
93 RetainDarwinObject(mPtr
);
97 DarwinObjectPtr
& operator=(const DarwinObjectPtr
& aOther
) {
98 DarwinObjectPtr ptr
= aOther
;
103 DarwinObjectPtr
& operator=(DarwinObjectPtr
&& aOther
) {
104 DarwinObjectPtr ptr
= std::move(aOther
);
109 DarwinObjectPtr
& operator=(std::nullptr_t
) {
111 ReleaseDarwinObject(mPtr
);
117 DarwinObjectPtr
& operator=(T aOther
) {
118 DarwinObjectPtr ptr
= std::move(aOther
);
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
);
130 struct AdoptDarwinObjectTag
{};
131 DarwinObjectPtr(AdoptDarwinObjectTag
, T aPtr
) : mPtr(std::move(aPtr
)) {}
136 template <typename T
>
137 inline DarwinObjectPtr
<T
> AdoptDarwinObject(T aPtr
) {
138 return DarwinObjectPtr
<T
>{typename DarwinObjectPtr
<T
>::AdoptDarwinObjectTag
{},
142 } // namespace mozilla