LLDB mainline
CFCReleaser.h
Go to the documentation of this file.
1//===-- CFCReleaser.h -------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
10#define LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
11
12#include <CoreFoundation/CoreFoundation.h>
13
14#include <cassert>
15
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.
24template <class T> class CFCReleaser {
25public:
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) {}
29
30 // Copy constructor
31 //
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
35 // std::auto_ptr.
36 CFCReleaser(const CFCReleaser &rhs) : _ptr(rhs.get()) {
37 if (get())
38 ::CFRetain(get());
39 }
40
41 // The destructor will release the pointer that it contains
42 // if it has a valid pointer.
43 virtual ~CFCReleaser() { reset(); }
44
45 // Assignment operator.
46 //
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.
52 if (this != &rhs) {
53 // Replace our owned pointer with the new one
54 reset(rhs.get());
55 // Retain the current pointer that we own
56 if (get())
57 ::CFRetain(get());
58 }
59 return *this;
60 }
61
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) {
74 if (enforce_null)
75 assert(_ptr == NULL);
76 return &_ptr;
77 }
78
79 // Access the pointer itself
80 T get() { return _ptr; }
81
82 const T get() const { return _ptr; }
83
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))
88 ::CFRelease(_ptr);
89 _ptr = ptr;
90 }
91
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.
96 T release() {
97 T tmp = _ptr;
98 _ptr = NULL;
99 return tmp;
100 }
101
102private:
104};
105
106#endif // LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
CFCReleaser(T ptr=NULL)
Definition: CFCReleaser.h:28
void reset(T ptr=NULL)
Definition: CFCReleaser.h:86
virtual ~CFCReleaser()
Definition: CFCReleaser.h:43
T * ptr_address(bool enforce_null=true)
Definition: CFCReleaser.h:73
CFCReleaser & operator=(const CFCReleaser< T > &rhs)
Definition: CFCReleaser.h:51
CFCReleaser(const CFCReleaser &rhs)
Definition: CFCReleaser.h:36
const T get() const
Definition: CFCReleaser.h:82