blob: 9dca02387dcef60e7afadf31fc232dadf1069a28 [file] [log] [blame]
willchan@chromium.org9be58392012-03-23 09:08:42 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains utility functions and classes that help the
6// implementation, and management of the Callback objects.
7
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +09008#ifndef BASE_CALLBACK_INTERNAL_H_
9#define BASE_CALLBACK_INTERNAL_H_
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090010
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090011#include <stddef.h>
12
darin@chromium.orge585bed2011-08-06 00:34:00 +090013#include "base/base_export.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090014#include "base/memory/ref_counted.h"
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090015#include "base/memory/scoped_ptr.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090016
willchan@chromium.org9be58392012-03-23 09:08:42 +090017template <typename T>
18class ScopedVector;
19
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090020namespace base {
21namespace internal {
22
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090023// BindStateBase is used to provide an opaque handle that the Callback
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090024// class can use to represent a function object with bound arguments. It
25// behaves as an existential type that is used by a corresponding
26// DoInvoke function to perform the function execution. This allows
27// us to shield the Callback class from the types of the bound argument via
28// "type erasure."
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090029class BindStateBase : public RefCountedThreadSafe<BindStateBase> {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090030 protected:
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090031 friend class RefCountedThreadSafe<BindStateBase>;
32 virtual ~BindStateBase() {}
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090033};
34
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090035// Holds the Callback methods that don't require specialization to reduce
36// template bloat.
darin@chromium.orge585bed2011-08-06 00:34:00 +090037class BASE_EXPORT CallbackBase {
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090038 public:
39 // Returns true if Callback is null (doesn't refer to anything).
ricea61846ae2014-09-17 11:12:53 +090040 bool is_null() const { return bind_state_.get() == NULL; }
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090041
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090042 // Returns the Callback into an uninitialized state.
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090043 void Reset();
44
ajwong@chromium.org3bc50ce2011-09-10 12:14:35 +090045 protected:
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090046 // In C++, it is safe to cast function pointers to function pointers of
47 // another type. It is not okay to use void*. We create a InvokeFuncStorage
48 // that that can store our function pointer, and then cast it back to
49 // the original type on usage.
50 typedef void(*InvokeFuncStorage)(void);
51
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090052 // Returns true if this callback equals |other|. |other| may be null.
53 bool Equals(const CallbackBase& other) const;
54
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090055 // Allow initializing of |bind_state_| via the constructor to avoid default
56 // initialization of the scoped_refptr. We do not also initialize
57 // |polymorphic_invoke_| here because doing a normal assignment in the
58 // derived Callback templates makes for much nicer compiler errors.
59 explicit CallbackBase(BindStateBase* bind_state);
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090060
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090061 // Force the destructor to be instantiated inside this translation unit so
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090062 // that our subclasses will not get inlined versions. Avoids more template
63 // bloat.
64 ~CallbackBase();
65
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090066 scoped_refptr<BindStateBase> bind_state_;
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090067 InvokeFuncStorage polymorphic_invoke_;
68};
69
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090070// A helper template to determine if given type is non-const move-only-type,
71// i.e. if a value of the given type should be passed via .Pass() in a
72// destructive way.
73template <typename T> struct IsMoveOnlyType {
74 template <typename U>
75 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
76
77 template <typename U>
78 static NoType Test(...);
79
brucedawson137c1532014-10-29 07:18:04 +090080 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) &&
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090081 !is_const<T>::value;
82};
83
tzikf9c38192014-11-20 10:08:20 +090084// Returns |Then| as SelectType::Type if |condition| is true. Otherwise returns
85// |Else|.
86template <bool condition, typename Then, typename Else>
87struct SelectType {
88 typedef Then Type;
89};
90
91template <typename Then, typename Else>
92struct SelectType<false, Then, Else> {
93 typedef Else Type;
94};
95
96template <typename>
97struct CallbackParamTraitsForMoveOnlyType;
98
99template <typename>
100struct CallbackParamTraitsForNonMoveOnlyType;
101
102// TODO(tzik): Use a default parameter once MSVS supports variadic templates
103// with default values.
104// http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilation-error-with-variadic-templates
105//
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900106// This is a typetraits object that's used to take an argument type, and
107// extract a suitable type for storing and forwarding arguments.
108//
109// In particular, it strips off references, and converts arrays to
110// pointers for storage; and it avoids accidentally trying to create a
111// "reference of a reference" if the argument is a reference type.
112//
113// This array type becomes an issue for storage because we are passing bound
114// parameters by const reference. In this case, we end up passing an actual
115// array type in the initializer list which C++ does not allow. This will
116// break passing of C-string literals.
tzikf9c38192014-11-20 10:08:20 +0900117template <typename T>
118struct CallbackParamTraits
119 : SelectType<IsMoveOnlyType<T>::value,
120 CallbackParamTraitsForMoveOnlyType<T>,
121 CallbackParamTraitsForNonMoveOnlyType<T> >::Type {
122};
123
124template <typename T>
125struct CallbackParamTraitsForNonMoveOnlyType {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900126 typedef const T& ForwardType;
127 typedef T StorageType;
128};
129
130// The Storage should almost be impossible to trigger unless someone manually
131// specifies type of the bind parameters. However, in case they do,
132// this will guard against us accidentally storing a reference parameter.
133//
134// The ForwardType should only be used for unbound arguments.
135template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900136struct CallbackParamTraitsForNonMoveOnlyType<T&> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900137 typedef T& ForwardType;
138 typedef T StorageType;
139};
140
141// Note that for array types, we implicitly add a const in the conversion. This
142// means that it is not possible to bind array arguments to functions that take
143// a non-const pointer. Trying to specialize the template based on a "const
144// T[n]" does not seem to match correctly, so we are stuck with this
145// restriction.
146template <typename T, size_t n>
tzikf9c38192014-11-20 10:08:20 +0900147struct CallbackParamTraitsForNonMoveOnlyType<T[n]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900148 typedef const T* ForwardType;
149 typedef const T* StorageType;
150};
151
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900152// See comment for CallbackParamTraits<T[n]>.
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900153template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900154struct CallbackParamTraitsForNonMoveOnlyType<T[]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900155 typedef const T* ForwardType;
156 typedef const T* StorageType;
157};
158
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900159// Parameter traits for movable-but-not-copyable scopers.
160//
161// Callback<>/Bind() understands movable-but-not-copyable semantics where
162// the type cannot be copied but can still have its state destructively
163// transferred (aka. moved) to another instance of the same type by calling a
164// helper function. When used with Bind(), this signifies transferal of the
165// object's state to the target function.
166//
167// For these types, the ForwardType must not be a const reference, or a
168// reference. A const reference is inappropriate, and would break const
169// correctness, because we are implementing a destructive move. A non-const
170// reference cannot be used with temporaries which means the result of a
171// function or a cast would not be usable with Callback<> or Bind().
willchan@chromium.org9be58392012-03-23 09:08:42 +0900172template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900173struct CallbackParamTraitsForMoveOnlyType {
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900174 typedef T ForwardType;
175 typedef T StorageType;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900176};
177
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900178// CallbackForward() is a very limited simulation of C++11's std::forward()
179// used by the Callback/Bind system for a set of movable-but-not-copyable
180// types. It is needed because forwarding a movable-but-not-copyable
181// argument to another function requires us to invoke the proper move
182// operator to create a rvalue version of the type. The supported types are
183// whitelisted below as overloads of the CallbackForward() function. The
184// default template compiles out to be a no-op.
185//
186// In C++11, std::forward would replace all uses of this function. However, it
187// is impossible to implement a general std::forward with C++11 due to a lack
188// of rvalue references.
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900189//
190// In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to
191// simulate std::forward() and forward the result of one Callback as a
192// parameter to another callback. This is to support Callbacks that return
193// the movable-but-not-copyable types whitelisted above.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900194template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900195typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) {
196 return t;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900197}
198
199template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900200typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) {
201 return t.Pass();
202}
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900203
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900204} // namespace internal
205} // namespace base
206
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +0900207#endif // BASE_CALLBACK_INTERNAL_H_