Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 1 | //===-- CFUtils.h -----------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Created by Greg Clayton on 3/5/07. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef __CFUtils_h__ |
| 14 | #define __CFUtils_h__ |
| 15 | |
| 16 | #include <CoreFoundation/CoreFoundation.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | |
| 20 | //---------------------------------------------------------------------- |
| 21 | // Templatized CF helper class that can own any CF pointer and will |
| 22 | // call CFRelease() on any valid pointer it owns unless that pointer is |
| 23 | // explicitly released using the release() member function. |
| 24 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | template <class T> class CFReleaser { |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 26 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | // Type names for the avlue |
| 28 | typedef T element_type; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | // Constructors and destructors |
| 31 | CFReleaser(T ptr = NULL) : _ptr(ptr) {} |
| 32 | CFReleaser(const CFReleaser ©) : _ptr(copy.get()) { |
| 33 | if (get()) |
| 34 | ::CFRetain(get()); |
| 35 | } |
| 36 | virtual ~CFReleaser() { reset(); } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | // Assignments |
| 39 | CFReleaser &operator=(const CFReleaser<T> ©) { |
| 40 | if (copy != *this) { |
| 41 | // Replace our owned pointer with the new one |
| 42 | reset(copy.get()); |
| 43 | // Retain the current pointer that we own |
| 44 | if (get()) |
| 45 | ::CFRetain(get()); |
| 46 | } |
| 47 | } |
| 48 | // Get the address of the contained type |
| 49 | T *ptr_address() { return &_ptr; } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | // Access the pointer itself |
| 52 | const T get() const { return _ptr; } |
| 53 | T get() { return _ptr; } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | // Set a new value for the pointer and CFRelease our old |
| 56 | // value if we had a valid one. |
| 57 | void reset(T ptr = NULL) { |
| 58 | if (ptr != _ptr) { |
| 59 | if (_ptr != NULL) |
| 60 | ::CFRelease(_ptr); |
| 61 | _ptr = ptr; |
| 62 | } |
| 63 | } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | // Release ownership without calling CFRelease |
| 66 | T release() { |
| 67 | T tmp = _ptr; |
| 68 | _ptr = NULL; |
| 69 | return tmp; |
| 70 | } |
| 71 | |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 72 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | element_type _ptr; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | #endif // #ifdef __cplusplus |
| 77 | #endif // #ifndef __CFUtils_h__ |