license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
avi@google.com | 0edcae8 | 2008-08-23 04:55:26 +0900 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_CFTYPEREF_H_ |
| 6 | #define BASE_SCOPED_CFTYPEREF_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
| 8 | #include <CoreFoundation/CoreFoundation.h> |
avi@google.com | 0edcae8 | 2008-08-23 04:55:26 +0900 | [diff] [blame] | 9 | #include "base/basictypes.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 10 | |
| 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. |
| 15 | template<typename CFT> |
| 16 | class 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.com | 0edcae8 | 2008-08-23 04:55:26 +0900 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 71 | }; |
| 72 | |
avi@google.com | 0edcae8 | 2008-08-23 04:55:26 +0900 | [diff] [blame] | 73 | #endif // BASE_SCOPED_CFTYPEREF_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 74 | |