1 //===-- CFUtils.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Created by Greg Clayton on 3/5/07.
12 //===----------------------------------------------------------------------===//
17 #include <CoreFoundation/CoreFoundation.h>
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 //----------------------------------------------------------------------
26 template <class T> class CFReleaser {
28 // Type names for the avlue
29 typedef T element_type;
31 // Constructors and destructors
32 CFReleaser(T ptr = NULL) : _ptr(ptr) {}
33 CFReleaser(const CFReleaser ©) : _ptr(copy.get()) {
37 virtual ~CFReleaser() { reset(); }
40 CFReleaser &operator=(const CFReleaser<T> ©) {
42 // Replace our owned pointer with the new one
44 // Retain the current pointer that we own
49 // Get the address of the contained type
50 T *ptr_address() { return &_ptr; }
52 // Access the pointer itself
53 const T get() const { return _ptr; }
54 T get() { return _ptr; }
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) {
66 // Release ownership without calling CFRelease
77 #endif // #ifdef __cplusplus
78 #endif // #ifndef __CFUtils_h__