| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_IOS_WEAK_NSOBJECT_H_ | |
| 6 #define BASE_IOS_WEAK_NSOBJECT_H_ | |
| 7 | |
| 8 #import <Foundation/Foundation.h> | |
| 9 #import <objc/runtime.h> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/threading/non_thread_safe.h" | |
| 16 #include "base/threading/thread_checker.h" | |
| 17 | |
| 18 // WeakNSObject<> is patterned after scoped_nsobject<>, but instead of | |
| 19 // maintaining ownership of an NSObject subclass object, it will nil itself out | |
| 20 // when the object is deallocated. | |
| 21 // | |
| 22 // WeakNSProtocol<> has the same behavior as WeakNSObject, but can be used | |
| 23 // with protocols. | |
| 24 // | |
| 25 // Example usage (base::WeakNSObject<T>): | |
| 26 // scoped_nsobject<Foo> foo([[Foo alloc] init]); | |
| 27 // WeakNSObject<Foo> weak_foo; // No pointer | |
| 28 // weak_foo.reset(foo) // Now a weak reference is kept. | |
| 29 // [weak_foo description]; // Returns [foo description]. | |
| 30 // foo.reset(); // The reference is released. | |
| 31 // [weak_foo description]; // Returns nil, as weak_foo is pointing to nil. | |
| 32 // | |
| 33 // | |
| 34 // Implementation wise a WeakNSObject keeps a reference to a refcounted | |
| 35 // WeakContainer. There is one unique instance of a WeakContainer per watched | |
| 36 // NSObject, this relationship is maintained via the ObjectiveC associated | |
| 37 // object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class. | |
| 38 // | |
| 39 // Threading restrictions: | |
| 40 // - Several WeakNSObject pointing to the same underlying object must all be | |
| 41 // created and dereferenced on the same thread; | |
| 42 // - thread safety is enforced by the implementation, except in two cases: | |
| 43 // (1) it is allowed to copy a WeakNSObject on a different thread. However, | |
| 44 // that copy must return to the original thread before being dereferenced, | |
| 45 // (2) it is allowed to destroy a WeakNSObject on any thread; | |
| 46 // - the implementation assumes that the tracked object will be released on the | |
| 47 // same thread that the WeakNSObject is created on. | |
| 48 namespace base { | |
| 49 | |
| 50 // WeakContainer keeps a weak pointer to an object and clears it when it | |
| 51 // receives nullify() from the object's sentinel. | |
| 52 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { | |
| 53 public: | |
| 54 explicit WeakContainer(id object) : object_(object) {} | |
| 55 | |
| 56 id object() { | |
| 57 DCHECK(checker_.CalledOnValidThread()); | |
| 58 return object_; | |
| 59 } | |
| 60 | |
| 61 void nullify() { | |
| 62 DCHECK(checker_.CalledOnValidThread()); | |
| 63 object_ = nil; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 friend base::RefCountedThreadSafe<WeakContainer>; | |
| 68 ~WeakContainer() {} | |
| 69 base::ThreadChecker checker_; | |
| 70 id object_; | |
| 71 }; | |
| 72 | |
| 73 } // namespace base | |
| 74 | |
| 75 // Sentinel for observing the object contained in the weak pointer. The object | |
| 76 // will be deleted when the weak object is deleted and will notify its | |
| 77 // container. | |
| 78 @interface CRBWeakNSProtocolSentinel : NSObject | |
| 79 // Return the only associated container for this object. There can be only one. | |
| 80 // Will return null if object is nil . | |
| 81 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object; | |
| 82 @end | |
| 83 | |
| 84 namespace base { | |
| 85 | |
| 86 // Base class for all WeakNSObject derivatives. | |
| 87 template <typename NST> | |
| 88 class WeakNSProtocol { | |
| 89 public: | |
| 90 explicit WeakNSProtocol(NST object = nil) { | |
| 91 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; | |
| 92 } | |
| 93 | |
| 94 WeakNSProtocol(const WeakNSProtocol<NST>& that) { | |
| 95 // A WeakNSProtocol object can be copied on one thread and used on | |
| 96 // another. | |
| 97 checker_.DetachFromThread(); | |
| 98 container_ = that.container_; | |
| 99 } | |
| 100 | |
| 101 ~WeakNSProtocol() { | |
| 102 // A WeakNSProtocol object can be used on one thread and released on | |
| 103 // another. This is not the case for the contained object. | |
| 104 checker_.DetachFromThread(); | |
| 105 } | |
| 106 | |
| 107 void reset(NST object = nil) { | |
| 108 DCHECK(checker_.CalledOnValidThread()); | |
| 109 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; | |
| 110 } | |
| 111 | |
| 112 NST get() const { | |
| 113 DCHECK(checker_.CalledOnValidThread()); | |
| 114 if (!container_.get()) | |
| 115 return nil; | |
| 116 return container_->object(); | |
| 117 } | |
| 118 | |
| 119 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) { | |
| 120 // A WeakNSProtocol object can be copied on one thread and used on | |
| 121 // another. | |
| 122 checker_.DetachFromThread(); | |
| 123 container_ = that.container_; | |
| 124 return *this; | |
| 125 } | |
| 126 | |
| 127 bool operator==(NST that) const { | |
| 128 DCHECK(checker_.CalledOnValidThread()); | |
| 129 return get() == that; | |
| 130 } | |
| 131 | |
| 132 bool operator!=(NST that) const { | |
| 133 DCHECK(checker_.CalledOnValidThread()); | |
| 134 return get() != that; | |
| 135 } | |
| 136 | |
| 137 operator NST() const { | |
| 138 DCHECK(checker_.CalledOnValidThread()); | |
| 139 return get(); | |
| 140 } | |
| 141 | |
| 142 private: | |
| 143 // Refecounted reference to the container tracking the ObjectiveC object this | |
| 144 // class encapsulates. | |
| 145 scoped_refptr<base::WeakContainer> container_; | |
| 146 base::ThreadChecker checker_; | |
| 147 }; | |
| 148 | |
| 149 // Free functions | |
| 150 template <class NST> | |
| 151 bool operator==(NST p1, const WeakNSProtocol<NST>& p2) { | |
| 152 return p1 == p2.get(); | |
| 153 } | |
| 154 | |
| 155 template <class NST> | |
| 156 bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) { | |
| 157 return p1 != p2.get(); | |
| 158 } | |
| 159 | |
| 160 template <typename NST> | |
| 161 class WeakNSObject : public WeakNSProtocol<NST*> { | |
| 162 public: | |
| 163 explicit WeakNSObject(NST* object = nil) : WeakNSProtocol<NST*>(object) {} | |
| 164 | |
| 165 WeakNSObject(const WeakNSObject<NST>& that) : WeakNSProtocol<NST*>(that) {} | |
| 166 | |
| 167 WeakNSObject& operator=(const WeakNSObject<NST>& that) { | |
| 168 WeakNSProtocol<NST*>::operator=(that); | |
| 169 return *this; | |
| 170 } | |
| 171 }; | |
| 172 | |
| 173 // Specialization to make WeakNSObject<id> work. | |
| 174 template <> | |
| 175 class WeakNSObject<id> : public WeakNSProtocol<id> { | |
| 176 public: | |
| 177 explicit WeakNSObject(id object = nil) : WeakNSProtocol<id>(object) {} | |
| 178 | |
| 179 WeakNSObject(const WeakNSObject<id>& that) : WeakNSProtocol<id>(that) {} | |
| 180 | |
| 181 WeakNSObject& operator=(const WeakNSObject<id>& that) { | |
| 182 WeakNSProtocol<id>::operator=(that); | |
| 183 return *this; | |
| 184 } | |
| 185 }; | |
| 186 | |
| 187 } // namespace base | |
| 188 | |
| 189 #endif // BASE_IOS_WEAK_NSOBJECT_H_ | |
| OLD | NEW |