Hash :
d4083c79
Author :
Date :
2025-01-31T15:06:36
Metal: Make ObjCPtr available to all modules Move ObjCPtr implementation to src/common/ObjCPtr.h, angle::ObjCPtr so that it's available also outside libANGLE. Bug: angleproject:393263506 Change-Id: I8ecd5632c7ae33ef4b409fc820fad229e91bc0ab Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6219318 Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
//
// Copyright 2025 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ObjCPtr.h:
// Implements smart pointer for Objective-C objects
#ifndef COMMON_APPLE_OBJCPTR_H_
#define COMMON_APPLE_OBJCPTR_H_
#ifndef __OBJC__
# error For Objective-C++ only.
#endif
#import <Foundation/Foundation.h>
#import <type_traits>
#import <utility>
#import "common/platform.h"
namespace angle
{
// Smart pointer for holding Objective-C objects. Use adoptObjCPtr for create functions
// that return owned reference, e.g. functions that begin with 'new', 'copy', 'create'.
template <typename T>
class ObjCPtr
{
public:
using PtrType = std::remove_pointer_t<T> *;
constexpr ObjCPtr() = default;
constexpr ObjCPtr(std::nullptr_t other) {}
ObjCPtr(PtrType other);
ObjCPtr(const ObjCPtr &other);
template <typename U>
constexpr ObjCPtr(ObjCPtr<U> &&other);
~ObjCPtr();
ObjCPtr &operator=(const ObjCPtr &other);
ObjCPtr &operator=(PtrType other);
template <typename U>
constexpr ObjCPtr &operator=(ObjCPtr<U> &&other);
[[nodiscard]] constexpr PtrType leakObject();
void reset();
constexpr explicit operator bool() const { return get(); }
constexpr operator PtrType() const { return get(); }
constexpr PtrType get() const { return mObject; }
constexpr void swap(ObjCPtr<T> &other);
template <typename U>
friend ObjCPtr<std::remove_pointer_t<U>> adoptObjCPtr(U NS_RELEASES_ARGUMENT);
private:
struct AdoptTag
{};
constexpr ObjCPtr(PtrType other, AdoptTag);
PtrType mObject = nil;
};
template <typename T>
ObjCPtr(T) -> ObjCPtr<std::remove_pointer_t<T>>;
template <typename T>
ObjCPtr<T>::ObjCPtr(PtrType other) : mObject(other)
{
#if !__has_feature(objc_arc)
[mObject retain];
#endif
}
template <typename T>
ObjCPtr<T>::ObjCPtr(const ObjCPtr &other) : ObjCPtr(other.mObject)
{}
template <typename T>
template <typename U>
constexpr ObjCPtr<T>::ObjCPtr(ObjCPtr<U> &&other) : mObject(other.leakObject())
{}
template <typename T>
ObjCPtr<T>::~ObjCPtr()
{
#if !__has_feature(objc_arc)
[mObject release];
#endif
}
template <typename T>
ObjCPtr<T> &ObjCPtr<T>::operator=(const ObjCPtr &other)
{
ObjCPtr temp = other;
swap(temp);
return *this;
}
template <typename T>
template <typename U>
constexpr ObjCPtr<T> &ObjCPtr<T>::operator=(ObjCPtr<U> &&other)
{
ObjCPtr temp = std::move(other);
swap(temp);
return *this;
}
template <typename T>
ObjCPtr<T> &ObjCPtr<T>::operator=(PtrType other)
{
ObjCPtr temp = other;
swap(temp);
return *this;
}
template <typename T>
constexpr ObjCPtr<T>::ObjCPtr(PtrType other, AdoptTag) : mObject(other)
{}
template <typename T>
constexpr void ObjCPtr<T>::swap(ObjCPtr<T> &other)
{
// std::swap is constexpr only in c++20.
auto object = other.mObject;
other.mObject = mObject;
mObject = object;
}
template <typename T>
constexpr typename ObjCPtr<T>::PtrType ObjCPtr<T>::leakObject()
{
// std::exchange is constexper only in c++20.
auto object = mObject;
mObject = nullptr;
return object;
}
template <typename T>
void ObjCPtr<T>::reset()
{
*this = {};
}
template <typename T, typename U>
constexpr bool operator==(const ObjCPtr<T> &a, const ObjCPtr<U> &b)
{
return a.get() == b.get();
}
template <typename T, typename U>
constexpr bool operator==(const ObjCPtr<T> &a, U *b)
{
return a.get() == b;
}
template <typename T, typename U>
constexpr bool operator==(T *a, const ObjCPtr<U> &b)
{
return a == b.get();
}
template <typename U>
ObjCPtr<std::remove_pointer_t<U>> adoptObjCPtr(U NS_RELEASES_ARGUMENT other)
{
using ResultType = ObjCPtr<std::remove_pointer_t<U>>;
return ResultType(other, typename ResultType::AdoptTag{});
}
} // namespace angle
#endif