Flutter Engine
The Flutter Engine
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
14template <class T>
15class CFRef {
16 public:
17 CFRef() : instance_(nullptr) {}
18
19 // NOLINTNEXTLINE(google-explicit-constructor)
20 CFRef(T instance) : instance_(instance) {}
21
22 CFRef(const CFRef& other) : instance_(other.instance_) {
23 if (instance_) {
24 CFRetain(instance_);
25 }
26 }
27
28 CFRef(CFRef&& other) : instance_(other.instance_) {
29 other.instance_ = nullptr;
30 }
31
32 CFRef& operator=(CFRef&& other) {
33 Reset(other.Release());
34 return *this;
35 }
36
38 if (instance_ != nullptr) {
39 CFRelease(instance_);
40 }
41 instance_ = nullptr;
42 }
43
44 void Reset(T instance = nullptr) {
45 if (instance_ != nullptr) {
46 CFRelease(instance_);
47 }
48
49 instance_ = instance;
50 }
51
52 [[nodiscard]] T Release() {
53 auto instance = instance_;
54 instance_ = nullptr;
55 return instance;
56 }
57
58 // NOLINTNEXTLINE(google-explicit-constructor)
59 operator T() const { return instance_; }
60
61 explicit operator bool() const { return instance_ != nullptr; }
62
63 private:
64 T instance_;
65
66 CFRef& operator=(const CFRef&) = delete;
67};
68
69} // namespace fml
70
71#endif // FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
CFRef(T instance)
Definition: cf_utils.h:20
T Release()
Definition: cf_utils.h:52
CFRef(const CFRef &other)
Definition: cf_utils.h:22
CFRef(CFRef &&other)
Definition: cf_utils.h:28
CFRef & operator=(CFRef &&other)
Definition: cf_utils.h:32
void Reset(T instance=nullptr)
Definition: cf_utils.h:44
VkInstance instance
Definition: main.cc:48
Definition: ascii_trie.cc:9
#define T
Definition: precompiler.cc:65