1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SCOPED_CFTYPEREF_H_
6 #define BASE_SCOPED_CFTYPEREF_H_
8 #include <CoreFoundation/CoreFoundation.h>
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
12 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership
13 // of a CoreFoundation object: any object that can be represented as a
14 // CFTypeRef. Style deviations here are solely for compatibility with
15 // scoped_ptr<>'s interface, with which everyone is already familiar.
17 // When scoped_cftyperef<> takes ownership of an object (in the constructor or
18 // in reset()), it takes over the caller's existing ownership claim. The
19 // caller must own the object it gives to scoped_cftyperef<>, and relinquishes
20 // an ownership claim to that object. scoped_cftyperef<> does not call
22 template<typename CFT
>
23 class scoped_cftyperef
{
25 typedef CFT element_type
;
27 explicit scoped_cftyperef(CFT object
= NULL
)
36 void reset(CFT object
= NULL
) {
42 bool operator==(CFT that
) const {
43 return object_
== that
;
46 bool operator!=(CFT that
) const {
47 return object_
!= that
;
50 operator CFT() const {
58 void swap(scoped_cftyperef
& that
) {
59 CFT temp
= that
.object_
;
60 that
.object_
= object_
;
64 // scoped_cftyperef<>::release() is like scoped_ptr<>::release. It is NOT
65 // a wrapper for CFRelease(). To force a scoped_cftyperef<> object to call
66 // CFRelease(), use scoped_cftyperef<>::reset().
67 CFT
release() WARN_UNUSED_RESULT
{
76 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef
);
79 #endif // BASE_SCOPED_CFTYPEREF_H_