1 //===-- CFUtils.h -----------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Created by Greg Clayton on 3/5/07.
11 //===----------------------------------------------------------------------===//
16 #include <CoreFoundation/CoreFoundation.h>
20 // Templatized CF helper class that can own any CF pointer and will
21 // call CFRelease() on any valid pointer it owns unless that pointer is
22 // explicitly released using the release() member function.
23 template <class T> class CFReleaser {
25 // Type names for the avlue
26 typedef T element_type;
28 // Constructors and destructors
29 CFReleaser(T ptr = NULL) : _ptr(ptr) {}
30 CFReleaser(const CFReleaser ©) : _ptr(copy.get()) {
34 virtual ~CFReleaser() { reset(); }
37 CFReleaser &operator=(const CFReleaser<T> ©) {
39 // Replace our owned pointer with the new one
41 // Retain the current pointer that we own
46 // Get the address of the contained type
47 T *ptr_address() { return &_ptr; }
49 // Access the pointer itself
50 const T get() const { return _ptr; }
51 T get() { return _ptr; }
53 // Set a new value for the pointer and CFRelease our old
54 // value if we had a valid one.
55 void reset(T ptr = NULL) {
63 // Release ownership without calling CFRelease
74 #endif // #ifdef __cplusplus
75 #endif // #ifndef __CFUtils_h__