OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_ | |
6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_ | |
7 | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/sequenced_task_runner.h" | |
12 | |
13 namespace base { | |
14 | |
15 // RefCountedDeleteOnTaskRunner is similar to RefCountedThreadSafe, and ensures | |
16 // that the object will be deleted on a specified task runner. | |
17 // | |
18 // Sample usage: | |
19 // class Foo : public RefCountedDeleteOnTaskRunner<Foo> { | |
20 // | |
21 // Foo(const scoped_refptr<SequencedTaskRunner>& task_runner) | |
22 // : RefCountedDeleteOnTaskRunner<Foo>(task_runner) { | |
23 // ... | |
24 // } | |
25 // ... | |
26 // private: | |
27 // friend class RefCountedDeleteOnTaskRunner<Foo>; | |
28 // friend class DeleteHelper<Foo>; | |
29 // | |
30 // ~Foo(); | |
31 // }; | |
32 | |
33 template <class T> | |
34 class RefCountedDeleteOnTaskRunner : public subtle::RefCountedThreadSafeBase { | |
35 public: | |
36 RefCountedDeleteOnTaskRunner( | |
awong
2014/07/07 21:50:00
explicit?
| |
37 const scoped_refptr<SequencedTaskRunner>& task_runner) | |
38 : task_runner_(task_runner) { | |
39 DCHECK(task_runner_.get()); | |
40 } | |
41 | |
42 void AddRef() const { subtle::RefCountedThreadSafeBase::AddRef(); } | |
43 | |
44 void Release() const { | |
45 if (subtle::RefCountedThreadSafeBase::Release()) | |
46 DestructOnTaskRunner(); | |
47 } | |
48 | |
49 protected: | |
50 friend class DeleteHelper<RefCountedDeleteOnTaskRunner>; | |
51 ~RefCountedDeleteOnTaskRunner() {} | |
52 | |
53 void DestructOnTaskRunner() const { | |
54 const T* t = static_cast<const T*>(this); | |
55 if (task_runner_->RunsTasksOnCurrentThread()) | |
56 delete t; | |
57 else | |
58 task_runner_->DeleteSoon(FROM_HERE, t); | |
59 } | |
60 | |
61 scoped_refptr<SequencedTaskRunner> task_runner_; | |
62 | |
63 private: | |
64 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnTaskRunner); | |
65 }; | |
66 | |
67 } // namespace base | |
68 | |
69 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_TASK_RUNNER_H_ | |
OLD | NEW |