ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
| 4 | |
| 5 | // This defines a set of argument wrappers and related factory methods that |
| 6 | // can be used specify the refcounting and reference semantics of arguments |
| 7 | // that are bound by the Bind() function in base/bind.h. |
| 8 | // |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 9 | // It also defines a set of simple functions and utilities that people want |
| 10 | // when using Callback<> and Bind(). |
| 11 | // |
| 12 | // |
| 13 | // ARGUMENT BINDING WRAPPERS |
| 14 | // |
tnagel@chromium.org | 7643c21 | 2014-04-26 08:13:44 +0900 | [diff] [blame] | 15 | // The wrapper functions are base::Unretained(), base::Owned(), base::Passed(), |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 16 | // base::ConstRef(), and base::IgnoreResult(). |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 17 | // |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 18 | // Unretained() allows Bind() to bind a non-refcounted class, and to disable |
| 19 | // refcounting on arguments that are refcounted objects. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 20 | // |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 21 | // Owned() transfers ownership of an object to the Callback resulting from |
| 22 | // bind; the object will be deleted when the Callback is deleted. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 23 | // |
| 24 | // Passed() is for transferring movable-but-not-copyable types (eg. scoped_ptr) |
| 25 | // through a Callback. Logically, this signifies a destructive transfer of |
| 26 | // the state of the argument into the target function. Invoking |
| 27 | // Callback::Run() twice on a Callback that was created with a Passed() |
| 28 | // argument will CHECK() because the first invocation would have already |
| 29 | // transferred ownership to the target function. |
| 30 | // |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 31 | // RetainedRef() accepts a ref counted object and retains a reference to it. |
| 32 | // When the callback is called, the object is passed as a raw pointer. |
| 33 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 34 | // ConstRef() allows binding a constant reference to an argument rather |
| 35 | // than a copy. |
| 36 | // |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 37 | // IgnoreResult() is used to adapt a function or Callback with a return type to |
| 38 | // one with a void return. This is most useful if you have a function with, |
| 39 | // say, a pesky ignorable bool return that you want to use with PostTask or |
| 40 | // something else that expect a Callback with a void return. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 41 | // |
| 42 | // EXAMPLE OF Unretained(): |
| 43 | // |
| 44 | // class Foo { |
| 45 | // public: |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 46 | // void func() { cout << "Foo:f" << endl; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 47 | // }; |
| 48 | // |
| 49 | // // In some function somewhere. |
| 50 | // Foo foo; |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 51 | // Closure foo_callback = |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 52 | // Bind(&Foo::func, Unretained(&foo)); |
| 53 | // foo_callback.Run(); // Prints "Foo:f". |
| 54 | // |
| 55 | // Without the Unretained() wrapper on |&foo|, the above call would fail |
| 56 | // to compile because Foo does not support the AddRef() and Release() methods. |
| 57 | // |
| 58 | // |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 59 | // EXAMPLE OF Owned(): |
| 60 | // |
| 61 | // void foo(int* arg) { cout << *arg << endl } |
| 62 | // |
| 63 | // int* pn = new int(1); |
| 64 | // Closure foo_callback = Bind(&foo, Owned(pn)); |
| 65 | // |
| 66 | // foo_callback.Run(); // Prints "1" |
| 67 | // foo_callback.Run(); // Prints "1" |
| 68 | // *n = 2; |
| 69 | // foo_callback.Run(); // Prints "2" |
| 70 | // |
| 71 | // foo_callback.Reset(); // |pn| is deleted. Also will happen when |
| 72 | // // |foo_callback| goes out of scope. |
| 73 | // |
| 74 | // Without Owned(), someone would have to know to delete |pn| when the last |
| 75 | // reference to the Callback is deleted. |
| 76 | // |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 77 | // EXAMPLE OF RetainedRef(): |
| 78 | // |
| 79 | // void foo(RefCountedBytes* bytes) {} |
| 80 | // |
| 81 | // scoped_refptr<RefCountedBytes> bytes = ...; |
| 82 | // Closure callback = Bind(&foo, base::RetainedRef(bytes)); |
| 83 | // callback.Run(); |
| 84 | // |
| 85 | // Without RetainedRef, the scoped_refptr would try to implicitly convert to |
| 86 | // a raw pointer and fail compilation: |
| 87 | // |
| 88 | // Closure callback = Bind(&foo, bytes); // ERROR! |
| 89 | // |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 90 | // |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 91 | // EXAMPLE OF ConstRef(): |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 92 | // |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 93 | // void foo(int arg) { cout << arg << endl } |
| 94 | // |
| 95 | // int n = 1; |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 96 | // Closure no_ref = Bind(&foo, n); |
| 97 | // Closure has_ref = Bind(&foo, ConstRef(n)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 98 | // |
| 99 | // no_ref.Run(); // Prints "1" |
| 100 | // has_ref.Run(); // Prints "1" |
| 101 | // |
| 102 | // n = 2; |
| 103 | // no_ref.Run(); // Prints "1" |
| 104 | // has_ref.Run(); // Prints "2" |
| 105 | // |
| 106 | // Note that because ConstRef() takes a reference on |n|, |n| must outlive all |
| 107 | // its bound callbacks. |
| 108 | // |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 109 | // |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 110 | // EXAMPLE OF IgnoreResult(): |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 111 | // |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 112 | // int DoSomething(int arg) { cout << arg << endl; } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 113 | // |
| 114 | // // Assign to a Callback with a void return type. |
| 115 | // Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething)); |
| 116 | // cb->Run(1); // Prints "1". |
| 117 | // |
| 118 | // // Prints "1" on |ml|. |
| 119 | // ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1); |
| 120 | // |
| 121 | // |
| 122 | // EXAMPLE OF Passed(): |
| 123 | // |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 124 | // void TakesOwnership(std::unique_ptr<Foo> arg) { } |
| 125 | // std::unique_ptr<Foo> CreateFoo() { return std::unique_ptr<Foo>(new Foo()); |
| 126 | // } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 127 | // |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 128 | // std::unique_ptr<Foo> f(new Foo()); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 129 | // |
| 130 | // // |cb| is given ownership of Foo(). |f| is now NULL. |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 131 | // // You can use std::move(f) in place of &f, but it's more verbose. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 132 | // Closure cb = Bind(&TakesOwnership, Passed(&f)); |
| 133 | // |
| 134 | // // Run was never called so |cb| still owns Foo() and deletes |
| 135 | // // it on Reset(). |
| 136 | // cb.Reset(); |
| 137 | // |
| 138 | // // |cb| is given a new Foo created by CreateFoo(). |
| 139 | // cb = Bind(&TakesOwnership, Passed(CreateFoo())); |
| 140 | // |
| 141 | // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| |
| 142 | // // no longer owns Foo() and, if reset, would not delete Foo(). |
| 143 | // cb.Run(); // Foo() is now transferred to |arg| and deleted. |
| 144 | // cb.Run(); // This CHECK()s since Foo() already been used once. |
| 145 | // |
| 146 | // Passed() is particularly useful with PostTask() when you are transferring |
| 147 | // ownership of an argument into a task, but don't necessarily know if the |
| 148 | // task will always be executed. This can happen if the task is cancellable |
skyostil | 97aefe1 | 2015-05-01 04:06:15 +0900 | [diff] [blame] | 149 | // or if it is posted to a TaskRunner. |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 150 | // |
| 151 | // |
| 152 | // SIMPLE FUNCTIONS AND UTILITIES. |
| 153 | // |
| 154 | // DoNothing() - Useful for creating a Closure that does nothing when called. |
| 155 | // DeletePointer<T>() - Useful for creating a Closure that will delete a |
| 156 | // pointer when invoked. Only use this when necessary. |
| 157 | // In most cases MessageLoop::DeleteSoon() is a better |
| 158 | // fit. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 159 | |
| 160 | #ifndef BASE_BIND_HELPERS_H_ |
| 161 | #define BASE_BIND_HELPERS_H_ |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 162 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 163 | #include <stddef.h> |
| 164 | |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 165 | #include <type_traits> |
| 166 | #include <utility> |
| 167 | |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 168 | #include "base/callback.h" |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 169 | #include "base/memory/weak_ptr.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 170 | #include "build/build_config.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 171 | |
| 172 | namespace base { |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 173 | |
| 174 | template <typename T> |
| 175 | struct IsWeakReceiver; |
| 176 | |
tzik | 66d9b74 | 2016-08-29 23:44:43 +0900 | [diff] [blame] | 177 | template <typename> |
| 178 | struct BindUnwrapTraits; |
| 179 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 180 | namespace internal { |
| 181 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 182 | template <typename T> |
| 183 | class UnretainedWrapper { |
| 184 | public: |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 185 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 186 | T* get() const { return ptr_; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 187 | private: |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 188 | T* ptr_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | template <typename T> |
| 192 | class ConstRefWrapper { |
| 193 | public: |
| 194 | explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 195 | const T& get() const { return *ptr_; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 196 | private: |
| 197 | const T* ptr_; |
| 198 | }; |
| 199 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 200 | template <typename T> |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 201 | class RetainedRefWrapper { |
| 202 | public: |
| 203 | explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 204 | explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 205 | T* get() const { return ptr_.get(); } |
| 206 | private: |
| 207 | scoped_refptr<T> ptr_; |
| 208 | }; |
| 209 | |
| 210 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 211 | struct IgnoreResultHelper { |
tzik | 3af517a | 2016-06-29 17:18:37 +0900 | [diff] [blame] | 212 | explicit IgnoreResultHelper(T functor) : functor_(std::move(functor)) {} |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame] | 213 | explicit operator bool() const { return !!functor_; } |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 214 | |
| 215 | T functor_; |
| 216 | }; |
| 217 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 218 | // An alternate implementation is to avoid the destructive copy, and instead |
| 219 | // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 220 | // a class that is essentially a std::unique_ptr<>. |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 221 | // |
| 222 | // The current implementation has the benefit though of leaving ParamTraits<> |
| 223 | // fully in callback_internal.h as well as avoiding type conversions during |
| 224 | // storage. |
| 225 | template <typename T> |
| 226 | class OwnedWrapper { |
| 227 | public: |
| 228 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| 229 | ~OwnedWrapper() { delete ptr_; } |
| 230 | T* get() const { return ptr_; } |
tzik | ca4b25e | 2016-07-06 15:39:19 +0900 | [diff] [blame] | 231 | OwnedWrapper(OwnedWrapper&& other) { |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 232 | ptr_ = other.ptr_; |
| 233 | other.ptr_ = NULL; |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | mutable T* ptr_; |
| 238 | }; |
| 239 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 240 | // PassedWrapper is a copyable adapter for a scoper that ignores const. |
| 241 | // |
| 242 | // It is needed to get around the fact that Bind() takes a const reference to |
| 243 | // all its arguments. Because Bind() takes a const reference to avoid |
| 244 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| 245 | // types; doing a destructive "move" of the type into Bind() would violate |
| 246 | // the const correctness. |
| 247 | // |
| 248 | // This conundrum cannot be solved without either C++11 rvalue references or |
| 249 | // a O(2^n) blowup of Bind() templates to handle each combination of regular |
| 250 | // types and movable-but-not-copyable types. Thus we introduce a wrapper type |
| 251 | // that is copyable to transmit the correct type information down into |
| 252 | // BindState<>. Ignoring const in this type makes sense because it is only |
| 253 | // created when we are explicitly trying to do a destructive move. |
| 254 | // |
| 255 | // Two notes: |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 256 | // 1) PassedWrapper supports any type that has a move constructor, however |
| 257 | // the type will need to be specifically whitelisted in order for it to be |
| 258 | // bound to a Callback. We guard this explicitly at the call of Passed() |
| 259 | // to make for clear errors. Things not given to Passed() will be forwarded |
| 260 | // and stored by value which will not work for general move-only types. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 261 | // 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL" |
| 262 | // scoper to a Callback and allow the Callback to execute once. |
| 263 | template <typename T> |
| 264 | class PassedWrapper { |
| 265 | public: |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 266 | explicit PassedWrapper(T&& scoper) |
| 267 | : is_valid_(true), scoper_(std::move(scoper)) {} |
tzik | ca4b25e | 2016-07-06 15:39:19 +0900 | [diff] [blame] | 268 | PassedWrapper(PassedWrapper&& other) |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 269 | : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {} |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 270 | T Take() const { |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 271 | CHECK(is_valid_); |
| 272 | is_valid_ = false; |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 273 | return std::move(scoper_); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | private: |
| 277 | mutable bool is_valid_; |
| 278 | mutable T scoper_; |
| 279 | }; |
| 280 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 281 | template <typename T> |
tzik | 66d9b74 | 2016-08-29 23:44:43 +0900 | [diff] [blame] | 282 | using Unwrapper = BindUnwrapTraits<typename std::decay<T>::type>; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 283 | |
| 284 | template <typename T> |
tzik | 66d9b74 | 2016-08-29 23:44:43 +0900 | [diff] [blame] | 285 | auto Unwrap(T&& o) -> decltype(Unwrapper<T>::Unwrap(std::forward<T>(o))) { |
| 286 | return Unwrapper<T>::Unwrap(std::forward<T>(o)); |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 287 | } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 288 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 289 | // IsWeakMethod is a helper that determine if we are binding a WeakPtr<> to a |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 290 | // method. It is used internally by Bind() to select the correct |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 291 | // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
| 292 | // the target object is invalidated. |
| 293 | // |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 294 | // The first argument should be the type of the object that will be received by |
| 295 | // the method. |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 296 | template <bool is_method, typename... Args> |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 297 | struct IsWeakMethod : std::false_type {}; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 298 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 299 | template <typename T, typename... Args> |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 300 | struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {}; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 301 | |
| 302 | // Packs a list of types to hold them in a single type. |
| 303 | template <typename... Types> |
| 304 | struct TypeList {}; |
| 305 | |
| 306 | // Used for DropTypeListItem implementation. |
| 307 | template <size_t n, typename List> |
| 308 | struct DropTypeListItemImpl; |
| 309 | |
| 310 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 311 | template <size_t n, typename T, typename... List> |
| 312 | struct DropTypeListItemImpl<n, TypeList<T, List...>> |
| 313 | : DropTypeListItemImpl<n - 1, TypeList<List...>> {}; |
| 314 | |
| 315 | template <typename T, typename... List> |
| 316 | struct DropTypeListItemImpl<0, TypeList<T, List...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 317 | using Type = TypeList<T, List...>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | template <> |
| 321 | struct DropTypeListItemImpl<0, TypeList<>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 322 | using Type = TypeList<>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | // A type-level function that drops |n| list item from given TypeList. |
| 326 | template <size_t n, typename List> |
| 327 | using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type; |
| 328 | |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 329 | // Used for TakeTypeListItem implementation. |
| 330 | template <size_t n, typename List, typename... Accum> |
| 331 | struct TakeTypeListItemImpl; |
| 332 | |
| 333 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 334 | template <size_t n, typename T, typename... List, typename... Accum> |
| 335 | struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...> |
| 336 | : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {}; |
| 337 | |
| 338 | template <typename T, typename... List, typename... Accum> |
| 339 | struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> { |
| 340 | using Type = TypeList<Accum...>; |
| 341 | }; |
| 342 | |
| 343 | template <typename... Accum> |
| 344 | struct TakeTypeListItemImpl<0, TypeList<>, Accum...> { |
| 345 | using Type = TypeList<Accum...>; |
| 346 | }; |
| 347 | |
| 348 | // A type-level function that takes first |n| list item from given TypeList. |
| 349 | // E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to |
| 350 | // TypeList<A, B, C>. |
| 351 | template <size_t n, typename List> |
| 352 | using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type; |
| 353 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 354 | // Used for ConcatTypeLists implementation. |
| 355 | template <typename List1, typename List2> |
| 356 | struct ConcatTypeListsImpl; |
| 357 | |
| 358 | template <typename... Types1, typename... Types2> |
| 359 | struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 360 | using Type = TypeList<Types1..., Types2...>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | // A type-level function that concats two TypeLists. |
| 364 | template <typename List1, typename List2> |
| 365 | using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type; |
| 366 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 367 | // Used for MakeFunctionType implementation. |
| 368 | template <typename R, typename ArgList> |
| 369 | struct MakeFunctionTypeImpl; |
| 370 | |
| 371 | template <typename R, typename... Args> |
| 372 | struct MakeFunctionTypeImpl<R, TypeList<Args...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 373 | // MSVC 2013 doesn't support Type Alias of function types. |
| 374 | // Revisit this after we update it to newer version. |
| 375 | typedef R Type(Args...); |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | // A type-level function that constructs a function type that has |R| as its |
| 379 | // return type and has TypeLists items as its arguments. |
| 380 | template <typename R, typename ArgList> |
| 381 | using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 382 | |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 383 | // Used for ExtractArgs and ExtractReturnType. |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 384 | template <typename Signature> |
| 385 | struct ExtractArgsImpl; |
| 386 | |
| 387 | template <typename R, typename... Args> |
| 388 | struct ExtractArgsImpl<R(Args...)> { |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 389 | using ReturnType = R; |
| 390 | using ArgsList = TypeList<Args...>; |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | // A type-level function that extracts function arguments into a TypeList. |
| 394 | // E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>. |
| 395 | template <typename Signature> |
tzik | fa6c985 | 2016-06-28 21:22:21 +0900 | [diff] [blame] | 396 | using ExtractArgs = typename ExtractArgsImpl<Signature>::ArgsList; |
| 397 | |
| 398 | // A type-level function that extracts the return type of a function. |
| 399 | // E.g. ExtractReturnType<R(A, B, C)> is evaluated to R. |
| 400 | template <typename Signature> |
| 401 | using ExtractReturnType = typename ExtractArgsImpl<Signature>::ReturnType; |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 402 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 403 | } // namespace internal |
| 404 | |
| 405 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 406 | static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 407 | return internal::UnretainedWrapper<T>(o); |
| 408 | } |
| 409 | |
| 410 | template <typename T> |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 411 | static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) { |
| 412 | return internal::RetainedRefWrapper<T>(o); |
| 413 | } |
| 414 | |
| 415 | template <typename T> |
| 416 | static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) { |
| 417 | return internal::RetainedRefWrapper<T>(std::move(o)); |
| 418 | } |
| 419 | |
| 420 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 421 | static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 422 | return internal::ConstRefWrapper<T>(o); |
| 423 | } |
| 424 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 425 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 426 | static inline internal::OwnedWrapper<T> Owned(T* o) { |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 427 | return internal::OwnedWrapper<T>(o); |
| 428 | } |
| 429 | |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 430 | // We offer 2 syntaxes for calling Passed(). The first takes an rvalue and |
| 431 | // is best suited for use with the return value of a function or other temporary |
| 432 | // rvalues. The second takes a pointer to the scoper and is just syntactic sugar |
| 433 | // to avoid having to write Passed(std::move(scoper)). |
| 434 | // |
| 435 | // Both versions of Passed() prevent T from being an lvalue reference. The first |
| 436 | // via use of enable_if, and the second takes a T* which will not bind to T&. |
| 437 | template <typename T, |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 438 | typename std::enable_if<!std::is_lvalue_reference<T>::value>::type* = |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 439 | nullptr> |
| 440 | static inline internal::PassedWrapper<T> Passed(T&& scoper) { |
| 441 | return internal::PassedWrapper<T>(std::move(scoper)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 442 | } |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 443 | template <typename T> |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 444 | static inline internal::PassedWrapper<T> Passed(T* scoper) { |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 445 | return internal::PassedWrapper<T>(std::move(*scoper)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 446 | } |
| 447 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 448 | template <typename T> |
| 449 | static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { |
tzik | 3af517a | 2016-06-29 17:18:37 +0900 | [diff] [blame] | 450 | return internal::IgnoreResultHelper<T>(std::move(data)); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 451 | } |
| 452 | |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 453 | BASE_EXPORT void DoNothing(); |
| 454 | |
| 455 | template<typename T> |
| 456 | void DeletePointer(T* obj) { |
| 457 | delete obj; |
| 458 | } |
| 459 | |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 460 | // An injection point to control |this| pointer behavior on a method invocation. |
| 461 | // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 462 | // method, base::Bind cancels the method invocation if the receiver is tested as |
| 463 | // false. |
| 464 | // E.g. Foo::bar() is not called: |
| 465 | // struct Foo : base::SupportsWeakPtr<Foo> { |
| 466 | // void bar() {} |
| 467 | // }; |
| 468 | // |
| 469 | // WeakPtr<Foo> oo = nullptr; |
| 470 | // base::Bind(&Foo::bar, oo).Run(); |
| 471 | template <typename T> |
| 472 | struct IsWeakReceiver : std::false_type {}; |
| 473 | |
| 474 | template <typename T> |
| 475 | struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
| 476 | |
| 477 | template <typename T> |
| 478 | struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 479 | |
tzik | 66d9b74 | 2016-08-29 23:44:43 +0900 | [diff] [blame] | 480 | // An injection point to control how bound objects passed to the target |
| 481 | // function. BindUnwrapTraits<>::Unwrap() is called for each bound objects right |
| 482 | // before the target function is invoked. |
| 483 | template <typename> |
| 484 | struct BindUnwrapTraits { |
| 485 | template <typename T> |
| 486 | static T&& Unwrap(T&& o) { return std::forward<T>(o); } |
| 487 | }; |
| 488 | |
| 489 | template <typename T> |
| 490 | struct BindUnwrapTraits<internal::UnretainedWrapper<T>> { |
| 491 | static T* Unwrap(const internal::UnretainedWrapper<T>& o) { |
| 492 | return o.get(); |
| 493 | } |
| 494 | }; |
| 495 | |
| 496 | template <typename T> |
| 497 | struct BindUnwrapTraits<internal::ConstRefWrapper<T>> { |
| 498 | static const T& Unwrap(const internal::ConstRefWrapper<T>& o) { |
| 499 | return o.get(); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | template <typename T> |
| 504 | struct BindUnwrapTraits<internal::RetainedRefWrapper<T>> { |
| 505 | static T* Unwrap(const internal::RetainedRefWrapper<T>& o) { |
| 506 | return o.get(); |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | template <typename T> |
| 511 | struct BindUnwrapTraits<internal::OwnedWrapper<T>> { |
| 512 | static T* Unwrap(const internal::OwnedWrapper<T>& o) { |
| 513 | return o.get(); |
| 514 | } |
| 515 | }; |
| 516 | |
| 517 | template <typename T> |
| 518 | struct BindUnwrapTraits<internal::PassedWrapper<T>> { |
| 519 | static T Unwrap(const internal::PassedWrapper<T>& o) { |
| 520 | return o.Take(); |
| 521 | } |
| 522 | }; |
| 523 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 524 | } // namespace base |
| 525 | |
| 526 | #endif // BASE_BIND_HELPERS_H_ |