OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_NSOBJECT_H_ | 5 #ifndef BASE_SCOPED_NSOBJECT_H_ |
6 #define BASE_SCOPED_NSOBJECT_H_ | 6 #define BASE_SCOPED_NSOBJECT_H_ |
7 | 7 |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 | 10 |
11 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership | 11 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership |
12 // of an NSObject subclass object. Style deviations here are solely for | 12 // of an NSObject subclass object. Style deviations here are solely for |
13 // compatibility with scoped_ptr<>'s interface, with which everyone is already | 13 // compatibility with scoped_ptr<>'s interface, with which everyone is already |
14 // familiar. | 14 // familiar. |
15 // | 15 // |
16 // When scoped_nsobject<> takes ownership of an object (in the constructor or | 16 // When scoped_nsobject<> takes ownership of an object (in the constructor or |
17 // in reset()), it takes over the caller's existing ownership claim. The | 17 // in reset()), it takes over the caller's existing ownership claim. The |
18 // caller must own the object it gives to scoped_nsobject<>, and relinquishes | 18 // caller must own the object it gives to scoped_nsobject<>, and relinquishes |
19 // an ownership claim to that object. scoped_nsobject<> does not call | 19 // an ownership claim to that object. scoped_nsobject<> does not call |
20 // -retain. | 20 // -retain. |
| 21 // |
| 22 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For |
| 23 // NSAutoreleasePools use ScopedNSAutoreleasePool from |
| 24 // scoped_nsautorelease_pool.h instead. |
| 25 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile |
| 26 // time with a template specialization (see below). |
21 template<typename NST> | 27 template<typename NST> |
22 class scoped_nsobject { | 28 class scoped_nsobject { |
23 public: | 29 public: |
24 typedef NST* element_type; | 30 typedef NST* element_type; |
25 | 31 |
26 explicit scoped_nsobject(NST* object = nil) | 32 explicit scoped_nsobject(NST* object = nil) |
27 : object_(object) { | 33 : object_(object) { |
28 } | 34 } |
29 | 35 |
30 ~scoped_nsobject() { | 36 ~scoped_nsobject() { |
31 [object_ release]; | 37 [object_ release]; |
32 } | 38 } |
33 | 39 |
34 void reset(NST* object = nil) { | 40 void reset(NST* object = nil) { |
| 41 // We intentionally do not check that object != object_ as the caller must |
| 42 // already have an ownership claim over whatever it gives to scoped_nsobject |
| 43 // and scoped_cftyperef, whether it's in the constructor or in a call to |
| 44 // reset(). In either case, it relinquishes that claim and the scoper |
| 45 // assumes it. |
35 [object_ release]; | 46 [object_ release]; |
36 object_ = object; | 47 object_ = object; |
37 } | 48 } |
38 | 49 |
39 bool operator==(NST* that) const { | 50 bool operator==(NST* that) const { |
40 return object_ == that; | 51 return object_ == that; |
41 } | 52 } |
42 | 53 |
43 bool operator!=(NST* that) const { | 54 bool operator!=(NST* that) const { |
44 return object_ != that; | 55 return object_ != that; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 92 |
82 explicit scoped_nsobject(id object = nil) | 93 explicit scoped_nsobject(id object = nil) |
83 : object_(object) { | 94 : object_(object) { |
84 } | 95 } |
85 | 96 |
86 ~scoped_nsobject() { | 97 ~scoped_nsobject() { |
87 [object_ release]; | 98 [object_ release]; |
88 } | 99 } |
89 | 100 |
90 void reset(id object = nil) { | 101 void reset(id object = nil) { |
| 102 // We intentionally do not check that object != object_ as the caller must |
| 103 // already have an ownership claim over whatever it gives to scoped_nsobject |
| 104 // and scoped_cftyperef, whether it's in the constructor or in a call to |
| 105 // reset(). In either case, it relinquishes that claim and the scoper |
| 106 // assumes it. |
91 [object_ release]; | 107 [object_ release]; |
92 object_ = object; | 108 object_ = object; |
93 } | 109 } |
94 | 110 |
95 bool operator==(id that) const { | 111 bool operator==(id that) const { |
96 return object_ == that; | 112 return object_ == that; |
97 } | 113 } |
98 | 114 |
99 bool operator!=(id that) const { | 115 bool operator!=(id that) const { |
100 return object_ != that; | 116 return object_ != that; |
(...skipping 21 matching lines...) Expand all Loading... |
122 object_ = nil; | 138 object_ = nil; |
123 return temp; | 139 return temp; |
124 } | 140 } |
125 | 141 |
126 private: | 142 private: |
127 id object_; | 143 id object_; |
128 | 144 |
129 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 145 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
130 }; | 146 }; |
131 | 147 |
| 148 // Do not use scoped_nsobject for NSAutoreleasePools, use |
| 149 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
| 150 // at top of header. |
| 151 template<> |
| 152 class scoped_nsobject<NSAutoreleasePool> { |
| 153 private: |
| 154 explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
| 155 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
| 156 }; |
| 157 |
132 #endif // BASE_SCOPED_NSOBJECT_H_ | 158 #endif // BASE_SCOPED_NSOBJECT_H_ |
OLD | NEW |