15 #ifndef SHAKA_EMBEDDED_UTIL_CF_REF_H_ 16 #define SHAKA_EMBEDDED_UTIL_CF_REF_H_ 20 # include <CoreFoundation/CoreFoundation.h> 23 # include <type_traits> 32 struct absorb_ref_t {};
33 extern absorb_ref_t absorb_ref;
34 struct acquire_ref_t {};
35 extern acquire_ref_t acquire_ref;
42 struct RefTypeTraits {
47 static constexpr
const bool AcquireWithRaw =
false;
53 static T Duplicate(T arg) {
62 static void Release(T arg) {
77 using traits = RefTypeTraits<T>;
80 CFRef() : ptr_(nullptr) {}
81 CFRef(std::nullptr_t) : ptr_(nullptr) {}
83 : ptr_(traits::AcquireWithRaw ? traits::Duplicate(arg) : arg) {}
84 CFRef(absorb_ref_t, T arg) : ptr_(arg) {}
85 CFRef(acquire_ref_t, T arg) : ptr_(traits::Duplicate(arg)) {}
86 CFRef(
const CFRef& other) : ptr_(traits::Duplicate(other.ptr_)) {}
87 CFRef(CFRef&& other) : ptr_(other.Detach()) {}
91 CFRef(
const CFRef<U>& other) : ptr_(traits::Duplicate(other.ptr_)) {}
93 CFRef(CFRef<U>&& other) : ptr_(other.Detach()) {}
98 CFRef& operator=(
const CFRef& other) {
100 ptr_ = traits::Duplicate(other);
103 CFRef& operator=(CFRef&& other) {
105 ptr_ = other.Detach();
108 CFRef& operator=(T other) {
110 ptr_ = traits::AcquireWithRaw ? traits::Duplicate(other) : other;
135 template <
typename U>
139 traits::Release(ptr_);
151 #endif // SHAKA_EMBEDDED_UTIL_CF_REF_H_