Flutter Engine
 
Loading...
Searching...
No Matches
cf_utils.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
6#define FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
7
8#include <CoreFoundation/CoreFoundation.h>
9
10#include "flutter/fml/macros.h"
11
12namespace fml {
13
14/// Default retain and release implementations for CFRef.
15template <typename T>
17 static constexpr T kNullValue = nullptr;
18 static void Retain(T instance) { CFRetain(instance); }
19 static void Release(T instance) { CFRelease(instance); }
20};
21
22/// RAII-based smart pointer wrapper for CoreFoundation objects.
23///
24/// CFRef takes over ownership of the object it wraps and ensures that retain
25/// and release are called as appropriate on creation, assignment, and disposal.
26template <class T>
27class CFRef {
28 public:
29 /// Creates a new null CFRef.
30 CFRef() : instance_(CFRefTraits<T>::kNullValue) {}
31
32 /// Takes over ownership of `instance`, which is expected to be already
33 /// retained.
34 explicit CFRef(T instance) : instance_(instance) {}
35
36 /// Copy ctor: Creates a retained copy of the CoreFoundation object owned by
37 /// `other`.
38 CFRef(const CFRef& other) : instance_(other.instance_) {
39 if (instance_) {
40 CFRefTraits<T>::Retain(instance_);
41 }
42 }
43
44 /// Move ctor: Takes over ownership of the CoreFoundation object owned
45 /// by `other`. The object owned by `other` is set to null.
46 CFRef(CFRef&& other) : instance_(other.instance_) {
47 other.instance_ = CFRefTraits<T>::kNullValue;
48 }
49
50 /// Takes over ownership of the CoreFoundation object owned by `other`.
51 CFRef& operator=(CFRef&& other) {
52 Reset(other.Release());
53 return *this;
54 }
55
56 /// Releases the underlying CoreFoundation object, if non-null.
58 if (instance_) {
59 CFRefTraits<T>::Release(instance_);
60 }
62 }
63
64 /// Takes over ownership of `instance`, null by default. The object is
65 /// expected to be already retained if non-null.
66 ///
67 /// Releases the previous object, if non-null.
69 if (instance_) {
70 CFRefTraits<T>::Release(instance_);
71 }
72 instance_ = instance;
73 }
74
75 /// Retains a shared copy of `instance`. The previous object is released if
76 /// non-null. Has no effect if `instance` is the currently-held object.
78 if (instance_ == instance) {
79 return;
80 }
81 if (instance) {
83 }
85 }
86
87 /// Returns and transfers ownership of the underlying CoreFoundation object
88 /// to the caller. The caller is responsible for calling `CFRelease` when done
89 /// with the object.
90 [[nodiscard]] T Release() {
91 auto instance = instance_;
93 return instance;
94 }
95
96 /// Returns the underlying CoreFoundation object. Ownership of the returned
97 /// object follows The Get Rule.
98 ///
99 /// See:
100 /// https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-SW1
101 T Get() const { return instance_; }
102
103 /// Returns the underlying CoreFoundation object. Ownership of the returned
104 /// object follows The Get Rule.
105 ///
106 /// See:
107 /// https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-SW1
108 // NOLINTNEXTLINE(google-explicit-constructor)
109 operator T() const { return instance_; }
110
111 /// Returns true if the underlying CoreFoundation object is non-null.
112 explicit operator bool() const {
113 return instance_ != CFRefTraits<T>::kNullValue;
114 }
115
116 private:
117 T instance_;
118
119 CFRef& operator=(const CFRef&) = delete;
120};
121
122} // namespace fml
123
124#endif // FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
CFRef(T instance)
Definition cf_utils.h:34
T Release()
Definition cf_utils.h:90
CFRef(const CFRef &other)
Definition cf_utils.h:38
CFRef()
Creates a new null CFRef.
Definition cf_utils.h:30
void Retain(T instance=CFRefTraits< T >::kNullValue)
Definition cf_utils.h:77
~CFRef()
Releases the underlying CoreFoundation object, if non-null.
Definition cf_utils.h:57
CFRef(CFRef &&other)
Definition cf_utils.h:46
CFRef & operator=(CFRef &&other)
Takes over ownership of the CoreFoundation object owned by other.
Definition cf_utils.h:51
void Reset(T instance=CFRefTraits< T >::kNullValue)
Definition cf_utils.h:68
T Get() const
Definition cf_utils.h:101
VkInstance instance
Definition main.cc:64
Default retain and release implementations for CFRef.
Definition cf_utils.h:16
static void Retain(T instance)
Definition cf_utils.h:18
static constexpr T kNullValue
Definition cf_utils.h:17
static void Release(T instance)
Definition cf_utils.h:19