Chromium Code Reviews| Index: base/mac/scoped_cftyperef.h |
| =================================================================== |
| --- base/mac/scoped_cftyperef.h (revision 105357) |
| +++ base/mac/scoped_cftyperef.h (working copy) |
| @@ -14,6 +14,15 @@ |
| namespace base { |
| namespace mac { |
| +// This class wraps the CoreFoundation routine CFRelease() in a class |
| +// that can be passed as a template argument to ScopedCFTypeRef below. |
| +class ScopedCFTypeRefRelease { |
| + public: |
| + inline void operator()(CFTypeRef x) const { |
| + CFRelease(x); |
| + } |
| +}; |
| + |
| // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership |
| // of a CoreFoundation object: any object that can be represented as a |
| // CFTypeRef. Style deviations here are solely for compatibility with |
| @@ -24,7 +33,12 @@ |
| // caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes |
| // an ownership claim to that object. ScopedCFTypeRef<> does not call |
| // CFRetain(). |
| -template<typename CFT> |
| +// |
| +// The second template argument can be used to specify a functor used |
| +// to free the object. This allows this class to manage the lifetime |
| +// of objects similar to Core Foundation types, where the pointer-ness |
| +// is embedded in the type name) but with specialized releasing routines. |
|
Mark Mentovai
2011/10/13 20:20:48
The ) doesn’t close any (.
|
| +template<typename CFT, class FreeProc = ScopedCFTypeRefRelease> |
| class ScopedCFTypeRef { |
| public: |
| typedef CFT element_type; |
| @@ -34,13 +48,17 @@ |
| } |
| ~ScopedCFTypeRef() { |
| - if (object_) |
| - CFRelease(object_); |
| + if (object_) { |
| + FreeProc free_proc; |
| + free_proc(object_); |
| + } |
| } |
| void reset(CFT object = NULL) { |
| - if (object_) |
| - CFRelease(object_); |
| + if (object_) { |
| + FreeProc free_proc; |
| + free_proc(object_); |
| + } |
| object_ = object; |
| } |