| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_IOS_WEAK_NSOBJECT_H_ | 5 #ifndef BASE_IOS_WEAK_NSOBJECT_H_ |
| 6 #define BASE_IOS_WEAK_NSOBJECT_H_ | 6 #define BASE_IOS_WEAK_NSOBJECT_H_ |
| 7 | 7 |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 #import <objc/runtime.h> | 9 #import <objc/runtime.h> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // that copy must return to the original thread before being dereferenced, | 42 // that copy must return to the original thread before being dereferenced, |
| 43 // (2) it is allowed to destroy a WeakNSObject on any thread; | 43 // (2) it is allowed to destroy a WeakNSObject on any thread; |
| 44 // - the implementation assumes that the tracked object will be released on the | 44 // - the implementation assumes that the tracked object will be released on the |
| 45 // same thread that the WeakNSObject is created on. | 45 // same thread that the WeakNSObject is created on. |
| 46 namespace base { | 46 namespace base { |
| 47 | 47 |
| 48 // WeakContainer keeps a weak pointer to an object and clears it when it | 48 // WeakContainer keeps a weak pointer to an object and clears it when it |
| 49 // receives nullify() from the object's sentinel. | 49 // receives nullify() from the object's sentinel. |
| 50 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { | 50 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { |
| 51 public: | 51 public: |
| 52 explicit WeakContainer(id object) : object_(object) {} | 52 explicit WeakContainer(id object); |
| 53 | 53 |
| 54 id object() { | 54 id object() { |
| 55 DCHECK(checker_.CalledOnValidThread()); | 55 DCHECK(checker_.CalledOnValidThread()); |
| 56 return object_; | 56 return object_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void nullify() { | 59 void nullify() { |
| 60 DCHECK(checker_.CalledOnValidThread()); | 60 DCHECK(checker_.CalledOnValidThread()); |
| 61 object_ = nil; | 61 object_ = nil; |
| 62 } | 62 } |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 friend base::RefCountedThreadSafe<WeakContainer>; | 65 friend base::RefCountedThreadSafe<WeakContainer>; |
| 66 ~WeakContainer() {} | 66 ~WeakContainer(); |
| 67 base::ThreadChecker checker_; | 67 base::ThreadChecker checker_; |
| 68 id object_; | 68 __unsafe_unretained id object_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 } // namespace base | 71 } // namespace base |
| 72 | 72 |
| 73 // Sentinel for observing the object contained in the weak pointer. The object | 73 // Sentinel for observing the object contained in the weak pointer. The object |
| 74 // will be deleted when the weak object is deleted and will notify its | 74 // will be deleted when the weak object is deleted and will notify its |
| 75 // container. | 75 // container. |
| 76 @interface CRBWeakNSProtocolSentinel : NSObject | 76 @interface CRBWeakNSProtocolSentinel : NSObject |
| 77 // Return the only associated container for this object. There can be only one. | 77 // Return the only associated container for this object. There can be only one. |
| 78 // Will return null if object is nil . | 78 // Will return null if object is nil . |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 178 |
| 179 WeakNSObject& operator=(const WeakNSObject<id>& that) { | 179 WeakNSObject& operator=(const WeakNSObject<id>& that) { |
| 180 WeakNSProtocol<id>::operator=(that); | 180 WeakNSProtocol<id>::operator=(that); |
| 181 return *this; | 181 return *this; |
| 182 } | 182 } |
| 183 }; | 183 }; |
| 184 | 184 |
| 185 } // namespace base | 185 } // namespace base |
| 186 | 186 |
| 187 #endif // BASE_IOS_WEAK_NSOBJECT_H_ | 187 #endif // BASE_IOS_WEAK_NSOBJECT_H_ |
| OLD | NEW |