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: CFRef()17 CFRef() : instance_(nullptr) {} 18 CFRef(T instance)19 CFRef(T instance) : instance_(instance) {} 20 ~CFRef()21 ~CFRef() { 22 if (instance_ != nullptr) { 23 CFRelease(instance_); 24 } 25 instance_ = nullptr; 26 } 27 Reset(T instance)28 void Reset(T instance) { 29 if (instance_ == instance) { 30 return; 31 } 32 if (instance_ != nullptr) { 33 CFRelease(instance_); 34 } 35 36 instance_ = instance; 37 } 38 T()39 operator T() const { return instance_; } 40 41 operator bool() const { return instance_ != nullptr; } 42 43 private: 44 T instance_; 45 46 FML_DISALLOW_COPY_AND_ASSIGN(CFRef); 47 }; 48 49 } // namespace fml 50 51 #endif // FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_ 52