OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef BASE_SCOPED_CFTYPEREF_H_ | |
6 #define BASE_SCOPED_CFTYPEREF_H_ | |
7 #pragma once | |
8 | |
9 #include <CoreFoundation/CoreFoundation.h> | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 | |
13 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership | |
14 // of a CoreFoundation object: any object that can be represented as a | |
15 // CFTypeRef. Style deviations here are solely for compatibility with | |
16 // scoped_ptr<>'s interface, with which everyone is already familiar. | |
17 // | |
18 // When scoped_cftyperef<> takes ownership of an object (in the constructor or | |
19 // in reset()), it takes over the caller's existing ownership claim. The | |
20 // caller must own the object it gives to scoped_cftyperef<>, and relinquishes | |
21 // an ownership claim to that object. scoped_cftyperef<> does not call | |
22 // CFRetain(). | |
23 template<typename CFT> | |
24 class scoped_cftyperef { | |
25 public: | |
26 typedef CFT element_type; | |
27 | |
28 explicit scoped_cftyperef(CFT object = NULL) | |
29 : object_(object) { | |
30 } | |
31 | |
32 ~scoped_cftyperef() { | |
33 if (object_) | |
34 CFRelease(object_); | |
35 } | |
36 | |
37 void reset(CFT object = NULL) { | |
38 if (object_) | |
39 CFRelease(object_); | |
40 object_ = object; | |
41 } | |
42 | |
43 bool operator==(CFT that) const { | |
44 return object_ == that; | |
45 } | |
46 | |
47 bool operator!=(CFT that) const { | |
48 return object_ != that; | |
49 } | |
50 | |
51 operator CFT() const { | |
52 return object_; | |
53 } | |
54 | |
55 CFT get() const { | |
56 return object_; | |
57 } | |
58 | |
59 void swap(scoped_cftyperef& that) { | |
60 CFT temp = that.object_; | |
61 that.object_ = object_; | |
62 object_ = temp; | |
63 } | |
64 | |
65 // scoped_cftyperef<>::release() is like scoped_ptr<>::release. It is NOT | |
66 // a wrapper for CFRelease(). To force a scoped_cftyperef<> object to call | |
67 // CFRelease(), use scoped_cftyperef<>::reset(). | |
68 CFT release() WARN_UNUSED_RESULT { | |
69 CFT temp = object_; | |
70 object_ = NULL; | |
71 return temp; | |
72 } | |
73 | |
74 private: | |
75 CFT object_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); | |
78 }; | |
79 | |
80 #endif // BASE_SCOPED_CFTYPEREF_H_ | |
OLD | NEW |