OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MAC_SCOPED_NSOBJECT_H_ | 5 #ifndef BASE_MAC_SCOPED_NSOBJECT_H_ |
6 #define BASE_MAC_SCOPED_NSOBJECT_H_ | 6 #define BASE_MAC_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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 template<typename NST> | 34 template<typename NST> |
35 class scoped_nsprotocol { | 35 class scoped_nsprotocol { |
36 public: | 36 public: |
37 explicit scoped_nsprotocol(NST object = nil) : object_(object) {} | 37 explicit scoped_nsprotocol(NST object = nil) : object_(object) {} |
38 | 38 |
39 scoped_nsprotocol(const scoped_nsprotocol<NST>& that) | 39 scoped_nsprotocol(const scoped_nsprotocol<NST>& that) |
40 : object_([that.object_ retain]) { | 40 : object_([that.object_ retain]) { |
41 } | 41 } |
42 | 42 |
| 43 template <typename NSU> |
| 44 scoped_nsprotocol(const scoped_nsprotocol<NSU>& that) |
| 45 : object_([that.get() retain]) { |
| 46 } |
| 47 |
43 ~scoped_nsprotocol() { | 48 ~scoped_nsprotocol() { |
44 [object_ release]; | 49 [object_ release]; |
45 } | 50 } |
46 | 51 |
47 scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) { | 52 scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) { |
48 reset([that.get() retain]); | 53 reset([that.get() retain]); |
49 return *this; | 54 return *this; |
50 } | 55 } |
51 | 56 |
52 void reset(NST object = nil) { | 57 void reset(NST object = nil) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 template<typename NST> | 117 template<typename NST> |
113 class scoped_nsobject : public scoped_nsprotocol<NST*> { | 118 class scoped_nsobject : public scoped_nsprotocol<NST*> { |
114 public: | 119 public: |
115 explicit scoped_nsobject(NST* object = nil) | 120 explicit scoped_nsobject(NST* object = nil) |
116 : scoped_nsprotocol<NST*>(object) {} | 121 : scoped_nsprotocol<NST*>(object) {} |
117 | 122 |
118 scoped_nsobject(const scoped_nsobject<NST>& that) | 123 scoped_nsobject(const scoped_nsobject<NST>& that) |
119 : scoped_nsprotocol<NST*>(that) { | 124 : scoped_nsprotocol<NST*>(that) { |
120 } | 125 } |
121 | 126 |
| 127 template<typename NSU> |
| 128 scoped_nsobject(const scoped_nsobject<NSU>& that) |
| 129 : scoped_nsprotocol<NST*>(that) { |
| 130 } |
| 131 |
122 scoped_nsobject& operator=(const scoped_nsobject<NST>& that) { | 132 scoped_nsobject& operator=(const scoped_nsobject<NST>& that) { |
123 scoped_nsprotocol<NST*>::operator=(that); | 133 scoped_nsprotocol<NST*>::operator=(that); |
124 return *this; | 134 return *this; |
125 } | 135 } |
126 }; | 136 }; |
127 | 137 |
128 // Specialization to make scoped_nsobject<id> work. | 138 // Specialization to make scoped_nsobject<id> work. |
129 template<> | 139 template<> |
130 class scoped_nsobject<id> : public scoped_nsprotocol<id> { | 140 class scoped_nsobject<id> : public scoped_nsprotocol<id> { |
131 public: | 141 public: |
132 explicit scoped_nsobject(id object = nil) : scoped_nsprotocol<id>(object) {} | 142 explicit scoped_nsobject(id object = nil) : scoped_nsprotocol<id>(object) {} |
133 | 143 |
134 scoped_nsobject(const scoped_nsobject<id>& that) | 144 scoped_nsobject(const scoped_nsobject<id>& that) |
135 : scoped_nsprotocol<id>(that) { | 145 : scoped_nsprotocol<id>(that) { |
136 } | 146 } |
137 | 147 |
| 148 template<typename NSU> |
| 149 scoped_nsobject(const scoped_nsobject<NSU>& that) |
| 150 : scoped_nsprotocol<id>(that) { |
| 151 } |
| 152 |
138 scoped_nsobject& operator=(const scoped_nsobject<id>& that) { | 153 scoped_nsobject& operator=(const scoped_nsobject<id>& that) { |
139 scoped_nsprotocol<id>::operator=(that); | 154 scoped_nsprotocol<id>::operator=(that); |
140 return *this; | 155 return *this; |
141 } | 156 } |
142 }; | 157 }; |
143 | 158 |
144 // Do not use scoped_nsobject for NSAutoreleasePools, use | 159 // Do not use scoped_nsobject for NSAutoreleasePools, use |
145 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | 160 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
146 // at top of header. | 161 // at top of header. |
147 template<> | 162 template<> |
148 class scoped_nsobject<NSAutoreleasePool> { | 163 class scoped_nsobject<NSAutoreleasePool> { |
149 private: | 164 private: |
150 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | 165 explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
151 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 166 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
152 }; | 167 }; |
153 | 168 |
154 } // namespace base | 169 } // namespace base |
155 | 170 |
156 #endif // BASE_MAC_SCOPED_NSOBJECT_H_ | 171 #endif // BASE_MAC_SCOPED_NSOBJECT_H_ |
OLD | NEW |