Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: base/ios/weak_nsobject.h

Issue 862133002: Update from https://crrev.com/312398 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 18 matching lines...) Expand all
29 // [weak_foo description]; // Returns [foo description]. 29 // [weak_foo description]; // Returns [foo description].
30 // foo.reset(); // The reference is released. 30 // foo.reset(); // The reference is released.
31 // [weak_foo description]; // Returns nil, as weak_foo is pointing to nil. 31 // [weak_foo description]; // Returns nil, as weak_foo is pointing to nil.
32 // 32 //
33 // 33 //
34 // Implementation wise a WeakNSObject keeps a reference to a refcounted 34 // Implementation wise a WeakNSObject keeps a reference to a refcounted
35 // WeakContainer. There is one unique instance of a WeakContainer per watched 35 // WeakContainer. There is one unique instance of a WeakContainer per watched
36 // NSObject, this relationship is maintained via the ObjectiveC associated 36 // NSObject, this relationship is maintained via the ObjectiveC associated
37 // object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class. 37 // object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class.
38 // 38 //
39 // The implementation assumes that the tracked object will be released on the 39 // Threading restrictions:
40 // same thread that the WeakNSObject is created on. 40 // - Several WeakNSObject pointing to the same underlying object must all be
41 // 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.
42 namespace base { 48 namespace base {
43 49
44 // WeakContainer keeps a weak pointer to an object and clears it when it 50 // WeakContainer keeps a weak pointer to an object and clears it when it
45 // receives nullify() from the object's sentinel. 51 // receives nullify() from the object's sentinel.
46 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { 52 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> {
47 public: 53 public:
48 WeakContainer(id object) : object_(object) {} 54 explicit WeakContainer(id object) : object_(object) {}
49 id object() { return object_; } 55
56 id object() {
57 DCHECK(checker_.CalledOnValidThread());
58 return object_;
59 }
60
50 void nullify() { 61 void nullify() {
51 DCHECK(checker_.CalledOnValidThread()); 62 DCHECK(checker_.CalledOnValidThread());
52 object_ = nil; 63 object_ = nil;
53 } 64 }
54 65
55 private: 66 private:
56 friend base::RefCountedThreadSafe<WeakContainer>; 67 friend base::RefCountedThreadSafe<WeakContainer>;
57 ~WeakContainer() {} 68 ~WeakContainer() {}
58 base::ThreadChecker checker_; 69 base::ThreadChecker checker_;
59 id object_; 70 id object_;
60 }; 71 };
61 72
62 } // namespace base 73 } // namespace base
63 74
64 // Sentinel for observing the object contained in the weak pointer. The object 75 // Sentinel for observing the object contained in the weak pointer. The object
65 // will be deleted when the weak object is deleted and will notify its 76 // will be deleted when the weak object is deleted and will notify its
66 // container. 77 // container.
67 @interface CRBWeakNSProtocolSentinel : NSObject 78 @interface CRBWeakNSProtocolSentinel : NSObject
68 // Return the only associated container for this object. There can be only one. 79 // Return the only associated container for this object. There can be only one.
69 // Will return null if object is nil . 80 // Will return null if object is nil .
70 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object; 81 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object;
71 @end 82 @end
72 83
73 namespace base { 84 namespace base {
74 85
75 // Base class for all WeakNSObject derivatives. 86 // Base class for all WeakNSObject derivatives.
76 template <typename NST> 87 template <typename NST>
77 class WeakNSProtocol : public base::NonThreadSafe { 88 class WeakNSProtocol {
78 public: 89 public:
79 explicit WeakNSProtocol(NST object = nil) { 90 explicit WeakNSProtocol(NST object = nil) {
80 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; 91 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
81 } 92 }
82 93
83 WeakNSProtocol(const WeakNSProtocol<NST>& that) { 94 WeakNSProtocol(const WeakNSProtocol<NST>& that) {
95 // A WeakNSProtocol object can be copied on one thread and used on
96 // another.
97 checker_.DetachFromThread();
84 container_ = that.container_; 98 container_ = that.container_;
85 } 99 }
86 100
87 ~WeakNSProtocol() { 101 ~WeakNSProtocol() {
88 // A WeakNSProtocol object can be allocated on one thread and released on 102 // A WeakNSProtocol object can be used on one thread and released on
89 // another. This is not the case for the contained object. 103 // another. This is not the case for the contained object.
90 DetachFromThread(); 104 checker_.DetachFromThread();
91 } 105 }
92 106
93 void reset(NST object = nil) { 107 void reset(NST object = nil) {
94 DCHECK(CalledOnValidThread()); 108 DCHECK(checker_.CalledOnValidThread());
95 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; 109 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
96 } 110 }
97 111
98 NST get() const { 112 NST get() const {
99 DCHECK(CalledOnValidThread()); 113 DCHECK(checker_.CalledOnValidThread());
100 if (!container_.get()) 114 if (!container_.get())
101 return nil; 115 return nil;
102 return container_->object(); 116 return container_->object();
103 } 117 }
104 118
105 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) { 119 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
106 DCHECK(CalledOnValidThread()); 120 DCHECK(checker_.CalledOnValidThread());
107 container_ = that.container_; 121 container_ = that.container_;
108 return *this; 122 return *this;
109 } 123 }
110 124
111 bool operator==(NST that) const { 125 bool operator==(NST that) const {
112 DCHECK(CalledOnValidThread()); 126 DCHECK(checker_.CalledOnValidThread());
113 return get() == that; 127 return get() == that;
114 } 128 }
115 129
116 bool operator!=(NST that) const { return get() != that; } 130 bool operator!=(NST that) const {
131 DCHECK(checker_.CalledOnValidThread());
132 return get() != that;
133 }
117 134
118 operator NST() const { return get(); } 135 operator NST() const {
136 DCHECK(checker_.CalledOnValidThread());
137 return get();
138 }
119 139
120 private: 140 private:
121 // Refecounted reference to the container tracking the ObjectiveC object this 141 // Refecounted reference to the container tracking the ObjectiveC object this
122 // class encapsulates. 142 // class encapsulates.
123 scoped_refptr<base::WeakContainer> container_; 143 scoped_refptr<base::WeakContainer> container_;
144 base::ThreadChecker checker_;
124 }; 145 };
125 146
126 // Free functions 147 // Free functions
127 template <class NST> 148 template <class NST>
128 bool operator==(NST p1, const WeakNSProtocol<NST>& p2) { 149 bool operator==(NST p1, const WeakNSProtocol<NST>& p2) {
129 return p1 == p2.get(); 150 return p1 == p2.get();
130 } 151 }
131 152
132 template <class NST> 153 template <class NST>
133 bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) { 154 bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) {
(...skipping 23 matching lines...) Expand all
157 178
158 WeakNSObject& operator=(const WeakNSObject<id>& that) { 179 WeakNSObject& operator=(const WeakNSObject<id>& that) {
159 WeakNSProtocol<id>::operator=(that); 180 WeakNSProtocol<id>::operator=(that);
160 return *this; 181 return *this;
161 } 182 }
162 }; 183 };
163 184
164 } // namespace base 185 } // namespace base
165 186
166 #endif // BASE_IOS_WEAK_NSOBJECT_H_ 187 #endif // BASE_IOS_WEAK_NSOBJECT_H_
OLDNEW
« no previous file with comments | « base/i18n/rtl.cc ('k') | base/ios/weak_nsobject_unittest.mm » ('j') | shell/BUILD.gn » ('J')

Powered by Google App Engine
This is Rietveld 408576698