diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-24 02:02:17 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-24 02:02:17 +0000 |
commit | c18b1056be790c55f10e407a1f6a3706bff7df03 (patch) | |
tree | 7ed333791fc8dfeaef29c07307e8efcfa1b44fba /base/callback_internal.h | |
parent | b2e900f755ea4e66b5f41413716ecb55720e354d (diff) | |
download | chromium_src-c18b1056be790c55f10e407a1f6a3706bff7df03.zip chromium_src-c18b1056be790c55f10e407a1f6a3706bff7df03.tar.gz chromium_src-c18b1056be790c55f10e407a1f6a3706bff7df03.tar.bz2 |
Callback support for unbound reference and array arguments.
Because the callback object uses const An& for the type of the Run() function in argument forwarding, the code breaks for An=T& or An=T[]. This CL adds in code to modify the parameter type to remove duplicate references, and other fun.
BUG=35223
TEST=new unittests
Review URL: http://codereview.chromium.org/6718021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79239 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback_internal.h')
-rw-r--r-- | base/callback_internal.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/base/callback_internal.h b/base/callback_internal.h index 4f1d3c3..6b8fdff2 100644 --- a/base/callback_internal.h +++ b/base/callback_internal.h @@ -80,6 +80,52 @@ class CallbackBase { InvokeFuncStorage polymorphic_invoke_; }; +// This is a typetraits object that's used to take an argument type, and +// extract a suitable type for storing and forwarding arguments. +// +// In particular, it strips off references, and converts arrays to +// pointers for storage; and it avoids accidentally trying to create a +// "reference of a reference" if the argument is a reference type. +// +// This array type becomes an issue for storage because we are passing bound +// parameters by const reference. In this case, we end up passing an actual +// array type in the initializer list which C++ does not allow. This will +// break passing of C-string literals. +template <typename T> +struct ParamTraits { + typedef const T& ForwardType; + typedef T StorageType; +}; + +// The Storage should almost be impossible to trigger unless someone manually +// specifies type of the bind parameters. However, in case they do, +// this will guard against us accidentally storing a reference parameter. +// +// The ForwardType should only be used for unbound arguments. +template <typename T> +struct ParamTraits<T&> { + typedef T& ForwardType; + typedef T StorageType; +}; + +// Note that for array types, we implicitly add a const in the conversion. This +// means that it is not possible to bind array arguments to functions that take +// a non-const pointer. Trying to specialize the template based on a "const +// T[n]" does not seem to match correctly, so we are stuck with this +// restriction. +template <typename T, size_t n> +struct ParamTraits<T[n]> { + typedef const T* ForwardType; + typedef const T* StorageType; +}; + +// See comment for ParamTraits<T[n]>. +template <typename T> +struct ParamTraits<T[]> { + typedef const T* ForwardType; + typedef const T* StorageType; +}; + } // namespace internal } // namespace base |