Remove an old PangoFontDescription forward declaration.
[chromium-blink-merge.git] / base / callback_internal.h
blob8a5c4378bc1ccbfb5303727ecfd129dc49a42903
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_
11 #include <stddef.h>
13 #include "base/base_export.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
17 template <typename T>
18 class ScopedVector;
20 namespace base {
21 namespace internal {
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
28 // "type erasure."
29 class BindStateBase : public RefCountedThreadSafe<BindStateBase> {
30 protected:
31 friend class RefCountedThreadSafe<BindStateBase>;
32 virtual ~BindStateBase() {}
35 // Holds the Callback methods that don't require specialization to reduce
36 // template bloat.
37 class BASE_EXPORT CallbackBase {
38 public:
39 CallbackBase(const CallbackBase& c);
40 CallbackBase& operator=(const CallbackBase& c);
42 // Returns true if Callback is null (doesn't refer to anything).
43 bool is_null() const { return bind_state_.get() == NULL; }
45 // Returns the Callback into an uninitialized state.
46 void Reset();
48 protected:
49 // 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);
55 // Returns true if this callback equals |other|. |other| may be null.
56 bool Equals(const CallbackBase& other) const;
58 // 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);
64 // Force the destructor to be instantiated inside this translation unit so
65 // that our subclasses will not get inlined versions. Avoids more template
66 // bloat.
67 ~CallbackBase();
69 scoped_refptr<BindStateBase> bind_state_;
70 InvokeFuncStorage polymorphic_invoke_;
73 // 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.
76 template <typename T> struct IsMoveOnlyType {
77 template <typename U>
78 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
80 template <typename U>
81 static NoType Test(...);
83 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) &&
84 !is_const<T>::value;
87 // Returns |Then| as SelectType::Type if |condition| is true. Otherwise returns
88 // |Else|.
89 template <bool condition, typename Then, typename Else>
90 struct SelectType {
91 typedef Then Type;
94 template <typename Then, typename Else>
95 struct SelectType<false, Then, Else> {
96 typedef Else Type;
99 template <typename>
100 struct CallbackParamTraitsForMoveOnlyType;
102 template <typename>
103 struct CallbackParamTraitsForNonMoveOnlyType;
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
109 // This is a typetraits object that's used to take an argument type, and
110 // extract a suitable type for storing and forwarding arguments.
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.
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.
120 template <typename T>
121 struct CallbackParamTraits
122 : SelectType<IsMoveOnlyType<T>::value,
123 CallbackParamTraitsForMoveOnlyType<T>,
124 CallbackParamTraitsForNonMoveOnlyType<T> >::Type {
127 template <typename T>
128 struct CallbackParamTraitsForNonMoveOnlyType {
129 typedef const T& ForwardType;
130 typedef T StorageType;
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.
137 // The ForwardType should only be used for unbound arguments.
138 template <typename T>
139 struct CallbackParamTraitsForNonMoveOnlyType<T&> {
140 typedef T& ForwardType;
141 typedef T StorageType;
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.
149 template <typename T, size_t n>
150 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> {
151 typedef const T* ForwardType;
152 typedef const T* StorageType;
155 // See comment for CallbackParamTraits<T[n]>.
156 template <typename T>
157 struct CallbackParamTraitsForNonMoveOnlyType<T[]> {
158 typedef const T* ForwardType;
159 typedef const T* StorageType;
162 // Parameter traits for movable-but-not-copyable scopers.
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.
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().
175 template <typename T>
176 struct CallbackParamTraitsForMoveOnlyType {
177 typedef T ForwardType;
178 typedef T StorageType;
181 // 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.
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.
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.
197 template <typename T>
198 typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) {
199 return t;
202 template <typename T>
203 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) {
204 return t.Pass();
207 } // namespace internal
208 } // namespace base
210 #endif // BASE_CALLBACK_INTERNAL_H_