Port PluginObject fix downstream. See http://trac.webkit.org/changeset/61415/ for...
[chromium-blink-merge.git] / base / scoped_cftyperef.h
blob908be240af17f0e061a83eaf7f62ee576dc6c609
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
21 // CFRetain().
22 template<typename CFT>
23 class scoped_cftyperef {
24 public:
25 typedef CFT element_type;
27 explicit scoped_cftyperef(CFT object = NULL)
28 : object_(object) {
31 ~scoped_cftyperef() {
32 if (object_)
33 CFRelease(object_);
36 void reset(CFT object = NULL) {
37 if (object_)
38 CFRelease(object_);
39 object_ = object;
42 bool operator==(CFT that) const {
43 return object_ == that;
46 bool operator!=(CFT that) const {
47 return object_ != that;
50 operator CFT() const {
51 return object_;
54 CFT get() const {
55 return object_;
58 void swap(scoped_cftyperef& that) {
59 CFT temp = that.object_;
60 that.object_ = object_;
61 object_ = temp;
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 {
68 CFT temp = object_;
69 object_ = NULL;
70 return temp;
73 private:
74 CFT object_;
76 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef);
79 #endif // BASE_SCOPED_CFTYPEREF_H_