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> |
| 12 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 13 | #include "base/base_export.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 16 | |
willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 17 | template <typename T> |
| 18 | class ScopedVector; |
| 19 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 20 | namespace base { |
| 21 | namespace internal { |
| 22 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 23 | // BindStateBase is used to provide an opaque handle that the Callback |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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." |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 29 | class BindStateBase : public RefCountedThreadSafe<BindStateBase> { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 30 | protected: |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 31 | friend class RefCountedThreadSafe<BindStateBase>; |
| 32 | virtual ~BindStateBase() {} |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 35 | // Holds the Callback methods that don't require specialization to reduce |
| 36 | // template bloat. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 37 | class BASE_EXPORT CallbackBase { |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 38 | public: |
| 39 | // Returns true if Callback is null (doesn't refer to anything). |
| 40 | bool is_null() const; |
| 41 | |
jhawkins@chromium.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 42 | // Returns the Callback into an uninitialized state. |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 43 | void Reset(); |
| 44 | |
ajwong@chromium.org | 3bc50ce | 2011-09-10 12:14:35 +0900 | [diff] [blame] | 45 | protected: |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 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); |
| 51 | |
jhawkins@chromium.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 52 | // Returns true if this callback equals |other|. |other| may be null. |
| 53 | bool Equals(const CallbackBase& other) const; |
| 54 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 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); |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 60 | |
jhawkins@chromium.org | 4d570be | 2011-10-26 08:19:51 +0900 | [diff] [blame] | 61 | // Force the destructor to be instantiated inside this translation unit so |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 62 | // that our subclasses will not get inlined versions. Avoids more template |
| 63 | // bloat. |
| 64 | ~CallbackBase(); |
| 65 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 66 | scoped_refptr<BindStateBase> bind_state_; |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 67 | InvokeFuncStorage polymorphic_invoke_; |
| 68 | }; |
| 69 | |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 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 |
| 72 | // destructive way. |
| 73 | template <typename T> struct IsMoveOnlyType { |
| 74 | template <typename U> |
| 75 | static YesType Test(const typename U::MoveOnlyTypeForCPP03*); |
| 76 | |
| 77 | template <typename U> |
| 78 | static NoType Test(...); |
| 79 | |
| 80 | static const bool value = sizeof(Test<T>(0)) == sizeof(YesType) && |
| 81 | !is_const<T>::value; |
| 82 | }; |
| 83 | |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 84 | // This is a typetraits object that's used to take an argument type, and |
| 85 | // extract a suitable type for storing and forwarding arguments. |
| 86 | // |
| 87 | // In particular, it strips off references, and converts arrays to |
| 88 | // pointers for storage; and it avoids accidentally trying to create a |
| 89 | // "reference of a reference" if the argument is a reference type. |
| 90 | // |
| 91 | // This array type becomes an issue for storage because we are passing bound |
| 92 | // parameters by const reference. In this case, we end up passing an actual |
| 93 | // array type in the initializer list which C++ does not allow. This will |
| 94 | // break passing of C-string literals. |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 95 | template <typename T, bool is_move_only = IsMoveOnlyType<T>::value> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 96 | struct CallbackParamTraits { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 97 | typedef const T& ForwardType; |
| 98 | typedef T StorageType; |
| 99 | }; |
| 100 | |
| 101 | // The Storage should almost be impossible to trigger unless someone manually |
| 102 | // specifies type of the bind parameters. However, in case they do, |
| 103 | // this will guard against us accidentally storing a reference parameter. |
| 104 | // |
| 105 | // The ForwardType should only be used for unbound arguments. |
| 106 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 107 | struct CallbackParamTraits<T&, false> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 108 | typedef T& ForwardType; |
| 109 | typedef T StorageType; |
| 110 | }; |
| 111 | |
| 112 | // Note that for array types, we implicitly add a const in the conversion. This |
| 113 | // means that it is not possible to bind array arguments to functions that take |
| 114 | // a non-const pointer. Trying to specialize the template based on a "const |
| 115 | // T[n]" does not seem to match correctly, so we are stuck with this |
| 116 | // restriction. |
| 117 | template <typename T, size_t n> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 118 | struct CallbackParamTraits<T[n], false> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 119 | typedef const T* ForwardType; |
| 120 | typedef const T* StorageType; |
| 121 | }; |
| 122 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 123 | // See comment for CallbackParamTraits<T[n]>. |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 124 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 125 | struct CallbackParamTraits<T[], false> { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 126 | typedef const T* ForwardType; |
| 127 | typedef const T* StorageType; |
| 128 | }; |
| 129 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 130 | // Parameter traits for movable-but-not-copyable scopers. |
| 131 | // |
| 132 | // Callback<>/Bind() understands movable-but-not-copyable semantics where |
| 133 | // the type cannot be copied but can still have its state destructively |
| 134 | // transferred (aka. moved) to another instance of the same type by calling a |
| 135 | // helper function. When used with Bind(), this signifies transferal of the |
| 136 | // object's state to the target function. |
| 137 | // |
| 138 | // For these types, the ForwardType must not be a const reference, or a |
| 139 | // reference. A const reference is inappropriate, and would break const |
| 140 | // correctness, because we are implementing a destructive move. A non-const |
| 141 | // reference cannot be used with temporaries which means the result of a |
| 142 | // 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] | 143 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 144 | struct CallbackParamTraits<T, true> { |
| 145 | typedef T ForwardType; |
| 146 | typedef T StorageType; |
willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 147 | }; |
| 148 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 149 | // CallbackForward() is a very limited simulation of C++11's std::forward() |
| 150 | // used by the Callback/Bind system for a set of movable-but-not-copyable |
| 151 | // types. It is needed because forwarding a movable-but-not-copyable |
| 152 | // argument to another function requires us to invoke the proper move |
| 153 | // operator to create a rvalue version of the type. The supported types are |
| 154 | // whitelisted below as overloads of the CallbackForward() function. The |
| 155 | // default template compiles out to be a no-op. |
| 156 | // |
| 157 | // In C++11, std::forward would replace all uses of this function. However, it |
| 158 | // is impossible to implement a general std::forward with C++11 due to a lack |
| 159 | // of rvalue references. |
rsleevi@chromium.org | 85c5f23 | 2012-05-04 07:34:17 +0900 | [diff] [blame] | 160 | // |
| 161 | // In addition to Callback/Bind, this is used by PostTaskAndReplyWithResult to |
| 162 | // simulate std::forward() and forward the result of one Callback as a |
| 163 | // parameter to another callback. This is to support Callbacks that return |
| 164 | // the movable-but-not-copyable types whitelisted above. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 165 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 166 | typename enable_if<!IsMoveOnlyType<T>::value, T>::type& CallbackForward(T& t) { |
| 167 | return t; |
willchan@chromium.org | 9be5839 | 2012-03-23 09:08:42 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | template <typename T> |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 171 | typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { |
| 172 | return t.Pass(); |
| 173 | } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 174 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 175 | } // namespace internal |
| 176 | } // namespace base |
| 177 | |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 178 | #endif // BASE_CALLBACK_INTERNAL_H_ |