Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / memory / ref_counted_delete_on_message_loop.h
blob7b898ac00dd015d6b8faa0bc0de665bad2346ca6
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.
5 #ifndef BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
6 #define BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop_proxy.h"
13 namespace base {
15 // RefCountedDeleteOnMessageLoop is similar to RefCountedThreadSafe, and ensures
16 // that the object will be deleted on a specified message loop.
18 // Sample usage:
19 // class Foo : public RefCountedDeleteOnMessageLoop<Foo> {
21 // Foo(const scoped_refptr<MessageLoopProxy>& loop)
22 // : RefCountedDeleteOnMessageLoop<Foo>(loop) {
23 // ...
24 // }
25 // ...
26 // private:
27 // friend class RefCountedDeleteOnMessageLoop<Foo>;
28 // friend class DeleteHelper<Foo>;
30 // ~Foo();
31 // };
33 template <class T>
34 class RefCountedDeleteOnMessageLoop : public subtle::RefCountedThreadSafeBase {
35 public:
36 RefCountedDeleteOnMessageLoop(
37 const scoped_refptr<MessageLoopProxy>& proxy) : proxy_(proxy) {
38 DCHECK(proxy_.get());
41 void AddRef() const {
42 subtle::RefCountedThreadSafeBase::AddRef();
45 void Release() const {
46 if (subtle::RefCountedThreadSafeBase::Release())
47 DestructOnMessageLoop();
50 protected:
51 friend class DeleteHelper<RefCountedDeleteOnMessageLoop>;
52 ~RefCountedDeleteOnMessageLoop() {}
54 void DestructOnMessageLoop() const {
55 const T* t = static_cast<const T*>(this);
56 if (proxy_->BelongsToCurrentThread())
57 delete t;
58 else
59 proxy_->DeleteSoon(FROM_HERE, t);
62 scoped_refptr<MessageLoopProxy> proxy_;
64 private:
65 DISALLOW_COPY_AND_ASSIGN(RefCountedDeleteOnMessageLoop);
68 } // namespace base
70 #endif // BASE_MEMORY_REF_COUNTED_DELETE_ON_MESSAGE_LOOP_H_