blob: 46aecb5e0b641fb3d004e23b71a5111f02295bfe [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//
39// The implementation assumes that the tracked object will be released on the
40// same thread that the WeakNSObject is created on.
41//
42namespace base {
43
44// WeakContainer keeps a weak pointer to an object and clears it when it
45// receives nullify() from the object's sentinel.
46class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> {
47 public:
48 WeakContainer(id object) : object_(object) {}
49 id object() { return object_; }
50 void nullify() {
51 DCHECK(checker_.CalledOnValidThread());
52 object_ = nil;
53 }
54
55 private:
56 friend base::RefCountedThreadSafe<WeakContainer>;
57 ~WeakContainer() {}
58 base::ThreadChecker checker_;
59 id object_;
60};
61
62} // namespace base
63
64// 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
66// container.
67@interface CRBWeakNSProtocolSentinel : NSObject
68// Return the only associated container for this object. There can be only one.
69// Will return null if object is nil .
70+ (scoped_refptr<base::WeakContainer>)containerForObject:(id)object;
71@end
72
73namespace base {
74
75// Base class for all WeakNSObject derivatives.
76template <typename NST>
77class WeakNSProtocol : public base::NonThreadSafe {
78 public:
79 explicit WeakNSProtocol(NST object = nil) {
80 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
81 }
82
83 WeakNSProtocol(const WeakNSProtocol<NST>& that) {
84 container_ = that.container_;
85 }
86
87 ~WeakNSProtocol() {
88 // A WeakNSProtocol object can be allocated on one thread and released on
89 // another. This is not the case for the contained object.
90 DetachFromThread();
91 }
92
93 void reset(NST object = nil) {
94 DCHECK(CalledOnValidThread());
95 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
96 }
97
98 NST get() const {
99 DCHECK(CalledOnValidThread());
100 if (!container_.get())
101 return nil;
102 return container_->object();
103 }
104
105 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
106 DCHECK(CalledOnValidThread());
107 container_ = that.container_;
108 return *this;
109 }
110
111 bool operator==(NST that) const {
112 DCHECK(CalledOnValidThread());
113 return get() == that;
114 }
115
116 bool operator!=(NST that) const { return get() != that; }
117
118 operator NST() const { return get(); }
119
120 private:
121 // Refecounted reference to the container tracking the ObjectiveC object this
122 // class encapsulates.
123 scoped_refptr<base::WeakContainer> container_;
124};
125
126// Free functions
127template <class NST>
128bool operator==(NST p1, const WeakNSProtocol<NST>& p2) {
129 return p1 == p2.get();
130}
131
132template <class NST>
133bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) {
134 return p1 != p2.get();
135}
136
137template <typename NST>
138class WeakNSObject : public WeakNSProtocol<NST*> {
139 public:
140 explicit WeakNSObject(NST* object = nil) : WeakNSProtocol<NST*>(object) {}
141
142 WeakNSObject(const WeakNSObject<NST>& that) : WeakNSProtocol<NST*>(that) {}
143
144 WeakNSObject& operator=(const WeakNSObject<NST>& that) {
145 WeakNSProtocol<NST*>::operator=(that);
146 return *this;
147 }
148};
149
150// Specialization to make WeakNSObject<id> work.
151template <>
152class WeakNSObject<id> : public WeakNSProtocol<id> {
153 public:
154 explicit WeakNSObject(id object = nil) : WeakNSProtocol<id>(object) {}
155
156 WeakNSObject(const WeakNSObject<id>& that) : WeakNSProtocol<id>(that) {}
157
158 WeakNSObject& operator=(const WeakNSObject<id>& that) {
159 WeakNSProtocol<id>::operator=(that);
160 return *this;
161 }
162};
163
164} // namespace base
165
166#endif // BASE_IOS_WEAK_NSOBJECT_H_