1061da546Spatrick //===-- CFCReleaser.h -------------------------------------------*- C++ -*-===// 2061da546Spatrick // 3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6061da546Spatrick // 7061da546Spatrick //===----------------------------------------------------------------------===// 8061da546Spatrick 9dda28197Spatrick #ifndef LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H 10dda28197Spatrick #define LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H 11061da546Spatrick 12061da546Spatrick #include <CoreFoundation/CoreFoundation.h> 13061da546Spatrick 14061da546Spatrick #ifdef __cplusplus 15061da546Spatrick 16*be691f3bSpatrick #include <cassert> 17061da546Spatrick 18061da546Spatrick // Templatized CF helper class that can own any CF pointer and will 19061da546Spatrick // call CFRelease() on any valid pointer it owns unless that pointer is 20061da546Spatrick // explicitly released using the release() member function. This class 21061da546Spatrick // is designed to mimic the std::auto_ptr<T> class and has all of the 22061da546Spatrick // same functions. The one thing to watch out for is the 23061da546Spatrick // CFCReleaser<T>::release() function won't actually CFRelease any owned 24061da546Spatrick // pointer, it is designed to relinquish ownership of the pointer just 25061da546Spatrick // like std:auto_ptr<T>::release() does. 26061da546Spatrick template <class T> class CFCReleaser { 27061da546Spatrick public: 28061da546Spatrick // Constructor that takes a pointer to a CF object that is 29061da546Spatrick // to be released when this object goes out of scope _ptr(ptr)30061da546Spatrick CFCReleaser(T ptr = NULL) : _ptr(ptr) {} 31061da546Spatrick 32061da546Spatrick // Copy constructor 33061da546Spatrick // 34061da546Spatrick // Note that copying a CFCReleaser will not transfer 35061da546Spatrick // ownership of the contained pointer, but it will bump its 36061da546Spatrick // reference count. This is where this class differs from 37061da546Spatrick // std::auto_ptr. CFCReleaser(const CFCReleaser & rhs)38061da546Spatrick CFCReleaser(const CFCReleaser &rhs) : _ptr(rhs.get()) { 39061da546Spatrick if (get()) 40061da546Spatrick ::CFRetain(get()); 41061da546Spatrick } 42061da546Spatrick 43061da546Spatrick // The destructor will release the pointer that it contains 44061da546Spatrick // if it has a valid pointer. ~CFCReleaser()45061da546Spatrick virtual ~CFCReleaser() { reset(); } 46061da546Spatrick 47061da546Spatrick // Assignment operator. 48061da546Spatrick // 49061da546Spatrick // Note that assigning one CFCReleaser to another will 50061da546Spatrick // not transfer ownership of the contained pointer, but it 51061da546Spatrick // will bump its reference count. This is where this class 52061da546Spatrick // differs from std::auto_ptr. 53061da546Spatrick CFCReleaser &operator=(const CFCReleaser<T> &rhs) { 54061da546Spatrick if (this != &rhs) { 55061da546Spatrick // Replace our owned pointer with the new one 56061da546Spatrick reset(rhs.get()); 57061da546Spatrick // Retain the current pointer that we own 58061da546Spatrick if (get()) 59061da546Spatrick ::CFRetain(get()); 60061da546Spatrick } 61061da546Spatrick return *this; 62061da546Spatrick } 63061da546Spatrick 64061da546Spatrick // Get the address of the contained type in case it needs 65061da546Spatrick // to be passed to a function that will fill in a pointer 66061da546Spatrick // value. The function currently will assert if _ptr is not 67061da546Spatrick // NULL because the only time this method should be used is 68061da546Spatrick // if another function will modify the contents, and we 69061da546Spatrick // could leak a pointer if this is not NULL. If the 70061da546Spatrick // assertion fires, check the offending code, or call 71061da546Spatrick // reset() prior to using the "ptr_address()" member to make 72061da546Spatrick // sure any owned objects has CFRelease called on it. 73061da546Spatrick // I had to add the "enforce_null" bool here because some 74061da546Spatrick // API's require the pointer address even though they don't change it. 75061da546Spatrick T *ptr_address(bool enforce_null = true) { 76061da546Spatrick if (enforce_null) 77061da546Spatrick assert(_ptr == NULL); 78061da546Spatrick return &_ptr; 79061da546Spatrick } 80061da546Spatrick 81061da546Spatrick // Access the pointer itself get()82061da546Spatrick T get() { return _ptr; } 83061da546Spatrick get()84061da546Spatrick const T get() const { return _ptr; } 85061da546Spatrick 86061da546Spatrick // Set a new value for the pointer and CFRelease our old 87061da546Spatrick // value if we had a valid one. 88061da546Spatrick void reset(T ptr = NULL) { 89061da546Spatrick if ((_ptr != NULL) && (ptr != _ptr)) 90061da546Spatrick ::CFRelease(_ptr); 91061da546Spatrick _ptr = ptr; 92061da546Spatrick } 93061da546Spatrick 94061da546Spatrick // Release ownership without calling CFRelease. This class 95061da546Spatrick // is designed to mimic std::auto_ptr<T>, so the release 96061da546Spatrick // method releases ownership of the contained pointer 97061da546Spatrick // and does NOT call CFRelease. release()98061da546Spatrick T release() { 99061da546Spatrick T tmp = _ptr; 100061da546Spatrick _ptr = NULL; 101061da546Spatrick return tmp; 102061da546Spatrick } 103061da546Spatrick 104061da546Spatrick private: 105061da546Spatrick T _ptr; 106061da546Spatrick }; 107061da546Spatrick 108061da546Spatrick #endif // #ifdef __cplusplus 109dda28197Spatrick #endif // LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H 110