OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_TASK_H_ | 5 #ifndef BASE_TASK_H_ |
6 #define BASE_TASK_H_ | 6 #define BASE_TASK_H_ |
7 | 7 |
8 #include "base/non_thread_safe.h" | 8 #include "base/non_thread_safe.h" |
9 #include "base/raw_scoped_refptr_mismatch_checker.h" | 9 #include "base/raw_scoped_refptr_mismatch_checker.h" |
10 #include "base/tracked.h" | 10 #include "base/tracked.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // A ScopedRunnableMethodFactory creates runnable methods for a specified | 76 // A ScopedRunnableMethodFactory creates runnable methods for a specified |
77 // object. This is particularly useful for generating callbacks for | 77 // object. This is particularly useful for generating callbacks for |
78 // non-reference counted objects when the factory is a member of the object. | 78 // non-reference counted objects when the factory is a member of the object. |
79 template<class T> | 79 template<class T> |
80 class ScopedRunnableMethodFactory { | 80 class ScopedRunnableMethodFactory { |
81 public: | 81 public: |
82 explicit ScopedRunnableMethodFactory(T* object) : weak_factory_(object) { | 82 explicit ScopedRunnableMethodFactory(T* object) : weak_factory_(object) { |
83 } | 83 } |
84 | 84 |
85 template <class Method> | 85 template <class Method> |
86 inline Task* NewRunnableMethod(Method method) { | 86 inline CancelableTask* NewRunnableMethod(Method method) { |
87 return new RunnableMethod<Method, Tuple0>( | 87 return new RunnableMethod<Method, Tuple0>( |
88 weak_factory_.GetWeakPtr(), method, MakeTuple()); | 88 weak_factory_.GetWeakPtr(), method, MakeTuple()); |
89 } | 89 } |
90 | 90 |
91 template <class Method, class A> | 91 template <class Method, class A> |
92 inline Task* NewRunnableMethod(Method method, const A& a) { | 92 inline CancelableTask* NewRunnableMethod(Method method, const A& a) { |
93 return new RunnableMethod<Method, Tuple1<A> >( | 93 return new RunnableMethod<Method, Tuple1<A> >( |
94 weak_factory_.GetWeakPtr(), method, MakeTuple(a)); | 94 weak_factory_.GetWeakPtr(), method, MakeTuple(a)); |
95 } | 95 } |
96 | 96 |
97 template <class Method, class A, class B> | 97 template <class Method, class A, class B> |
98 inline Task* NewRunnableMethod(Method method, const A& a, const B& b) { | 98 inline CancelableTask* NewRunnableMethod(Method method, const A& a, |
| 99 const B& b) { |
99 return new RunnableMethod<Method, Tuple2<A, B> >( | 100 return new RunnableMethod<Method, Tuple2<A, B> >( |
100 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b)); | 101 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b)); |
101 } | 102 } |
102 | 103 |
103 template <class Method, class A, class B, class C> | 104 template <class Method, class A, class B, class C> |
104 inline Task* NewRunnableMethod(Method method, | 105 inline CancelableTask* NewRunnableMethod(Method method, |
105 const A& a, | 106 const A& a, |
106 const B& b, | 107 const B& b, |
107 const C& c) { | 108 const C& c) { |
108 return new RunnableMethod<Method, Tuple3<A, B, C> >( | 109 return new RunnableMethod<Method, Tuple3<A, B, C> >( |
109 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c)); | 110 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c)); |
110 } | 111 } |
111 | 112 |
112 template <class Method, class A, class B, class C, class D> | 113 template <class Method, class A, class B, class C, class D> |
113 inline Task* NewRunnableMethod(Method method, | 114 inline CancelableTask* NewRunnableMethod(Method method, |
114 const A& a, | 115 const A& a, |
115 const B& b, | 116 const B& b, |
116 const C& c, | 117 const C& c, |
117 const D& d) { | 118 const D& d) { |
118 return new RunnableMethod<Method, Tuple4<A, B, C, D> >( | 119 return new RunnableMethod<Method, Tuple4<A, B, C, D> >( |
119 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d)); | 120 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d)); |
120 } | 121 } |
121 | 122 |
122 template <class Method, class A, class B, class C, class D, class E> | 123 template <class Method, class A, class B, class C, class D, class E> |
123 inline Task* NewRunnableMethod(Method method, | 124 inline CancelableTask* NewRunnableMethod(Method method, |
124 const A& a, | 125 const A& a, |
125 const B& b, | 126 const B& b, |
126 const C& c, | 127 const C& c, |
127 const D& d, | 128 const D& d, |
128 const E& e) { | 129 const E& e) { |
129 return new RunnableMethod<Method, Tuple5<A, B, C, D, E> >( | 130 return new RunnableMethod<Method, Tuple5<A, B, C, D, E> >( |
130 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d, e)); | 131 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d, e)); |
131 } | 132 } |
132 | 133 |
133 void RevokeAll() { weak_factory_.InvalidateWeakPtrs(); } | 134 void RevokeAll() { weak_factory_.InvalidateWeakPtrs(); } |
134 | 135 |
135 bool empty() const { return !weak_factory_.HasWeakPtrs(); } | 136 bool empty() const { return !weak_factory_.HasWeakPtrs(); } |
136 | 137 |
137 protected: | 138 protected: |
138 template <class Method, class Params> | 139 template <class Method, class Params> |
139 class RunnableMethod : public Task { | 140 class RunnableMethod : public CancelableTask { |
140 public: | 141 public: |
141 RunnableMethod(const base::WeakPtr<T>& obj, Method meth, const Params& param
s) | 142 RunnableMethod(const base::WeakPtr<T>& obj, Method meth, |
| 143 const Params& params) |
142 : obj_(obj), | 144 : obj_(obj), |
143 meth_(meth), | 145 meth_(meth), |
144 params_(params) { | 146 params_(params) { |
145 COMPILE_ASSERT((MethodUsesScopedRefptrCorrectly<Method, Params>::value), | 147 COMPILE_ASSERT((MethodUsesScopedRefptrCorrectly<Method, Params>::value), |
146 badscopedrunnablemethodparams); | 148 badscopedrunnablemethodparams); |
147 } | 149 } |
148 | 150 |
149 virtual void Run() { | 151 virtual void Run() { |
150 if (obj_) | 152 if (obj_) |
151 DispatchToMethod(obj_.get(), meth_, params_); | 153 DispatchToMethod(obj_.get(), meth_, params_); |
152 } | 154 } |
153 | 155 |
| 156 virtual void Cancel() { |
| 157 obj_.reset(); |
| 158 } |
| 159 |
154 private: | 160 private: |
155 base::WeakPtr<T> obj_; | 161 base::WeakPtr<T> obj_; |
156 Method meth_; | 162 Method meth_; |
157 Params params_; | 163 Params params_; |
158 | 164 |
159 DISALLOW_COPY_AND_ASSIGN(RunnableMethod); | 165 DISALLOW_COPY_AND_ASSIGN(RunnableMethod); |
160 }; | 166 }; |
161 | 167 |
162 private: | 168 private: |
163 base::WeakPtrFactory<T> weak_factory_; | 169 base::WeakPtrFactory<T> weak_factory_; |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 const A& a, const B& b, | 454 const A& a, const B& b, |
449 const C& c, const D& d, | 455 const C& c, const D& d, |
450 const E& e) { | 456 const E& e) { |
451 return new RunnableFunction<Function, Tuple5<A, B, C, D, E> >(function, | 457 return new RunnableFunction<Function, Tuple5<A, B, C, D, E> >(function, |
452 MakeTuple(a, b, | 458 MakeTuple(a, b, |
453 c, d, | 459 c, d, |
454 e)); | 460 e)); |
455 } | 461 } |
456 | 462 |
457 #endif // BASE_TASK_H_ | 463 #endif // BASE_TASK_H_ |
OLD | NEW |