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. scoped_nsobject<> does not call -retain. | 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 |
| 20 // -retain. |
19 template<typename NST> | 21 template<typename NST> |
20 class scoped_nsobject { | 22 class scoped_nsobject { |
21 public: | 23 public: |
22 typedef NST* element_type; | 24 typedef NST* element_type; |
23 | 25 |
24 explicit scoped_nsobject(NST* object = nil) | 26 explicit scoped_nsobject(NST* object = nil) |
25 : object_(object) { | 27 : object_(object) { |
26 } | 28 } |
27 | 29 |
28 ~scoped_nsobject() { | 30 ~scoped_nsobject() { |
29 [object_ release]; | 31 [object_ release]; |
30 } | 32 } |
31 | 33 |
32 void reset(NST* object = nil) { | 34 void reset(NST* object = nil) { |
33 if (object_ != object) { | 35 [object_ release]; |
34 [object_ release]; | 36 object_ = object; |
35 object_ = object; | |
36 } | |
37 } | 37 } |
38 | 38 |
39 bool operator==(NST* that) const { | 39 bool operator==(NST* that) const { |
40 return object_ == that; | 40 return object_ == that; |
41 } | 41 } |
42 | 42 |
43 bool operator!=(NST* that) const { | 43 bool operator!=(NST* that) const { |
44 return object_ != that; | 44 return object_ != that; |
45 } | 45 } |
46 | 46 |
(...skipping 20 matching lines...) Expand all Loading... |
67 return temp; | 67 return temp; |
68 } | 68 } |
69 | 69 |
70 private: | 70 private: |
71 NST* object_; | 71 NST* object_; |
72 | 72 |
73 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 73 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
74 }; | 74 }; |
75 | 75 |
76 #endif // BASE_SCOPED_NSOBJECT_H_ | 76 #endif // BASE_SCOPED_NSOBJECT_H_ |
OLD | NEW |