2019-06-07 17:10:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkCFObject_DEFINED
|
|
|
|
#define SkCFObject_DEFINED
|
|
|
|
|
2021-10-05 18:22:02 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
|
|
|
|
#include <cstddef> // std::nullptr_t
|
2019-06-07 17:10:50 +00:00
|
|
|
|
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class for managing lifetime of CoreFoundation objects. It will call
|
|
|
|
* CFRetain and CFRelease appropriately on creation, assignment, and deletion.
|
|
|
|
* Based on sk_sp<>.
|
|
|
|
*/
|
|
|
|
template <typename T> static inline T SkCFSafeRetain(T obj) {
|
|
|
|
if (obj) {
|
|
|
|
CFRetain(obj);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> static inline void SkCFSafeRelease(T obj) {
|
|
|
|
if (obj) {
|
|
|
|
CFRelease(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> class sk_cfp {
|
2019-06-07 17:10:50 +00:00
|
|
|
public:
|
|
|
|
using element_type = T;
|
|
|
|
|
2021-02-22 14:41:27 +00:00
|
|
|
constexpr sk_cfp() {}
|
|
|
|
constexpr sk_cfp(std::nullptr_t) {}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shares the underlying object by calling CFRetain(), so that both the argument and the newly
|
2021-02-22 14:41:27 +00:00
|
|
|
* created sk_cfp both have a reference to it.
|
2019-06-07 17:10:50 +00:00
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
sk_cfp(const sk_cfp<T>& that) : fObject(SkCFSafeRetain(that.get())) {}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-22 14:41:27 +00:00
|
|
|
* Move the underlying object from the argument to the newly created sk_cfp. Afterwards only
|
|
|
|
* the new sk_cfp will have a reference to the object, and the argument will point to null.
|
2019-06-07 17:10:50 +00:00
|
|
|
* No call to CFRetain() or CFRelease() will be made.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
sk_cfp(sk_cfp<T>&& that) : fObject(that.release()) {}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-22 14:41:27 +00:00
|
|
|
* Adopt the bare object into the newly created sk_cfp.
|
2019-06-07 17:10:50 +00:00
|
|
|
* No call to CFRetain() or CFRelease() will be made.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
explicit sk_cfp(T obj) {
|
2019-06-07 17:10:50 +00:00
|
|
|
fObject = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls CFRelease() on the underlying object pointer.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
~sk_cfp() {
|
2019-06-07 17:10:50 +00:00
|
|
|
SkCFSafeRelease(fObject);
|
2020-11-12 20:21:11 +00:00
|
|
|
SkDEBUGCODE(fObject = nil);
|
2019-06-07 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:41:27 +00:00
|
|
|
sk_cfp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
|
2020-11-12 20:21:11 +00:00
|
|
|
|
2019-06-07 17:10:50 +00:00
|
|
|
/**
|
|
|
|
* Shares the underlying object referenced by the argument by calling CFRetain() on it. If this
|
2021-02-22 14:41:27 +00:00
|
|
|
* sk_cfp previously had a reference to an object (i.e. not null) it will call CFRelease()
|
2019-06-07 17:10:50 +00:00
|
|
|
* on that object.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
sk_cfp<T>& operator=(const sk_cfp<T>& that) {
|
2019-06-07 17:10:50 +00:00
|
|
|
if (this != &that) {
|
|
|
|
this->reset(SkCFSafeRetain(that.get()));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-22 14:41:27 +00:00
|
|
|
* Move the underlying object from the argument to the sk_cfp. If the sk_cfp
|
2019-06-07 17:10:50 +00:00
|
|
|
* previously held a reference to another object, CFRelease() will be called on that object.
|
|
|
|
* No call to CFRetain() will be made.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
sk_cfp<T>& operator=(sk_cfp<T>&& that) {
|
2019-06-07 17:10:50 +00:00
|
|
|
this->reset(that.release());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:21:11 +00:00
|
|
|
explicit operator bool() const { return this->get() != nil; }
|
|
|
|
|
2019-06-07 17:10:50 +00:00
|
|
|
T get() const { return fObject; }
|
2020-11-12 20:21:11 +00:00
|
|
|
T operator*() const {
|
|
|
|
SkASSERT(fObject);
|
|
|
|
return fObject;
|
|
|
|
}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adopt the new object, and call CFRelease() on any previously held object (if not null).
|
|
|
|
* No call to CFRetain() will be made.
|
|
|
|
*/
|
2020-11-12 20:21:11 +00:00
|
|
|
void reset(T object = nil) {
|
|
|
|
// Need to unref after assigning, see
|
|
|
|
// http://wg21.cmeerw.net/lwg/issue998
|
|
|
|
// http://wg21.cmeerw.net/lwg/issue2262
|
2019-06-07 17:10:50 +00:00
|
|
|
T oldObject = fObject;
|
|
|
|
fObject = object;
|
|
|
|
SkCFSafeRelease(oldObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-22 14:41:27 +00:00
|
|
|
* Shares the new object by calling CFRetain() on it. If this sk_cfp previously had a
|
2019-06-07 17:10:50 +00:00
|
|
|
* reference to an object (i.e. not null) it will call CFRelease() on that object.
|
|
|
|
*/
|
|
|
|
void retain(T object) {
|
2020-11-12 20:21:11 +00:00
|
|
|
if (fObject != object) {
|
2019-06-07 17:10:50 +00:00
|
|
|
this->reset(SkCFSafeRetain(object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the original object, and set the internal object to nullptr.
|
|
|
|
* The caller must assume ownership of the object, and manage its reference count directly.
|
|
|
|
* No call to CFRelease() will be made.
|
|
|
|
*/
|
|
|
|
T SK_WARN_UNUSED_RESULT release() {
|
|
|
|
T obj = fObject;
|
2020-11-12 20:21:11 +00:00
|
|
|
fObject = nil;
|
2019-06-07 17:10:50 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-11-12 20:21:11 +00:00
|
|
|
T fObject = nil;
|
2019-06-07 17:10:50 +00:00
|
|
|
};
|
|
|
|
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> inline bool operator==(const sk_cfp<T>& a,
|
|
|
|
const sk_cfp<T>& b) {
|
2019-06-07 17:10:50 +00:00
|
|
|
return a.get() == b.get();
|
|
|
|
}
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> inline bool operator==(const sk_cfp<T>& a,
|
2020-11-12 20:21:11 +00:00
|
|
|
std::nullptr_t) {
|
|
|
|
return !a;
|
|
|
|
}
|
|
|
|
template <typename T> inline bool operator==(std::nullptr_t,
|
2021-02-22 14:41:27 +00:00
|
|
|
const sk_cfp<T>& b) {
|
2020-11-12 20:21:11 +00:00
|
|
|
return !b;
|
|
|
|
}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> inline bool operator!=(const sk_cfp<T>& a,
|
|
|
|
const sk_cfp<T>& b) {
|
2019-06-07 17:10:50 +00:00
|
|
|
return a.get() != b.get();
|
|
|
|
}
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> inline bool operator!=(const sk_cfp<T>& a,
|
2020-11-12 20:21:11 +00:00
|
|
|
std::nullptr_t) {
|
|
|
|
return static_cast<bool>(a);
|
|
|
|
}
|
|
|
|
template <typename T> inline bool operator!=(std::nullptr_t,
|
2021-02-22 14:41:27 +00:00
|
|
|
const sk_cfp<T>& b) {
|
2020-11-12 20:21:11 +00:00
|
|
|
return static_cast<bool>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-02-22 14:41:27 +00:00
|
|
|
* Returns a sk_cfp wrapping the provided object AND calls retain on it (if not null).
|
2020-11-12 20:21:11 +00:00
|
|
|
*
|
2021-02-22 14:41:27 +00:00
|
|
|
* This is different than the semantics of the constructor for sk_cfp, which just wraps the
|
2020-11-12 20:21:11 +00:00
|
|
|
* object, effectively "adopting" it.
|
|
|
|
*/
|
2021-02-22 14:41:27 +00:00
|
|
|
template <typename T> sk_cfp<T> sk_ret_cfp(T obj) {
|
|
|
|
return sk_cfp<T>(SkCFSafeRetain(obj));
|
2020-11-12 20:21:11 +00:00
|
|
|
}
|
2019-06-07 17:10:50 +00:00
|
|
|
|
2021-10-05 18:22:02 +00:00
|
|
|
#endif // __APPLE__
|
2020-11-12 20:21:11 +00:00
|
|
|
#endif // SkCFObject_DEFINED
|