diff options
author | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-30 04:32:58 +0000 |
---|---|---|
committer | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-30 04:32:58 +0000 |
commit | b1d183282176bddba847c024ef111b4795046668 (patch) | |
tree | 939afc09384b365411d7d049d4ff0a73d6c98d41 /base | |
parent | b4bd6c9c76e0d5f95028dd563e0e948fe6d9095b (diff) | |
download | chromium_src-b1d183282176bddba847c024ef111b4795046668.zip chromium_src-b1d183282176bddba847c024ef111b4795046668.tar.gz chromium_src-b1d183282176bddba847c024ef111b4795046668.tar.bz2 |
Replace MessageLoop::DeleteSoon implementation with one that uses base::Bind.
BUG=none
TEST=none
TBR=willchan,brettw,tony
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=115997
Review URL: http://codereview.chromium.org/9004051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116026 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/message_loop.cc | 6 | ||||
-rw-r--r-- | base/message_loop.h | 10 | ||||
-rw-r--r-- | base/message_loop_helpers.h | 73 | ||||
-rw-r--r-- | base/message_loop_proxy.cc | 7 | ||||
-rw-r--r-- | base/message_loop_proxy.h | 11 |
5 files changed, 104 insertions, 3 deletions
diff --git a/base/message_loop.cc b/base/message_loop.cc index 32b4061..10be720 100644 --- a/base/message_loop.cc +++ b/base/message_loop.cc @@ -770,6 +770,12 @@ bool MessageLoop::DoIdleWork() { return false; } +void MessageLoop::DeleteSoonInternal(const tracked_objects::Location& from_here, + void(*deleter)(const void*), + const void* object) { + PostNonNestableTask(from_here, base::Bind(deleter, object)); +} + //------------------------------------------------------------------------------ // MessageLoop::AutoRunState diff --git a/base/message_loop.h b/base/message_loop.h index 18333e2..573e712 100644 --- a/base/message_loop.h +++ b/base/message_loop.h @@ -14,6 +14,7 @@ #include "base/callback_forward.h" #include "base/location.h" #include "base/memory/ref_counted.h" +#include "base/message_loop_helpers.h" #include "base/message_loop_proxy.h" #include "base/message_pump.h" #include "base/observer_list.h" @@ -228,7 +229,8 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { // from RefCountedThreadSafe<T>! template <class T> void DeleteSoon(const tracked_objects::Location& from_here, const T* object) { - PostNonNestableTask(from_here, new DeleteTask<T>(object)); + base::subtle::DeleteHelperInternal<T, void>::DeleteOnMessageLoop( + this, from_here, object); } // A variant on PostTask that releases the given reference counted object @@ -552,6 +554,12 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; private: + template <class T, class R> friend class base::subtle::DeleteHelperInternal; + + void DeleteSoonInternal(const tracked_objects::Location& from_here, + void(*deleter)(const void*), + const void* object); + DISALLOW_COPY_AND_ASSIGN(MessageLoop); }; diff --git a/base/message_loop_helpers.h b/base/message_loop_helpers.h new file mode 100644 index 0000000..151128c --- /dev/null +++ b/base/message_loop_helpers.h @@ -0,0 +1,73 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_MESSAGE_LOOP_HELPERS_H_ +#define BASE_MESSAGE_LOOP_HELPERS_H_ +#pragma once + +#include "base/basictypes.h" + +namespace tracked_objects { +class Location; +} + +namespace base { + +namespace subtle { +template <class T, class R> class DeleteHelperInternal; +} + +// This is a template helper which uses a function indirection to erase T from +// the function signature while still remembering it so we can call the correct +// destructor. We use this trick so we don't need to include bind.h in a +// header file. We also embed the function in a class to make it easier for +// users of DeleteSoon to declare the helper as a friend. +template <class T> +class DeleteHelper { + private: + // TODO(dcheng): Move the return type back to a function template parameter. + template <class T2, class R> friend class subtle::DeleteHelperInternal; + + static void DoDelete(const void* object) { + delete reinterpret_cast<const T*>(object); + } + + DISALLOW_COPY_AND_ASSIGN(DeleteHelper); +}; + +namespace subtle { + +// An internal MessageLoop-like class helper for DeleteHelper. We don't want to +// expose DoDelete directly since the void* argument makes it possible to pass +// an object of the wrong type to delete. Instead, we force callers to go +// through DeleteHelperInternal for type safety. MessageLoop-like classes which +// expose a DeleteSoon method should friend this class and implement a +// DeleteSoonInternal method with the following signature: +// bool(const tracked_objects::Location&, +// void(*deleter)(const void*), +// void* object) +// An implementation of DeleteSoonInternal should simply create a base::Closure +// from (deleter, object) and return the result of posting the task. +template <class T, class ReturnType> +class DeleteHelperInternal { + public: + template <class MessageLoopType> + static ReturnType DeleteOnMessageLoop( + MessageLoopType* message_loop, + const tracked_objects::Location& from_here, + const T* object) { + return message_loop->DeleteSoonInternal(from_here, + &DeleteHelper<T>::DoDelete, + object); + } + + private: + DISALLOW_COPY_AND_ASSIGN(DeleteHelperInternal); +}; + +} // namespace subtle + +} // namespace base + +#endif // BASE_MESSAGE_LOOP_HELPERS_H_ diff --git a/base/message_loop_proxy.cc b/base/message_loop_proxy.cc index f9b4ad8..63837d7 100644 --- a/base/message_loop_proxy.cc +++ b/base/message_loop_proxy.cc @@ -49,4 +49,11 @@ void MessageLoopProxy::OnDestruct() const { delete this; } +bool MessageLoopProxy::DeleteSoonInternal( + const tracked_objects::Location& from_here, + void(*deleter)(const void*), + const void* object) { + return PostNonNestableTask(from_here, base::Bind(deleter, object)); +} + } // namespace base diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h index aac11b9..738f39f 100644 --- a/base/message_loop_proxy.h +++ b/base/message_loop_proxy.h @@ -10,6 +10,7 @@ #include "base/basictypes.h" #include "base/callback_forward.h" #include "base/memory/ref_counted.h" +#include "base/message_loop_helpers.h" #include "base/task.h" namespace tracked_objects { @@ -121,8 +122,9 @@ class BASE_EXPORT MessageLoopProxy template <class T> bool DeleteSoon(const tracked_objects::Location& from_here, - T* object) { - return PostNonNestableTask(from_here, new DeleteTask<T>(object)); + const T* object) { + return base::subtle::DeleteHelperInternal<T, bool>::DeleteOnMessageLoop( + this, from_here, object); } template <class T> bool ReleaseSoon(const tracked_objects::Location& from_here, @@ -135,6 +137,7 @@ class BASE_EXPORT MessageLoopProxy static scoped_refptr<MessageLoopProxy> current(); protected: + template <class T, class R> friend class subtle::DeleteHelperInternal; friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; friend struct MessageLoopProxyTraits; @@ -144,6 +147,10 @@ class BASE_EXPORT MessageLoopProxy // Called when the proxy is about to be deleted. Subclasses can override this // to provide deletion on specific threads. virtual void OnDestruct() const; + + bool DeleteSoonInternal(const tracked_objects::Location& from_here, + void(*deleter)(const void*), + const void* object); }; struct MessageLoopProxyTraits { |