1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkCFObject_DEFINED
9 #define SkCFObject_DEFINED
10
11 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
12
13 #import <CoreFoundation/CoreFoundation.h>
14
15 /**
16 * Wrapper class for managing lifetime of CoreFoundation objects. It will call
17 * CFRetain and CFRelease appropriately on creation, assignment, and deletion.
18 * Based on sk_sp<>.
19 */
SkCFSafeRetain(T obj)20 template <typename T> static inline T SkCFSafeRetain(T obj) {
21 if (obj) {
22 CFRetain(obj);
23 }
24 return obj;
25 }
26
SkCFSafeRelease(T obj)27 template <typename T> static inline void SkCFSafeRelease(T obj) {
28 if (obj) {
29 CFRelease(obj);
30 }
31 }
32
33 template <typename T> class sk_cfp {
34 public:
35 using element_type = T;
36
sk_cfp()37 constexpr sk_cfp() {}
sk_cfp(std::nullptr_t)38 constexpr sk_cfp(std::nullptr_t) {}
39
40 /**
41 * Shares the underlying object by calling CFRetain(), so that both the argument and the newly
42 * created sk_cfp both have a reference to it.
43 */
sk_cfp(const sk_cfp<T> & that)44 sk_cfp(const sk_cfp<T>& that) : fObject(SkCFSafeRetain(that.get())) {}
45
46 /**
47 * Move the underlying object from the argument to the newly created sk_cfp. Afterwards only
48 * the new sk_cfp will have a reference to the object, and the argument will point to null.
49 * No call to CFRetain() or CFRelease() will be made.
50 */
sk_cfp(sk_cfp<T> && that)51 sk_cfp(sk_cfp<T>&& that) : fObject(that.release()) {}
52
53 /**
54 * Adopt the bare object into the newly created sk_cfp.
55 * No call to CFRetain() or CFRelease() will be made.
56 */
sk_cfp(T obj)57 explicit sk_cfp(T obj) {
58 fObject = obj;
59 }
60
61 /**
62 * Calls CFRelease() on the underlying object pointer.
63 */
~sk_cfp()64 ~sk_cfp() {
65 SkCFSafeRelease(fObject);
66 SkDEBUGCODE(fObject = nil);
67 }
68
69 sk_cfp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
70
71 /**
72 * Shares the underlying object referenced by the argument by calling CFRetain() on it. If this
73 * sk_cfp previously had a reference to an object (i.e. not null) it will call CFRelease()
74 * on that object.
75 */
76 sk_cfp<T>& operator=(const sk_cfp<T>& that) {
77 if (this != &that) {
78 this->reset(SkCFSafeRetain(that.get()));
79 }
80 return *this;
81 }
82
83 /**
84 * Move the underlying object from the argument to the sk_cfp. If the sk_cfp
85 * previously held a reference to another object, CFRelease() will be called on that object.
86 * No call to CFRetain() will be made.
87 */
88 sk_cfp<T>& operator=(sk_cfp<T>&& that) {
89 this->reset(that.release());
90 return *this;
91 }
92
93 explicit operator bool() const { return this->get() != nil; }
94
get()95 T get() const { return fObject; }
96 T operator*() const {
97 SkASSERT(fObject);
98 return fObject;
99 }
100
101 /**
102 * Adopt the new object, and call CFRelease() on any previously held object (if not null).
103 * No call to CFRetain() will be made.
104 */
105 void reset(T object = nil) {
106 // Need to unref after assigning, see
107 // http://wg21.cmeerw.net/lwg/issue998
108 // http://wg21.cmeerw.net/lwg/issue2262
109 T oldObject = fObject;
110 fObject = object;
111 SkCFSafeRelease(oldObject);
112 }
113
114 /**
115 * Shares the new object by calling CFRetain() on it. If this sk_cfp previously had a
116 * reference to an object (i.e. not null) it will call CFRelease() on that object.
117 */
retain(T object)118 void retain(T object) {
119 if (fObject != object) {
120 this->reset(SkCFSafeRetain(object));
121 }
122 }
123
124 /**
125 * Return the original object, and set the internal object to nullptr.
126 * The caller must assume ownership of the object, and manage its reference count directly.
127 * No call to CFRelease() will be made.
128 */
release()129 T SK_WARN_UNUSED_RESULT release() {
130 T obj = fObject;
131 fObject = nil;
132 return obj;
133 }
134
135 private:
136 T fObject = nil;
137 };
138
139 template <typename T> inline bool operator==(const sk_cfp<T>& a,
140 const sk_cfp<T>& b) {
141 return a.get() == b.get();
142 }
143 template <typename T> inline bool operator==(const sk_cfp<T>& a,
144 std::nullptr_t) {
145 return !a;
146 }
147 template <typename T> inline bool operator==(std::nullptr_t,
148 const sk_cfp<T>& b) {
149 return !b;
150 }
151
152 template <typename T> inline bool operator!=(const sk_cfp<T>& a,
153 const sk_cfp<T>& b) {
154 return a.get() != b.get();
155 }
156 template <typename T> inline bool operator!=(const sk_cfp<T>& a,
157 std::nullptr_t) {
158 return static_cast<bool>(a);
159 }
160 template <typename T> inline bool operator!=(std::nullptr_t,
161 const sk_cfp<T>& b) {
162 return static_cast<bool>(b);
163 }
164
165 /*
166 * Returns a sk_cfp wrapping the provided object AND calls retain on it (if not null).
167 *
168 * This is different than the semantics of the constructor for sk_cfp, which just wraps the
169 * object, effectively "adopting" it.
170 */
sk_ret_cfp(T obj)171 template <typename T> sk_cfp<T> sk_ret_cfp(T obj) {
172 return sk_cfp<T>(SkCFSafeRetain(obj));
173 }
174
175 // For Flutter.
176 // TODO: migrate them away from this and remove
177 template <typename T> using sk_cf_obj = sk_cfp<T>;
178
179 #endif // SK_BUILD_FOR_MAC || SK_BUILD_FOR_IOS
180 #endif // SkCFObject_DEFINED
181