From 59eff91b6b042f8389c4af756968a93407bf7c24 Mon Sep 17 00:00:00 2001 From: "ajwong@chromium.org" Date: Fri, 18 Feb 2011 23:29:31 +0000 Subject: Callback: De-inline CallbackBase, and move to callback_helpers -> callback_internal.h We can re-inline later if it starts being an issue. BUG=none TEST=unit-tests Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=75443 Review URL: http://codereview.chromium.org/6542026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75464 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.gypi | 3 +- base/bind.h | 2 +- base/bind.h.pump | 2 +- base/bind_internal.h | 2 +- base/bind_internal.h.pump | 2 +- base/callback.h | 45 +------------------------ base/callback.h.pump | 45 +------------------------ base/callback_helpers.h | 55 ------------------------------ base/callback_internal.cc | 36 ++++++++++++++++++++ base/callback_internal.h | 86 +++++++++++++++++++++++++++++++++++++++++++++++ base/callback_unittest.cc | 2 +- 11 files changed, 131 insertions(+), 149 deletions(-) delete mode 100644 base/callback_helpers.h create mode 100644 base/callback_internal.cc create mode 100644 base/callback_internal.h (limited to 'base') diff --git a/base/base.gypi b/base/base.gypi index 79ba5f4..13cb1ba 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -42,7 +42,8 @@ 'bits.h', 'bzip2_error_handler.cc', 'callback.h', - 'callback_helpers.h', + 'callback_internal.cc', + 'callback_internal.h', 'callback_old.h', 'command_line.cc', 'command_line.h', diff --git a/base/bind.h b/base/bind.h index c23af2e..cd9eb19 100644 --- a/base/bind.h +++ b/base/bind.h @@ -12,7 +12,7 @@ #pragma once #include "base/bind_internal.h" -#include "base/callback_helpers.h" +#include "base/callback_internal.h" // See base/callback.h for how to use these functions. // diff --git a/base/bind.h.pump b/base/bind.h.pump index fc7f246..62b313f 100644 --- a/base/bind.h.pump +++ b/base/bind.h.pump @@ -16,7 +16,7 @@ $var MAX_ARITY = 6 #pragma once #include "base/bind_internal.h" -#include "base/callback_helpers.h" +#include "base/callback_internal.h" // See base/callback.h for how to use these functions. // diff --git a/base/bind_internal.h b/base/bind_internal.h index 62f2050..dd8afde 100644 --- a/base/bind_internal.h +++ b/base/bind_internal.h @@ -12,7 +12,7 @@ #pragma once #include "base/bind_helpers.h" -#include "base/callback_helpers.h" +#include "base/callback_internal.h" #include "base/template_util.h" namespace base { diff --git a/base/bind_internal.h.pump b/base/bind_internal.h.pump index 132b0db..6fd95fe 100644 --- a/base/bind_internal.h.pump +++ b/base/bind_internal.h.pump @@ -16,7 +16,7 @@ $var MAX_ARITY = 6 #pragma once #include "base/bind_helpers.h" -#include "base/callback_helpers.h" +#include "base/callback_internal.h" #include "base/template_util.h" namespace base { diff --git a/base/callback.h b/base/callback.h index 9e43afb..9c76aa5 100644 --- a/base/callback.h +++ b/base/callback.h @@ -11,7 +11,7 @@ #define BASE_CALLBACK_H_ #pragma once -#include "base/callback_helpers.h" +#include "base/callback_internal.h" #include "base/callback_old.h" // New, super-duper, unified Callback system. This will eventually replace @@ -212,49 +212,6 @@ namespace base { -namespace internal { - -// Holds the methods that don't require specialization to reduce template bloat. -class CallbackBase { - public: - // Returns true if Callback is null (doesn't refer to anything). - bool is_null() const { - return invoker_storage_.get() == NULL; - } - - // Returns the Callback into an uninitalized 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: - // 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); - - CallbackBase(InvokeFuncStorage polymorphic_invoke, - scoped_refptr* invoker_storage) - : polymorphic_invoke_(polymorphic_invoke) { - if (invoker_storage) { - invoker_storage_.swap(*invoker_storage); - } - } - - scoped_refptr 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. diff --git a/base/callback.h.pump b/base/callback.h.pump index 19b7987..2efe0c5 100644 --- a/base/callback.h.pump +++ b/base/callback.h.pump @@ -15,7 +15,7 @@ $var MAX_ARITY = 6 #define BASE_CALLBACK_H_ #pragma once -#include "base/callback_helpers.h" +#include "base/callback_internal.h" #include "base/callback_old.h" // New, super-duper, unified Callback system. This will eventually replace @@ -216,49 +216,6 @@ $var MAX_ARITY = 6 namespace base { -namespace internal { - -// Holds the methods that don't require specialization to reduce template bloat. -class CallbackBase { - public: - // Returns true if Callback is null (doesn't refer to anything). - bool is_null() const { - return invoker_storage_.get() == NULL; - } - - // Returns the Callback into an uninitalized 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: - // 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); - - CallbackBase(InvokeFuncStorage polymorphic_invoke, - scoped_refptr* invoker_storage) - : polymorphic_invoke_(polymorphic_invoke) { - if (invoker_storage) { - invoker_storage_.swap(*invoker_storage); - } - } - - scoped_refptr 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. diff --git a/base/callback_helpers.h b/base/callback_helpers.h deleted file mode 100644 index 86b0df1..0000000 --- a/base/callback_helpers.h +++ /dev/null @@ -1,55 +0,0 @@ -// 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. - -// This file contains utility functions and classes that help the -// implementation, and management of the Callback objects. - -#ifndef BASE_CALLBACK_HELPERS_H_ -#define BASE_CALLBACK_HELPERS_H_ -#pragma once - -#include "base/ref_counted.h" - -namespace base { -namespace internal { - -// InvokerStorageBase is used to provide an opaque handle that the Callback -// class can use to represent a function object with bound arguments. It -// behaves as an existential type that is used by a corresponding -// DoInvoke function to perform the function execution. This allows -// us to shield the Callback class from the types of the bound argument via -// "type erasure." -class InvokerStorageBase : public RefCountedThreadSafe { - protected: - friend class RefCountedThreadSafe; - virtual ~InvokerStorageBase() {} -}; - -// This structure exists purely to pass the returned |invoker_storage_| from -// Bind() to Callback while avoiding an extra AddRef/Release() pair. -// -// To do this, the constructor of Callback<> must take a const-ref. The -// reference must be to a const object otherwise the compiler will emit a -// warning about taking a reference to a temporary. -// -// Unfortunately, this means that the internal |invoker_storage_| field must -// be made mutable. -template -struct InvokerStorageHolder { - explicit InvokerStorageHolder(T* invoker_storage) - : invoker_storage_(invoker_storage) { - } - - mutable scoped_refptr invoker_storage_; -}; - -template -InvokerStorageHolder MakeInvokerStorageHolder(T* o) { - return InvokerStorageHolder(o); -} - -} // namespace internal -} // namespace base - -#endif // BASE_CALLBACK_HELPERS_H_ diff --git a/base/callback_internal.cc b/base/callback_internal.cc new file mode 100644 index 0000000..d9d1e6f --- /dev/null +++ b/base/callback_internal.cc @@ -0,0 +1,36 @@ +// 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. + +#include "base/callback_internal.h" + +namespace base { +namespace internal { + +bool CallbackBase::is_null() const { + return invoker_storage_.get() == NULL; +} + +void CallbackBase::Reset() { + invoker_storage_ = NULL; + polymorphic_invoke_ = NULL; +} + +bool CallbackBase::Equals(const CallbackBase& other) const { + return invoker_storage_.get() == other.invoker_storage_.get() && + polymorphic_invoke_ == other.polymorphic_invoke_; +} + +CallbackBase::CallbackBase(InvokeFuncStorage polymorphic_invoke, + scoped_refptr* invoker_storage) + : polymorphic_invoke_(polymorphic_invoke) { + if (invoker_storage) { + invoker_storage_.swap(*invoker_storage); + } +} + +CallbackBase::~CallbackBase() { +} + +} // namespace base +} // namespace internal diff --git a/base/callback_internal.h b/base/callback_internal.h new file mode 100644 index 0000000..4f1d3c3 --- /dev/null +++ b/base/callback_internal.h @@ -0,0 +1,86 @@ +// 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. + +// This file contains utility functions and classes that help the +// implementation, and management of the Callback objects. + +#ifndef BASE_CALLBACK_INTERNAL_H_ +#define BASE_CALLBACK_INTERNAL_H_ +#pragma once + +#include "base/ref_counted.h" + +namespace base { +namespace internal { + +// InvokerStorageBase is used to provide an opaque handle that the Callback +// class can use to represent a function object with bound arguments. It +// behaves as an existential type that is used by a corresponding +// DoInvoke function to perform the function execution. This allows +// us to shield the Callback class from the types of the bound argument via +// "type erasure." +class InvokerStorageBase : public RefCountedThreadSafe { + protected: + friend class RefCountedThreadSafe; + virtual ~InvokerStorageBase() {} +}; + +// This structure exists purely to pass the returned |invoker_storage_| from +// Bind() to Callback while avoiding an extra AddRef/Release() pair. +// +// To do this, the constructor of Callback<> must take a const-ref. The +// reference must be to a const object otherwise the compiler will emit a +// warning about taking a reference to a temporary. +// +// Unfortunately, this means that the internal |invoker_storage_| field must +// be made mutable. +template +struct InvokerStorageHolder { + explicit InvokerStorageHolder(T* invoker_storage) + : invoker_storage_(invoker_storage) { + } + + mutable scoped_refptr invoker_storage_; +}; + +template +InvokerStorageHolder MakeInvokerStorageHolder(T* o) { + return InvokerStorageHolder(o); +} + +// Holds the Callback methods that don't require specialization to reduce +// template bloat. +class CallbackBase { + public: + // Returns true if Callback is null (doesn't refer to anything). + bool is_null() const; + + // Returns the Callback into an uninitalized state. + void Reset(); + + bool Equals(const CallbackBase& other) const; + + 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); + + CallbackBase(InvokeFuncStorage polymorphic_invoke, + scoped_refptr* invoker_storage); + + // Force the destructor to be instaniated inside this translation unit so + // that our subclasses will not get inlined versions. Avoids more template + // bloat. + ~CallbackBase(); + + scoped_refptr invoker_storage_; + InvokeFuncStorage polymorphic_invoke_; +}; + +} // namespace internal +} // namespace base + +#endif // BASE_CALLBACK_INTERNAL_H_ diff --git a/base/callback_unittest.cc b/base/callback_unittest.cc index bf2b606..e41b399 100644 --- a/base/callback_unittest.cc +++ b/base/callback_unittest.cc @@ -3,7 +3,7 @@ // found in the LICENSE file. #include "base/callback.h" -#include "base/callback_helpers.h" +#include "base/callback_internal.h" #include "base/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" -- cgit v1.1