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 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 void reset(NST* object = nil) { | 41 void reset(NST* object = nil) { |
42 // We intentionally do not check that object != object_ as the caller must | 42 // We intentionally do not check that object != object_ as the caller must |
43 // already have an ownership claim over whatever it gives to scoped_nsobject | 43 // already have an ownership claim over whatever it gives to scoped_nsobject |
44 // and scoped_cftyperef, whether it's in the constructor or in a call to | 44 // and scoped_cftyperef, whether it's in the constructor or in a call to |
45 // reset(). In either case, it relinquishes that claim and the scoper | 45 // reset(). In either case, it relinquishes that claim and the scoper |
46 // assumes it. | 46 // assumes it. |
47 [object_ release]; | 47 [object_ release]; |
48 object_ = object; | 48 object_ = object; |
49 } | 49 } |
50 | 50 |
51 bool operator==(NST* that) const { return object_ == that; } | 51 bool operator==(NST* that) const { |
52 bool operator!=(NST* that) const { return object_ != that; } | 52 return object_ == that; |
| 53 } |
| 54 |
| 55 bool operator!=(NST* that) const { |
| 56 return object_ != that; |
| 57 } |
53 | 58 |
54 operator NST*() const { | 59 operator NST*() const { |
55 return object_; | 60 return object_; |
56 } | 61 } |
57 | 62 |
58 NST* get() const { | 63 NST* get() const { |
59 return object_; | 64 return object_; |
60 } | 65 } |
61 | 66 |
62 void swap(scoped_nsobject& that) { | 67 void swap(scoped_nsobject& that) { |
(...skipping 10 matching lines...) Expand all Loading... |
73 object_ = nil; | 78 object_ = nil; |
74 return temp; | 79 return temp; |
75 } | 80 } |
76 | 81 |
77 private: | 82 private: |
78 NST* object_; | 83 NST* object_; |
79 | 84 |
80 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 85 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
81 }; | 86 }; |
82 | 87 |
83 // Free functions | |
84 template <class C> | |
85 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { | |
86 p1.swap(p2); | |
87 } | |
88 | |
89 template <class C> | |
90 bool operator==(C* p1, const scoped_nsobject<C>& p2) { | |
91 return p1 == p2.get(); | |
92 } | |
93 | |
94 template <class C> | |
95 bool operator!=(C* p1, const scoped_nsobject<C>& p2) { | |
96 return p1 != p2.get(); | |
97 } | |
98 | |
99 | |
100 // Specialization to make scoped_nsobject<id> work. | 88 // Specialization to make scoped_nsobject<id> work. |
101 template<> | 89 template<> |
102 class scoped_nsobject<id> { | 90 class scoped_nsobject<id> { |
103 public: | 91 public: |
104 typedef id element_type; | 92 typedef id element_type; |
105 | 93 |
106 explicit scoped_nsobject(id object = nil) | 94 explicit scoped_nsobject(id object = nil) |
107 : object_(object) { | 95 : object_(object) { |
108 } | 96 } |
109 | 97 |
110 ~scoped_nsobject() { | 98 ~scoped_nsobject() { |
111 [object_ release]; | 99 [object_ release]; |
112 } | 100 } |
113 | 101 |
114 void reset(id object = nil) { | 102 void reset(id object = nil) { |
115 // We intentionally do not check that object != object_ as the caller must | 103 // We intentionally do not check that object != object_ as the caller must |
116 // already have an ownership claim over whatever it gives to scoped_nsobject | 104 // already have an ownership claim over whatever it gives to scoped_nsobject |
117 // and scoped_cftyperef, whether it's in the constructor or in a call to | 105 // and scoped_cftyperef, whether it's in the constructor or in a call to |
118 // reset(). In either case, it relinquishes that claim and the scoper | 106 // reset(). In either case, it relinquishes that claim and the scoper |
119 // assumes it. | 107 // assumes it. |
120 [object_ release]; | 108 [object_ release]; |
121 object_ = object; | 109 object_ = object; |
122 } | 110 } |
123 | 111 |
124 bool operator==(id that) const { return object_ == that; } | 112 bool operator==(id that) const { |
125 bool operator!=(id that) const { return object_ != that; } | 113 return object_ == that; |
| 114 } |
| 115 |
| 116 bool operator!=(id that) const { |
| 117 return object_ != that; |
| 118 } |
126 | 119 |
127 operator id() const { | 120 operator id() const { |
128 return object_; | 121 return object_; |
129 } | 122 } |
130 | 123 |
131 id get() const { | 124 id get() const { |
132 return object_; | 125 return object_; |
133 } | 126 } |
134 | 127 |
135 void swap(scoped_nsobject& that) { | 128 void swap(scoped_nsobject& that) { |
(...skipping 21 matching lines...) Expand all Loading... |
157 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | 150 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
158 // at top of header. | 151 // at top of header. |
159 template<> | 152 template<> |
160 class scoped_nsobject<NSAutoreleasePool> { | 153 class scoped_nsobject<NSAutoreleasePool> { |
161 private: | 154 private: |
162 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | 155 explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
163 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 156 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
164 }; | 157 }; |
165 | 158 |
166 #endif // BASE_SCOPED_NSOBJECT_H_ | 159 #endif // BASE_SCOPED_NSOBJECT_H_ |
OLD | NEW |