summaryrefslogtreecommitdiffstats
path: root/base/task.h
diff options
context:
space:
mode:
authordarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-25 23:34:34 +0000
committerdarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-25 23:34:34 +0000
commit59326aacf6020c3cfa8978a334c36b2dcc1a99bb (patch)
tree698940cee180d498131c98b9efbb2de30b642161 /base/task.h
parent754f7e974507e71f6ac40eec66bcb73f13d868c6 (diff)
downloadchromium_src-59326aacf6020c3cfa8978a334c36b2dcc1a99bb.zip
chromium_src-59326aacf6020c3cfa8978a334c36b2dcc1a99bb.tar.gz
chromium_src-59326aacf6020c3cfa8978a334c36b2dcc1a99bb.tar.bz2
Implement ScopedRunnableMethodFactory using WeakPtr.
This required some changes to WeakPtr to support the addition of WeakPtrFactory::HasWeakPtrs(), which is used to implement ScopedRunnableMethodFactory::empty(). Now, the WeakReferenceOwner just holds a pointer to the Flag class, and the Flag holds a back-pointer that it can use to clear the WeakReferenceOwner's pointer when the Flag is destroyed. I use the null'ness of this back-pointer in place of the bool member that was previously used to indicate if the WeakReference is valid. It was also necessary to expose a HasOneRef method on RefCounted. I included one on RefCountedThreadSafe for completeness. Finally, I switched HttpCache over to using WeakPtr instead of RevocableStore so that I could delete RevocableStore. (I'm making this change to consolidate similar functionality.) R=abarth BUG=none TEST=none Review URL: http://codereview.chromium.org/235027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27287 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/task.h')
-rw-r--r--base/task.h113
1 files changed, 31 insertions, 82 deletions
diff --git a/base/task.h b/base/task.h
index 447bfab..c827681 100644
--- a/base/task.h
+++ b/base/task.h
@@ -6,9 +6,9 @@
#define BASE_TASK_H_
#include "base/non_thread_safe.h"
-#include "base/revocable_store.h"
#include "base/tracked.h"
#include "base/tuple.h"
+#include "base/weak_ptr.h"
// Task ------------------------------------------------------------------------
//
@@ -71,73 +71,31 @@ class CancelableTask : public Task {
// }
// };
-// A ScopedTaskFactory produces tasks of type |TaskType| and prevents them from
-// running after it is destroyed.
-template<class TaskType>
-class ScopedTaskFactory : public RevocableStore {
- public:
- ScopedTaskFactory() { }
-
- // Create a new task.
- inline TaskType* NewTask() {
- return new TaskWrapper(this);
- }
-
- class TaskWrapper : public TaskType, public NonThreadSafe {
- public:
- explicit TaskWrapper(RevocableStore* store) : revocable_(store) { }
-
- virtual void Run() {
- if (!revocable_.revoked())
- TaskType::Run();
- }
-
- private:
- Revocable revocable_;
-
- DISALLOW_EVIL_CONSTRUCTORS(TaskWrapper);
- };
-
- private:
- DISALLOW_EVIL_CONSTRUCTORS(ScopedTaskFactory);
-};
-
// A ScopedRunnableMethodFactory creates runnable methods for a specified
// object. This is particularly useful for generating callbacks for
// non-reference counted objects when the factory is a member of the object.
template<class T>
-class ScopedRunnableMethodFactory : public RevocableStore {
+class ScopedRunnableMethodFactory {
public:
- explicit ScopedRunnableMethodFactory(T* object) : object_(object) { }
+ explicit ScopedRunnableMethodFactory(T* object) : weak_factory_(object) {
+ }
template <class Method>
inline Task* NewRunnableMethod(Method method) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple0> >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple());
- return task;
+ return new RunnableMethod<Method, Tuple0>(
+ weak_factory_.GetWeakPtr(), method, MakeTuple());
}
template <class Method, class A>
inline Task* NewRunnableMethod(Method method, const A& a) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple1<A> > >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple(a));
- return task;
+ return new RunnableMethod<Method, Tuple1<A> >(
+ weak_factory_.GetWeakPtr(), method, MakeTuple(a));
}
template <class Method, class A, class B>
inline Task* NewRunnableMethod(Method method, const A& a, const B& b) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple2<A, B> > >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple(a, b));
- return task;
+ return new RunnableMethod<Method, Tuple2<A, B> >(
+ weak_factory_.GetWeakPtr(), method, MakeTuple(a, b));
}
template <class Method, class A, class B, class C>
@@ -145,12 +103,8 @@ class ScopedRunnableMethodFactory : public RevocableStore {
const A& a,
const B& b,
const C& c) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple3<A, B, C> > >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple(a, b, c));
- return task;
+ return new RunnableMethod<Method, Tuple3<A, B, C> >(
+ weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c));
}
template <class Method, class A, class B, class C, class D>
@@ -159,12 +113,8 @@ class ScopedRunnableMethodFactory : public RevocableStore {
const B& b,
const C& c,
const D& d) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple4<A, B, C, D> > >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple(a, b, c, d));
- return task;
+ return new RunnableMethod<Method, Tuple4<A, B, C, D> >(
+ weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d));
}
template <class Method, class A, class B, class C, class D, class E>
@@ -174,40 +124,39 @@ class ScopedRunnableMethodFactory : public RevocableStore {
const C& c,
const D& d,
const E& e) {
- typedef typename ScopedTaskFactory<RunnableMethod<
- Method, Tuple5<A, B, C, D, E> > >::TaskWrapper TaskWrapper;
-
- TaskWrapper* task = new TaskWrapper(this);
- task->Init(object_, method, MakeTuple(a, b, c, d, e));
- return task;
+ return new RunnableMethod<Method, Tuple5<A, B, C, D, E> >(
+ weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d, e));
}
+ void RevokeAll() { weak_factory_.InvalidateWeakPtrs(); }
+
+ bool empty() const { return !weak_factory_.HasWeakPtrs(); }
+
protected:
template <class Method, class Params>
class RunnableMethod : public Task {
public:
- RunnableMethod() { }
-
- void Init(T* obj, Method meth, const Params& params) {
- obj_ = obj;
- meth_ = meth;
- params_ = params;
+ RunnableMethod(const base::WeakPtr<T>& obj, Method meth, const Params& params)
+ : obj_(obj),
+ meth_(meth),
+ params_(params) {
}
- virtual void Run() { DispatchToMethod(obj_, meth_, params_); }
+ virtual void Run() {
+ if (obj_)
+ DispatchToMethod(obj_.get(), meth_, params_);
+ }
private:
- T* obj_;
+ base::WeakPtr<T> obj_;
Method meth_;
Params params_;
- DISALLOW_EVIL_CONSTRUCTORS(RunnableMethod);
+ DISALLOW_COPY_AND_ASSIGN(RunnableMethod);
};
private:
- T* object_;
-
- DISALLOW_EVIL_CONSTRUCTORS(ScopedRunnableMethodFactory);
+ base::WeakPtrFactory<T> weak_factory_;
};
// General task implementations ------------------------------------------------