blob: 0b4cb8cbcd85a956e4da924fbd0d79e0dd0eed1e [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
avi@google.com0edcae82008-08-23 04:55:26 +09005#ifndef BASE_SCOPED_CFTYPEREF_H_
6#define BASE_SCOPED_CFTYPEREF_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
8#include <CoreFoundation/CoreFoundation.h>
avi@google.com0edcae82008-08-23 04:55:26 +09009#include "base/basictypes.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010
11// scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership
12// of a CoreFoundation object: any object that can be represented as a
13// CFTypeRef. Style deviations here are solely for compatibility with
14// scoped_ptr<>'s interface, with which everyone is already familiar.
15template<typename CFT>
16class scoped_cftyperef {
17 public:
18 typedef CFT element_type;
19
20 explicit scoped_cftyperef(CFT object = NULL)
21 : object_(object) {
22 }
23
24 ~scoped_cftyperef() {
25 if (object_)
26 CFRelease(object_);
27 }
28
29 void reset(CFT object = NULL) {
30 if (object_ && object_ != object) {
31 CFRelease(object_);
32 object_ = object;
33 }
34 }
35
36 bool operator==(CFT that) const {
37 return object_ == that;
38 }
39
40 bool operator!=(CFT that) const {
41 return object_ != that;
42 }
43
44 operator CFT() const {
45 return object_;
46 }
47
48 CFT get() const {
49 return object_;
50 }
51
52 void swap(scoped_cftyperef& that) {
53 CFT temp = that.object_;
54 that.object_ = object_;
55 object_ = temp;
56 }
57
58 // scoped_cftyperef<>::release() is like scoped_ptr<>::release. It is NOT
59 // a wrapper for CFRelease(). To force a scoped_cftyperef<> object to call
60 // CFRelease(), use scoped_cftyperef<>::reset().
61 CFT release() {
62 CFT temp = object_;
63 object_ = NULL;
64 return temp;
65 }
66
67 private:
68 CFT object_;
69
avi@google.com0edcae82008-08-23 04:55:26 +090070 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef);
initial.commit3f4a7322008-07-27 06:49:38 +090071};
72
avi@google.com0edcae82008-08-23 04:55:26 +090073#endif // BASE_SCOPED_CFTYPEREF_H_
license.botf003cfe2008-08-24 09:55:55 +090074