blob: 8a5c4378bc1ccbfb5303727ecfd129dc49a42903 [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:
dcheng60283a82014-11-26 14:04:55 +090039 CallbackBase(const CallbackBase& c);
40 CallbackBase& operator=(const CallbackBase& c);
41
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090042 // Returns true if Callback is null (doesn't refer to anything).
ricea61846ae2014-09-17 11:12:53 +090043 bool is_null() const { return bind_state_.get() == NULL; }
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090044
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090045 // Returns the Callback into an uninitialized state.
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090046 void Reset();
47
ajwong@chromium.org3bc50ce2011-09-10 12:14:35 +090048 protected:
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090049 // In C++, it is safe to cast function pointers to function pointers of
50 // another type. It is not okay to use void*. We create a InvokeFuncStorage
51 // that that can store our function pointer, and then cast it back to
52 // the original type on usage.
53 typedef void(*InvokeFuncStorage)(void);
54
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090055 // Returns true if this callback equals |other|. |other| may be null.
56 bool Equals(const CallbackBase& other) const;
57
ajwong@chromium.orgabd70002011-12-20 09:10:04 +090058 // Allow initializing of |bind_state_| via the constructor to avoid default
59 // initialization of the scoped_refptr. We do not also initialize
60 // |polymorphic_invoke_| here because doing a normal assignment in the
61 // derived Callback templates makes for much nicer compiler errors.
62 explicit CallbackBase(BindStateBase* bind_state);
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090063
jhawkins@chromium.org4d570be2011-10-26 08:19:51 +090064 // Force the destructor to be instantiated inside this translation unit so
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090065 // that our subclasses will not get inlined versions. Avoids more template
66 // bloat.
67 ~CallbackBase();
68
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090069 scoped_refptr<BindStateBase> bind_state_;
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +090070 InvokeFuncStorage polymorphic_invoke_;
71};
72
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090073// A helper template to determine if given type is non-const move-only-type,
74// i.e. if a value of the given type should be passed via .Pass() in a
75// destructive way.
76template <typename T> struct IsMoveOnlyType {
77 template <typename U>
78 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
79
80 template <typename U>
81 static NoType Test(...);
82
brucedawson137c1532014-10-29 07:18:04 +090083 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) &&
kinuko@chromium.org288f27a2013-10-22 18:18:20 +090084 !is_const<T>::value;
85};
86
tzikf9c38192014-11-20 10:08:20 +090087// Returns |Then| as SelectType::Type if |condition| is true. Otherwise returns
88// |Else|.
89template <bool condition, typename Then, typename Else>
90struct SelectType {
91 typedef Then Type;
92};
93
94template <typename Then, typename Else>
95struct SelectType<false, Then, Else> {
96 typedef Else Type;
97};
98
99template <typename>
100struct CallbackParamTraitsForMoveOnlyType;
101
102template <typename>
103struct CallbackParamTraitsForNonMoveOnlyType;
104
105// TODO(tzik): Use a default parameter once MSVS supports variadic templates
106// with default values.
107// http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilation-error-with-variadic-templates
108//
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900109// This is a typetraits object that's used to take an argument type, and
110// extract a suitable type for storing and forwarding arguments.
111//
112// In particular, it strips off references, and converts arrays to
113// pointers for storage; and it avoids accidentally trying to create a
114// "reference of a reference" if the argument is a reference type.
115//
116// This array type becomes an issue for storage because we are passing bound
117// parameters by const reference. In this case, we end up passing an actual
118// array type in the initializer list which C++ does not allow. This will
119// break passing of C-string literals.
tzikf9c38192014-11-20 10:08:20 +0900120template <typename T>
121struct CallbackParamTraits
122 : SelectType<IsMoveOnlyType<T>::value,
123 CallbackParamTraitsForMoveOnlyType<T>,
124 CallbackParamTraitsForNonMoveOnlyType<T> >::Type {
125};
126
127template <typename T>
128struct CallbackParamTraitsForNonMoveOnlyType {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900129 typedef const T& ForwardType;
130 typedef T StorageType;
131};
132
133// The Storage should almost be impossible to trigger unless someone manually
134// specifies type of the bind parameters. However, in case they do,
135// this will guard against us accidentally storing a reference parameter.
136//
137// The ForwardType should only be used for unbound arguments.
138template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900139struct CallbackParamTraitsForNonMoveOnlyType<T&> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900140 typedef T& ForwardType;
141 typedef T StorageType;
142};
143
144// Note that for array types, we implicitly add a const in the conversion. This
145// means that it is not possible to bind array arguments to functions that take
146// a non-const pointer. Trying to specialize the template based on a "const
147// T[n]" does not seem to match correctly, so we are stuck with this
148// restriction.
149template <typename T, size_t n>
tzikf9c38192014-11-20 10:08:20 +0900150struct CallbackParamTraitsForNonMoveOnlyType<T[n]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900151 typedef const T* ForwardType;
152 typedef const T* StorageType;
153};
154
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900155// See comment for CallbackParamTraits<T[n]>.
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900156template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900157struct CallbackParamTraitsForNonMoveOnlyType<T[]> {
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900158 typedef const T* ForwardType;
159 typedef const T* StorageType;
160};
161
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900162// Parameter traits for movable-but-not-copyable scopers.
163//
164// Callback<>/Bind() understands movable-but-not-copyable semantics where
165// the type cannot be copied but can still have its state destructively
166// transferred (aka. moved) to another instance of the same type by calling a
167// helper function. When used with Bind(), this signifies transferal of the
168// object's state to the target function.
169//
170// For these types, the ForwardType must not be a const reference, or a
171// reference. A const reference is inappropriate, and would break const
172// correctness, because we are implementing a destructive move. A non-const
173// reference cannot be used with temporaries which means the result of a
174// function or a cast would not be usable with Callback<> or Bind().
willchan@chromium.org9be58392012-03-23 09:08:42 +0900175template <typename T>
tzikf9c38192014-11-20 10:08:20 +0900176struct CallbackParamTraitsForMoveOnlyType {
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900177 typedef T ForwardType;
178 typedef T StorageType;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900179};
180
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900181// CallbackForward() is a very limited simulation of C++11's std::forward()
182// used by the Callback/Bind system for a set of movable-but-not-copyable
183// types. It is needed because forwarding a movable-but-not-copyable
184// argument to another function requires us to invoke the proper move
185// operator to create a rvalue version of the type. The supported types are
186// whitelisted below as overloads of the CallbackForward() function. The
187// default template compiles out to be a no-op.
188//
189// In C++11, std::forward would replace all uses of this function. However, it
190// is impossible to implement a general std::forward with C++11 due to a lack
191// of rvalue references.
rsleevi@chromium.org85c5f232012-05-04 07:34:17 +0900192//
193// In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to
194// simulate std::forward() and forward the result of one Callback as a
195// parameter to another callback. This is to support Callbacks that return
196// the movable-but-not-copyable types whitelisted above.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900197template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900198typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) {
199 return t;
willchan@chromium.org9be58392012-03-23 09:08:42 +0900200}
201
202template <typename T>
kinuko@chromium.org288f27a2013-10-22 18:18:20 +0900203typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) {
204 return t.Pass();
205}
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900206
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900207} // namespace internal
208} // namespace base
209
ajwong@chromium.orgfa0ff432011-02-19 08:29:31 +0900210#endif // BASE_CALLBACK_INTERNAL_H_