blob: 4b47bc7978db33f0946a9c30645d4b35f241751e [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>
vmpstr495da402015-11-18 17:43:26 +090012#include <type_traits>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090013
tapted9cef4212015-05-14 17:03:32 +090014#include "base/atomic_ref_count.h"
darin@chromium.orge585bed2011-08-06 00:34:00 +090015#include "base/base_export.h"
tapted9cef4212015-05-14 17:03:32 +090016#include "base/macros.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090017#include "base/memory/ref_counted.h"
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090018#include "base/memory/scoped_ptr.h"
tapted9cef4212015-05-14 17:03:32 +090019#include "base/template_util.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090020
21namespace base {
22namespace internal {
tapted9cef4212015-05-14 17:03:32 +090023class CallbackBase;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090024
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090025// BindStateBase is used to provide an opaque handle that the Callback
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090026// class can use to represent a function object with bound arguments. It
27// behaves as an existential type that is used by a corresponding
28// DoInvoke function to perform the function execution. This allows
29// us to shield the Callback class from the types of the bound argument via
30// "type erasure."
tapted9cef4212015-05-14 17:03:32 +090031// At the base level, the only task is to add reference counting data. Don't use
32// RefCountedThreadSafe since it requires the destructor to be a virtual method.
33// Creating a vtable for every BindState template instantiation results in a lot
34// of bloat. Its only task is to call the destructor which can be done with a
35// function pointer.
36class BindStateBase {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090037 protected:
tapted9cef4212015-05-14 17:03:32 +090038 explicit BindStateBase(void (*destructor)(BindStateBase*))
39 : ref_count_(0), destructor_(destructor) {}
40 ~BindStateBase() = default;
41
42 private:
43 friend class scoped_refptr<BindStateBase>;
44 friend class CallbackBase;
45
46 void AddRef();
47 void Release();
48
49 AtomicRefCount ref_count_;
50
51 // Pointer to a function that will properly destroy |this|.
52 void (*destructor_)(BindStateBase*);
53
54 DISALLOW_COPY_AND_ASSIGN(BindStateBase);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090055};
56
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090057// Holds the Callback methods that don't require specialization to reduce
58// template bloat.
darin@chromium.orge585bed2011-08-06 00:34:00 +090059class BASE_EXPORT CallbackBase {
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090060 public:
dcheng60283a82014-11-26 14:04:55 +090061 CallbackBase(const CallbackBase& c);
62 CallbackBase& operator=(const CallbackBase& c);
63
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090064 // Returns true if Callback is null (doesn't refer to anything).
ricea61846ae2014-09-17 11:12:53 +090065 bool is_null() const { return bind_state_.get() == NULL; }
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090066
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090067 // Returns the Callback into an uninitialized state.
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090068 void Reset();
69
ajwong@chromium.org3bc50ce2011-09-10 12:14:35 +090070 protected:
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090071 // In C++, it is safe to cast function pointers to function pointers of
72 // another type. It is not okay to use void*. We create a InvokeFuncStorage
73 // that that can store our function pointer, and then cast it back to
74 // the original type on usage.
75 typedef void(*InvokeFuncStorage)(void);
76
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090077 // Returns true if this callback equals |other|. |other| may be null.
78 bool Equals(const CallbackBase& other) const;
79
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090080 // Allow initializing of |bind_state_| via the constructor to avoid default
81 // initialization of the scoped_refptr. We do not also initialize
82 // |polymorphic_invoke_| here because doing a normal assignment in the
83 // derived Callback templates makes for much nicer compiler errors.
84 explicit CallbackBase(BindStateBase* bind_state);
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090085
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090086 // Force the destructor to be instantiated inside this translation unit so
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090087 // that our subclasses will not get inlined versions. Avoids more template
88 // bloat.
89 ~CallbackBase();
90
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090091 scoped_refptr<BindStateBase> bind_state_;
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090092 InvokeFuncStorage polymorphic_invoke_;
93};
94
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090095// A helper template to determine if given type is non-const move-only-type,
96// i.e. if a value of the given type should be passed via .Pass() in a
97// destructive way.
98template <typename T> struct IsMoveOnlyType {
99 template <typename U>
100 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
101
102 template <typename U>
103 static NoType Test(...);
104
brucedawson137c1532014-10-29 07:18:04 +0900105 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) &&
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900106 !is_const<T>::value;
107};
108
tzikf9c38192014-11-20 10:08:20 +0900109template <typename>
110struct CallbackParamTraitsForMoveOnlyType;
111
112template <typename>
113struct CallbackParamTraitsForNonMoveOnlyType;
114
115// TODO(tzik): Use a default parameter once MSVS supports variadic templates
116// with default values.
117// http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilation-error-with-variadic-templates
118//
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900119// This is a typetraits object that's used to take an argument type, and
120// extract a suitable type for storing and forwarding arguments.
121//
122// In particular, it strips off references, and converts arrays to
123// pointers for storage; and it avoids accidentally trying to create a
124// "reference of a reference" if the argument is a reference type.
125//
126// This array type becomes an issue for storage because we are passing bound
127// parameters by const reference. In this case, we end up passing an actual
128// array type in the initializer list which C++ does not allow. This will
129// break passing of C-string literals.
tzikf9c38192014-11-20 10:08:20 +0900130template <typename T>
131struct CallbackParamTraits
vmpstr495da402015-11-18 17:43:26 +0900132 : std::conditional<IsMoveOnlyType<T>::value,
tzikf9c38192014-11-20 10:08:20 +0900133 CallbackParamTraitsForMoveOnlyType<T>,
vmpstr495da402015-11-18 17:43:26 +0900134 CallbackParamTraitsForNonMoveOnlyType<T>>::type {
tzikf9c38192014-11-20 10:08:20 +0900135};
136
137template <typename T>
138struct CallbackParamTraitsForNonMoveOnlyType {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900139 typedef const T& ForwardType;
140 typedef T StorageType;
141};
142
143// The Storage should almost be impossible to trigger unless someone manually
144// specifies type of the bind parameters. However, in case they do,
145// this will guard against us accidentally storing a reference parameter.
146//
147// The ForwardType should only be used for unbound arguments.
148template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900149struct CallbackParamTraitsForNonMoveOnlyType<T&> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900150 typedef T& ForwardType;
151 typedef T StorageType;
152};
153
154// Note that for array types, we implicitly add a const in the conversion. This
155// means that it is not possible to bind array arguments to functions that take
156// a non-const pointer. Trying to specialize the template based on a "const
157// T[n]" does not seem to match correctly, so we are stuck with this
158// restriction.
159template <typename T, size_t n>
tzikf9c38192014-11-20 10:08:20 +0900160struct CallbackParamTraitsForNonMoveOnlyType<T[n]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900161 typedef const T* ForwardType;
162 typedef const T* StorageType;
163};
164
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900165// See comment for CallbackParamTraits<T[n]>.
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900166template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900167struct CallbackParamTraitsForNonMoveOnlyType<T[]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900168 typedef const T* ForwardType;
169 typedef const T* StorageType;
170};
171
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900172// Parameter traits for movable-but-not-copyable scopers.
173//
174// Callback<>/Bind() understands movable-but-not-copyable semantics where
175// the type cannot be copied but can still have its state destructively
176// transferred (aka. moved) to another instance of the same type by calling a
177// helper function. When used with Bind(), this signifies transferal of the
178// object's state to the target function.
179//
180// For these types, the ForwardType must not be a const reference, or a
181// reference. A const reference is inappropriate, and would break const
182// correctness, because we are implementing a destructive move. A non-const
183// reference cannot be used with temporaries which means the result of a
184// function or a cast would not be usable with Callback<> or Bind().
willchan@chromium.org9be58392012-03-23 09:08:42 +0900185template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900186struct CallbackParamTraitsForMoveOnlyType {
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900187 typedef T ForwardType;
188 typedef T StorageType;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900189};
190
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900191// CallbackForward() is a very limited simulation of C++11's std::forward()
192// used by the Callback/Bind system for a set of movable-but-not-copyable
193// types. It is needed because forwarding a movable-but-not-copyable
194// argument to another function requires us to invoke the proper move
195// operator to create a rvalue version of the type. The supported types are
196// whitelisted below as overloads of the CallbackForward() function. The
197// default template compiles out to be a no-op.
198//
199// In C++11, std::forward would replace all uses of this function. However, it
200// is impossible to implement a general std::forward with C++11 due to a lack
201// of rvalue references.
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900202//
203// In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to
204// simulate std::forward() and forward the result of one Callback as a
205// parameter to another callback. This is to support Callbacks that return
206// the movable-but-not-copyable types whitelisted above.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900207template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900208typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) {
209 return t;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900210}
211
212template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900213typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) {
214 return t.Pass();
215}
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900216
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900217} // namespace internal
218} // namespace base
219
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +0900220#endif // BASE_CALLBACK_INTERNAL_H_