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