Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: base/memory/ref_counted_delete_on_message_loop.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/ref_counted.cc ('k') | base/memory/ref_counted_memory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_MESSAGE_LOOP_H_
6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
7
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12
13 namespace base {
14
15 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures
16 // that the object will be deleted on a specified message loop.
17 //
18 // Sample usage:
19 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> {
20 //
21 // Foo(const scoped_refptr<SingleThreadTaskRunner>& loop)
22 // : RefCountedDeleteOnMessageLoop<Foo>(loop) {
23 // ...
24 // }
25 // ...
26 // private:
27 // friend class RefCountedDeleteOnMessageLoop<Foo>;
28 // friend class DeleteHelper<Foo>;
29 //
30 // ~Foo();
31 // };
32
33 // TODO(skyostil): Rename this to RefCountedDeleteOnTaskRunner.
34 template <class T>
35 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase {
36 public:
37 // This constructor will accept a MessageL00pProxy object, but new code should
38 // prefer a SingleThreadTaskRunner. A SingleThreadTaskRunner for the
39 // MessageLoop on the current thread can be acquired by calling
40 // MessageLoop::current()->task_runner().
41 RefCountedDeleteOnMessageLoop(
42 const scoped_refptr<SingleThreadTaskRunner>& task_runner)
43 : task_runner_(task_runner) {
44 DCHECK(task_runner_);
45 }
46
47 void AddRef() const {
48 subtle::RefCountedThreadSafeBase::AddRef();
49 }
50
51 void Release() const {
52 if (subtle::RefCountedThreadSafeBase::Release())
53 DestructOnMessageLoop();
54 }
55
56 protected:
57 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>;
58 ~RefCountedDeleteOnMessageLoop() {}
59
60 void DestructOnMessageLoop() const {
61 const T* t = static_cast<const T*>(this);
62 if (task_runner_->BelongsToCurrentThread())
63 delete t;
64 else
65 task_runner_->DeleteSoon(FROM_HERE, t);
66 }
67
68 scoped_refptr<SingleThreadTaskRunner> task_runner_;
69
70 private:
71 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop);
72 };
73
74 } // namespace base
75
76 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
OLDNEW
« no previous file with comments | « base/memory/ref_counted.cc ('k') | base/memory/ref_counted_memory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698