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