1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains utility functions and classes that help the
6 // implementation, and management of the Callback objects.
8 #ifndef BASE_CALLBACK_INTERNAL_H_
9 #define BASE_CALLBACK_INTERNAL_H_
13 #include "base/base_export.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
23 // BindStateBase is used to provide an opaque handle that the Callback
24 // 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
29 class BindStateBase
: public RefCountedThreadSafe
<BindStateBase
> {
31 friend class RefCountedThreadSafe
<BindStateBase
>;
32 virtual ~BindStateBase() {}
35 // Holds the Callback methods that don't require specialization to reduce
37 class BASE_EXPORT CallbackBase
{
39 // Returns true if Callback is null (doesn't refer to anything).
40 bool is_null() const { return bind_state_
.get() == NULL
; }
42 // Returns the Callback into an uninitialized state.
46 // 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);
52 // Returns true if this callback equals |other|. |other| may be null.
53 bool Equals(const CallbackBase
& other
) const;
55 // 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
);
61 // Force the destructor to be instantiated inside this translation unit so
62 // that our subclasses will not get inlined versions. Avoids more template
66 scoped_refptr
<BindStateBase
> bind_state_
;
67 InvokeFuncStorage polymorphic_invoke_
;
70 // 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
73 template <typename T
> struct IsMoveOnlyType
{
75 static YesType
Test(const typename
U::MoveOnlyTypeForCPP03
*);
78 static NoType
Test(...);
80 static const bool value
= sizeof((Test
<T
>(0))) == sizeof(YesType
) &&
84 // Returns |Then| as SelectType::Type if |condition| is true. Otherwise returns
86 template <bool condition
, typename Then
, typename Else
>
91 template <typename Then
, typename Else
>
92 struct SelectType
<false, Then
, Else
> {
97 struct CallbackParamTraitsForMoveOnlyType
;
100 struct CallbackParamTraitsForNonMoveOnlyType
;
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
106 // This is a typetraits object that's used to take an argument type, and
107 // extract a suitable type for storing and forwarding arguments.
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.
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.
117 template <typename T
>
118 struct CallbackParamTraits
119 : SelectType
<IsMoveOnlyType
<T
>::value
,
120 CallbackParamTraitsForMoveOnlyType
<T
>,
121 CallbackParamTraitsForNonMoveOnlyType
<T
> >::Type
{
124 template <typename T
>
125 struct CallbackParamTraitsForNonMoveOnlyType
{
126 typedef const T
& ForwardType
;
127 typedef T StorageType
;
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.
134 // The ForwardType should only be used for unbound arguments.
135 template <typename T
>
136 struct CallbackParamTraitsForNonMoveOnlyType
<T
&> {
137 typedef T
& ForwardType
;
138 typedef T StorageType
;
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
146 template <typename T
, size_t n
>
147 struct CallbackParamTraitsForNonMoveOnlyType
<T
[n
]> {
148 typedef const T
* ForwardType
;
149 typedef const T
* StorageType
;
152 // See comment for CallbackParamTraits<T[n]>.
153 template <typename T
>
154 struct CallbackParamTraitsForNonMoveOnlyType
<T
[]> {
155 typedef const T
* ForwardType
;
156 typedef const T
* StorageType
;
159 // Parameter traits for movable-but-not-copyable scopers.
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.
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().
172 template <typename T
>
173 struct CallbackParamTraitsForMoveOnlyType
{
174 typedef T ForwardType
;
175 typedef T StorageType
;
178 // 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.
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.
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.
194 template <typename T
>
195 typename enable_if
<!IsMoveOnlyType
<T
>::value
, T
>::type
& CallbackForward(T
& t
) {
199 template <typename T
>
200 typename enable_if
<IsMoveOnlyType
<T
>::value
, T
>::type
CallbackForward(T
& t
) {
204 } // namespace internal
207 #endif // BASE_CALLBACK_INTERNAL_H_