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_ == instance) {
45  return;
46  }
47  if (instance_ != nullptr) {
48  CFRelease(instance_);
49  }
50 
51  instance_ = instance;
52  }
53 
54  [[nodiscard]] T Release() {
55  auto instance = instance_;
56  instance_ = nullptr;
57  return instance;
58  }
59 
60  operator T() const { return instance_; }
61 
62  operator bool() const { return instance_ != nullptr; }
63 
64  private:
65  T instance_;
66 
67  CFRef& operator=(const CFRef&) = delete;
68 };
69 
70 } // namespace fml
71 
72 #endif // FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
CFRef & operator=(CFRef &&other)
Definition: cf_utils.h:31
T Release()
Definition: cf_utils.h:54
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