willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 2 | // 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.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 8 | #ifndef BASE_CALLBACK_INTERNAL_H_ |
| 9 | #define BASE_CALLBACK_INTERNAL_H_ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 10 | |
jhawkins@chromium.org | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 11 | #include <stddef.h> |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 12 | #include <type_traits> |
jhawkins@chromium.org | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 13 | |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 14 | #include "base/atomic_ref_count.h" |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 15 | #include "base/base_export.h" |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 16 | #include "base/macros.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 18 | #include "base/memory/scoped_ptr.h" |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 19 | #include "base/template_util.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | namespace internal { |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 23 | class CallbackBase; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 24 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 25 | // BindStateBase is used to provide an opaque handle that the Callback |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 26 | // 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." |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 31 | // 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. |
| 36 | class BindStateBase { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 37 | protected: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 38 | 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.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 55 | }; |
| 56 | |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 57 | // Holds the Callback methods that don't require specialization to reduce |
| 58 | // template bloat. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 59 | class BASE_EXPORT CallbackBase { |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 60 | public: |
dcheng | 60283a8 | 2014-11-26 14:04:55 +0900 | [diff] [blame] | 61 | CallbackBase(const CallbackBase& c); |
| 62 | CallbackBase& operator=(const CallbackBase& c); |
| 63 | |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 64 | // Returns true if Callback is null (doesn't refer to anything). |
ricea | 61846ae | 2014-09-17 11:12:53 +0900 | [diff] [blame] | 65 | bool is_null() const { return bind_state_.get() == NULL; } |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 66 | |
jhawkins@chromium.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 67 | // Returns the Callback into an uninitialized state. |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 68 | void Reset(); |
| 69 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 70 | protected: |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 71 | // 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.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 77 | // Returns true if this callback equals |other|. |other| may be null. |
| 78 | bool Equals(const CallbackBase& other) const; |
| 79 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 80 | // 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.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 85 | |
jhawkins@chromium.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 86 | // Force the destructor to be instantiated inside this translation unit so |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 87 | // that our subclasses will not get inlined versions. Avoids more template |
| 88 | // bloat. |
| 89 | ~CallbackBase(); |
| 90 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 91 | scoped_refptr<BindStateBase> bind_state_; |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 92 | InvokeFuncStorage polymorphic_invoke_; |
| 93 | }; |
| 94 | |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 95 | // 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. |
| 98 | template <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 | |
brucedawson | 137c153 | 2014-10-29 07:18:04 +0900 | [diff] [blame] | 105 | static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) && |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 106 | !is_const<T>::value; |
| 107 | }; |
| 108 | |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 109 | template <typename> |
| 110 | struct CallbackParamTraitsForMoveOnlyType; |
| 111 | |
| 112 | template <typename> |
| 113 | struct 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.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 119 | // 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. |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 130 | template <typename T> |
| 131 | struct CallbackParamTraits |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 132 | : std::conditional<IsMoveOnlyType<T>::value, |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 133 | CallbackParamTraitsForMoveOnlyType<T>, |
vmpstr | 495da40 | 2015-11-18 17:43:26 +0900 | [diff] [blame] | 134 | CallbackParamTraitsForNonMoveOnlyType<T>>::type { |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | template <typename T> |
| 138 | struct CallbackParamTraitsForNonMoveOnlyType { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 139 | 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. |
| 148 | template <typename T> |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 149 | struct CallbackParamTraitsForNonMoveOnlyType<T&> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 150 | 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. |
| 159 | template <typename T, size_t n> |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 160 | struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 161 | typedef const T* ForwardType; |
| 162 | typedef const T* StorageType; |
| 163 | }; |
| 164 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 165 | // See comment for CallbackParamTraits<T[n]>. |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 166 | template <typename T> |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 167 | struct CallbackParamTraitsForNonMoveOnlyType<T[]> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 168 | typedef const T* ForwardType; |
| 169 | typedef const T* StorageType; |
| 170 | }; |
| 171 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 172 | // 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.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 185 | template <typename T> |
tzik | f9c3819 | 2014-11-20 10:08:20 +0900 | [diff] [blame] | 186 | struct CallbackParamTraitsForMoveOnlyType { |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 187 | typedef T ForwardType; |
| 188 | typedef T StorageType; |
willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 189 | }; |
| 190 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 191 | // 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.org | 85c5f23 | 2012-05-04 07:34:17 +0900 | [diff] [blame] | 202 | // |
| 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.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 207 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 208 | typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) { |
| 209 | return t; |
willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 213 | typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { |
| 214 | return t.Pass(); |
| 215 | } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 216 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 217 | } // namespace internal |
| 218 | } // namespace base |
| 219 | |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 220 | #endif // BASE_CALLBACK_INTERNAL_H_ |