107 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H10#define LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H11 12#include <CoreFoundation/CoreFoundation.h>13 14#include <cassert>15 16// Templatized CF helper class that can own any CF pointer and will17// call CFRelease() on any valid pointer it owns unless that pointer is18// explicitly released using the release() member function. This class19// is designed to mimic the std::auto_ptr<T> class and has all of the20// same functions. The one thing to watch out for is the21// CFCReleaser<T>::release() function won't actually CFRelease any owned22// pointer, it is designed to relinquish ownership of the pointer just23// like std:auto_ptr<T>::release() does.24template <class T> class CFCReleaser {25public:26 // Constructor that takes a pointer to a CF object that is27 // to be released when this object goes out of scope28 CFCReleaser(T ptr = NULL) : _ptr(ptr) {}29 30 // Copy constructor31 //32 // Note that copying a CFCReleaser will not transfer33 // ownership of the contained pointer, but it will bump its34 // reference count. This is where this class differs from35 // 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 contains42 // if it has a valid pointer.43 virtual ~CFCReleaser() { reset(); }44 45 // Assignment operator.46 //47 // Note that assigning one CFCReleaser to another will48 // not transfer ownership of the contained pointer, but it49 // will bump its reference count. This is where this class50 // differs from std::auto_ptr.51 CFCReleaser &operator=(const CFCReleaser<T> &rhs) {52 if (this != &rhs) {53 // Replace our owned pointer with the new one54 reset(rhs.get());55 // Retain the current pointer that we own56 if (get())57 ::CFRetain(get());58 }59 return *this;60 }61 62 // Get the address of the contained type in case it needs63 // to be passed to a function that will fill in a pointer64 // value. The function currently will assert if _ptr is not65 // NULL because the only time this method should be used is66 // if another function will modify the contents, and we67 // could leak a pointer if this is not NULL. If the68 // assertion fires, check the offending code, or call69 // reset() prior to using the "ptr_address()" member to make70 // sure any owned objects has CFRelease called on it.71 // I had to add the "enforce_null" bool here because some72 // 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 itself80 T get() { return _ptr; }81 82 const T get() const { return _ptr; }83 84 // Set a new value for the pointer and CFRelease our old85 // 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 class93 // is designed to mimic std::auto_ptr<T>, so the release94 // method releases ownership of the contained pointer95 // and does NOT call CFRelease.96 T release() {97 T tmp = _ptr;98 _ptr = NULL;99 return tmp;100 }101 102private:103 T _ptr;104};105 106#endif // LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H107