1 //===-- CFCReleaser.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 #ifndef LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
10 #define LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
12 #include <CoreFoundation/CoreFoundation.h>
16 // Templatized CF helper class that can own any CF pointer and will
17 // call CFRelease() on any valid pointer it owns unless that pointer is
18 // explicitly released using the release() member function. This class
19 // is designed to mimic the std::auto_ptr<T> class and has all of the
20 // same functions. The one thing to watch out for is the
21 // CFCReleaser<T>::release() function won't actually CFRelease any owned
22 // pointer, it is designed to relinquish ownership of the pointer just
23 // like std:auto_ptr<T>::release() does.
24 template <class T
> class CFCReleaser
{
26 // Constructor that takes a pointer to a CF object that is
27 // to be released when this object goes out of scope
28 CFCReleaser(T ptr
= NULL
) : _ptr(ptr
) {}
32 // Note that copying a CFCReleaser will not transfer
33 // ownership of the contained pointer, but it will bump its
34 // reference count. This is where this class differs from
36 CFCReleaser(const CFCReleaser
&rhs
) : _ptr(rhs
.get()) {
41 // The destructor will release the pointer that it contains
42 // if it has a valid pointer.
43 virtual ~CFCReleaser() { reset(); }
45 // Assignment operator.
47 // Note that assigning one CFCReleaser to another will
48 // not transfer ownership of the contained pointer, but it
49 // will bump its reference count. This is where this class
50 // differs from std::auto_ptr.
51 CFCReleaser
&operator=(const CFCReleaser
<T
> &rhs
) {
53 // Replace our owned pointer with the new one
55 // Retain the current pointer that we own
62 // Get the address of the contained type in case it needs
63 // to be passed to a function that will fill in a pointer
64 // value. The function currently will assert if _ptr is not
65 // NULL because the only time this method should be used is
66 // if another function will modify the contents, and we
67 // could leak a pointer if this is not NULL. If the
68 // assertion fires, check the offending code, or call
69 // reset() prior to using the "ptr_address()" member to make
70 // sure any owned objects has CFRelease called on it.
71 // I had to add the "enforce_null" bool here because some
72 // API's require the pointer address even though they don't change it.
73 T
*ptr_address(bool enforce_null
= true) {
79 // Access the pointer itself
80 T
get() { return _ptr
; }
82 const T
get() const { return _ptr
; }
84 // Set a new value for the pointer and CFRelease our old
85 // value if we had a valid one.
86 void reset(T ptr
= NULL
) {
87 if ((_ptr
!= NULL
) && (ptr
!= _ptr
))
92 // Release ownership without calling CFRelease. This class
93 // is designed to mimic std::auto_ptr<T>, so the release
94 // method releases ownership of the contained pointer
95 // and does NOT call CFRelease.
106 #endif // LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H