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

Unified Diff: base/callback.h.pump

Issue 6507029: Callback API Change: is_null, Reset, and Equals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: grammar Created 9 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 side-by-side diff with in-line comments
Download patch
Index: base/callback.h.pump
diff --git a/base/callback.h.pump b/base/callback.h.pump
index 9fc4b0b0ce9743e862defe7314fb6188919e4334..377647bbbfd0b3a80ba7b34085993a9ff3afc31f 100644
--- a/base/callback.h.pump
+++ b/base/callback.h.pump
@@ -216,6 +216,46 @@ $var MAX_ARITY = 6
namespace base {
+namespace internal {
+
+// Holds the methods that don't require specialization to reduce template bloat.
+class CallbackBase {
+ protected:
+ // In C++, it is safe to cast function pointers to function pointers of
+ // another type. It is not okay to use void*. We create a InvokeFuncStorage
+ // that that can store our function pointer, and then cast it back to
+ // the original type on usage.
+ typedef void(*InvokeFuncStorage)(void);
+
+ public:
+ explicit CallbackBase(InvokeFuncStorage polymorphic_invoke)
+ : polymorphic_invoke_(polymorphic_invoke) {
+ }
+
+ // Returns true if Callback is empty (doesn't refer to anything).
+ bool is_empty() const {
+ return invoker_storage_.get() == NULL;
+ }
+
+ // Returns the Callback into an empty state.
+ void reset() {
+ invoker_storage_ = NULL;
+ polymorphic_invoke_ = NULL;
+ }
+
+ bool Equals(const CallbackBase& other) const {
+ return invoker_storage_.get() == other.invoker_storage_.get() &&
+ polymorphic_invoke_ == other.polymorphic_invoke_;
+ }
+
+ protected:
+ scoped_refptr<InvokerStorageBase> invoker_storage_;
+ InvokeFuncStorage polymorphic_invoke_;
+};
+
+} // namespace internal
+
+
// First, we forward declare the Callback class template. This informs the
// compiler that the template only has 1 type parameter which is the function
// signature that the Callback is representing.
@@ -226,17 +266,16 @@ namespace base {
template <typename Sig>
class Callback;
-
$range ARITY 0..MAX_ARITY
$for ARITY [[
$range ARG 1..ARITY
$if ARITY == 0 [[
template <typename R>
-class Callback<R(void)> {
+class Callback<R(void)> : public internal::CallbackBase {
]] $else [[
template <typename R, $for ARG , [[typename A$(ARG)]]>
-class Callback<R($for ARG , [[A$(ARG)]])> {
+class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase {
]]
public:
@@ -245,7 +284,7 @@ $if ARITY != 0 [[, ]]
$for ARG ,
[[const A$(ARG)&]]);
- Callback() : polymorphic_invoke_(NULL) { }
+ Callback() : CallbackBase(NULL) { }
// We pass InvokerStorageHolder by const ref to avoid incurring an
// unnecessary AddRef/Unref pair even though we will modify the object.
@@ -256,27 +295,22 @@ $for ARG ,
// return the exact Callback<> type. See base/bind.h for details.
template <typename T>
Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
- : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) {
+ : CallbackBase(
+ reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke)) {
invoker_storage_.swap(invoker_holder.invoker_storage_);
}
-$if ARITY == 0 [[
- R Run(void) const {
-]] $else [[
R Run($for ARG ,
[[const A$(ARG)& a$(ARG)]]) const {
-]]
+ PolymorphicInvoke f =
+ reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
- return polymorphic_invoke_(invoker_storage_.get()[[]]
+ return f(invoker_storage_.get()[[]]
$if ARITY != 0 [[, ]]
$for ARG ,
- [[a$(ARG)]]);
+ [[a$(ARG)]]);
}
-
- private:
- scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
- PolymorphicInvoke polymorphic_invoke_;
};
« base/callback.h ('K') | « base/callback.h ('k') | base/callback_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698