OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SCOPED_CFTYPEREF_H_ | 5 #ifndef BASE_SCOPED_CFTYPEREF_H_ |
6 #define BASE_SCOPED_CFTYPEREF_H_ | 6 #define BASE_SCOPED_CFTYPEREF_H_ |
7 | 7 |
8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 | 10 |
11 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership | 11 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership |
12 // of a CoreFoundation object: any object that can be represented as a | 12 // of a CoreFoundation object: any object that can be represented as a |
13 // CFTypeRef. Style deviations here are solely for compatibility with | 13 // CFTypeRef. Style deviations here are solely for compatibility with |
14 // scoped_ptr<>'s interface, with which everyone is already familiar. | 14 // scoped_ptr<>'s interface, with which everyone is already familiar. |
15 template<typename CFT> | 15 template<typename CFT> |
16 class scoped_cftyperef { | 16 class scoped_cftyperef { |
17 public: | 17 public: |
18 typedef CFT element_type; | 18 typedef CFT element_type; |
19 | 19 |
20 explicit scoped_cftyperef(CFT object = NULL) | 20 explicit scoped_cftyperef(CFT object = NULL) |
21 : object_(object) { | 21 : object_(object) { |
22 } | 22 } |
23 | 23 |
24 ~scoped_cftyperef() { | 24 ~scoped_cftyperef() { |
25 if (object_) | 25 if (object_) |
26 CFRelease(object_); | 26 CFRelease(object_); |
27 } | 27 } |
28 | 28 |
29 void reset(CFT object = NULL) { | 29 void reset(CFT object = NULL) { |
30 if (object_ && object_ != object) { | 30 if (object_ != object) { |
31 CFRelease(object_); | 31 if (object_) |
| 32 CFRelease(object_); |
32 object_ = object; | 33 object_ = object; |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
36 bool operator==(CFT that) const { | 37 bool operator==(CFT that) const { |
37 return object_ == that; | 38 return object_ == that; |
38 } | 39 } |
39 | 40 |
40 bool operator!=(CFT that) const { | 41 bool operator!=(CFT that) const { |
41 return object_ != that; | 42 return object_ != that; |
(...skipping 22 matching lines...) Loading... |
64 return temp; | 65 return temp; |
65 } | 66 } |
66 | 67 |
67 private: | 68 private: |
68 CFT object_; | 69 CFT object_; |
69 | 70 |
70 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); | 71 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); |
71 }; | 72 }; |
72 | 73 |
73 #endif // BASE_SCOPED_CFTYPEREF_H_ | 74 #endif // BASE_SCOPED_CFTYPEREF_H_ |
74 | |
OLD | NEW |