blob: fc3a7c38dc4ab5b195bdc9669d5ff50bc1095e7f [file] [log] [blame]
drogerc6762652014-12-12 17:39:021// 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//
droger4f05f5a82015-01-16 11:33:5239// 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.
drogerc6762652014-12-12 17:39:0248namespace base {
49
50// WeakContainer keeps a weak pointer to an object and clears it when it
51// receives nullify() from the object's sentinel.
52class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> {
53 public:
droger4f05f5a82015-01-16 11:33:5254 explicit WeakContainer(id object) : object_(object) {}
55
56 id object() {
57 DCHECK(checker_.CalledOnValidThread());
58 return object_;
59 }
60
drogerc6762652014-12-12 17:39:0261 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
84namespace base {
85
86// Base class for all WeakNSObject derivatives.
87template <typename NST>
droger4f05f5a82015-01-16 11:33:5288class WeakNSProtocol {
drogerc6762652014-12-12 17:39:0289 public:
90 explicit WeakNSProtocol(NST object = nil) {
91 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
92 }
93
94 WeakNSProtocol(const WeakNSProtocol<NST>& that) {
droger4f05f5a82015-01-16 11:33:5295 // A WeakNSProtocol object can be copied on one thread and used on
96 // another.
97 checker_.DetachFromThread();
drogerc6762652014-12-12 17:39:0298 container_ = that.container_;
99 }
100
101 ~WeakNSProtocol() {
droger4f05f5a82015-01-16 11:33:52102 // A WeakNSProtocol object can be used on one thread and released on
drogerc6762652014-12-12 17:39:02103 // another. This is not the case for the contained object.
droger4f05f5a82015-01-16 11:33:52104 checker_.DetachFromThread();
drogerc6762652014-12-12 17:39:02105 }
106
107 void reset(NST object = nil) {
droger4f05f5a82015-01-16 11:33:52108 DCHECK(checker_.CalledOnValidThread());
drogerc6762652014-12-12 17:39:02109 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
110 }
111
112 NST get() const {
droger4f05f5a82015-01-16 11:33:52113 DCHECK(checker_.CalledOnValidThread());
drogerc6762652014-12-12 17:39:02114 if (!container_.get())
115 return nil;
116 return container_->object();
117 }
118
119 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
droger1fd547a2015-03-17 15:13:08120 // A WeakNSProtocol object can be copied on one thread and used on
121 // another.
122 checker_.DetachFromThread();
drogerc6762652014-12-12 17:39:02123 container_ = that.container_;
124 return *this;
125 }
126
127 bool operator==(NST that) const {
droger4f05f5a82015-01-16 11:33:52128 DCHECK(checker_.CalledOnValidThread());
drogerc6762652014-12-12 17:39:02129 return get() == that;
130 }
131
droger4f05f5a82015-01-16 11:33:52132 bool operator!=(NST that) const {
133 DCHECK(checker_.CalledOnValidThread());
134 return get() != that;
135 }
drogerc6762652014-12-12 17:39:02136
droger4f05f5a82015-01-16 11:33:52137 operator NST() const {
138 DCHECK(checker_.CalledOnValidThread());
139 return get();
140 }
drogerc6762652014-12-12 17:39:02141
142 private:
143 // Refecounted reference to the container tracking the ObjectiveC object this
144 // class encapsulates.
145 scoped_refptr<base::WeakContainer> container_;
droger4f05f5a82015-01-16 11:33:52146 base::ThreadChecker checker_;
drogerc6762652014-12-12 17:39:02147};
148
149// Free functions
150template <class NST>
151bool operator==(NST p1, const WeakNSProtocol<NST>& p2) {
152 return p1 == p2.get();
153}
154
155template <class NST>
156bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) {
157 return p1 != p2.get();
158}
159
160template <typename NST>
161class 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.
174template <>
175class 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_