1 //===-- CFCReleaser.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 #ifndef CoreFoundationCPP_CFReleaser_h_
11 #define CoreFoundationCPP_CFReleaser_h_
13 #include <CoreFoundation/CoreFoundation.h>
19 //----------------------------------------------------------------------
20 // Templatized CF helper class that can own any CF pointer and will
21 // call CFRelease() on any valid pointer it owns unless that pointer is
22 // explicitly released using the release() member function. This class
23 // is designed to mimic the std::auto_ptr<T> class and has all of the
24 // same functions. The one thing to watch out for is the
25 // CFCReleaser<T>::release() function won't actually CFRelease any owned
26 // pointer, it is designed to relinquish ownership of the pointer just
27 // like std:auto_ptr<T>::release() does.
28 //----------------------------------------------------------------------
33 //----------------------------------------------------------
34 // Constructor that takes a pointer to a CF object that is
35 // to be released when this object goes out of scope
36 //----------------------------------------------------------
37 CFCReleaser(T ptr = NULL) :
42 //----------------------------------------------------------
45 // Note that copying a CFCReleaser will not transfer
46 // ownership of the contained pointer, but it will bump its
47 // reference count. This is where this class differs from
49 //----------------------------------------------------------
50 CFCReleaser(const CFCReleaser& rhs) :
58 //----------------------------------------------------------
59 // The destructor will release the pointer that it contains
60 // if it has a valid pointer.
61 //----------------------------------------------------------
62 virtual ~CFCReleaser()
67 //----------------------------------------------------------
68 // Assignment operator.
70 // Note that assigning one CFCReleaser to another will
71 // not transfer ownership of the contained pointer, but it
72 // will bump its reference count. This is where this class
73 // differs from std::auto_ptr.
74 //----------------------------------------------------------
76 operator= (const CFCReleaser<T>& rhs)
80 // Replace our owned pointer with the new one
82 // Retain the current pointer that we own
89 //----------------------------------------------------------
90 // Get the address of the contained type in case it needs
91 // to be passed to a function that will fill in a pointer
92 // value. The function currently will assert if _ptr is not
93 // NULL because the only time this method should be used is
94 // if another function will modify the contents, and we
95 // could leak a pointer if this is not NULL. If the
96 // assertion fires, check the offending code, or call
97 // reset() prior to using the "ptr_address()" member to make
98 // sure any owned objects has CFRelease called on it.
99 // I had to add the "enforce_null" bool here because some
100 // API's require the pointer address even though they don't change it.
101 //----------------------------------------------------------
103 ptr_address(bool enforce_null = true)
106 assert (_ptr == NULL);
110 //----------------------------------------------------------
111 // Access the pointer itself
112 //----------------------------------------------------------
126 //----------------------------------------------------------
127 // Set a new value for the pointer and CFRelease our old
128 // value if we had a valid one.
129 //----------------------------------------------------------
133 if ((_ptr != NULL) && (ptr != _ptr))
138 //----------------------------------------------------------
139 // Release ownership without calling CFRelease. This class
140 // is designed to mimic std::auto_ptr<T>, so the release
141 // method releases ownership of the contained pointer
142 // and does NOT call CFRelease.
143 //----------------------------------------------------------
156 #endif // #ifdef __cplusplus
157 #endif // #ifndef CoreFoundationCPP_CFReleaser_h_