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 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 177 | namespace internal { |
| 178 | |
| 179 | // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T |
| 180 | // for the existence of AddRef() and Release() functions of the correct |
| 181 | // signature. |
| 182 | // |
| 183 | // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error |
| 184 | // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence |
| 185 | // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison |
| 186 | // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions |
| 187 | // |
| 188 | // The last link in particular show the method used below. |
| 189 | // |
| 190 | // For SFINAE to work with inherited methods, we need to pull some extra tricks |
| 191 | // with multiple inheritance. In the more standard formulation, the overloads |
| 192 | // of Check would be: |
| 193 | // |
| 194 | // template <typename C> |
| 195 | // Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*); |
| 196 | // |
| 197 | // template <typename C> |
| 198 | // No NotTheCheckWeWant(...); |
| 199 | // |
| 200 | // static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes); |
| 201 | // |
| 202 | // The problem here is that template resolution will not match |
| 203 | // C::TargetFunc if TargetFunc does not exist directly in C. That is, if |
| 204 | // TargetFunc in inherited from an ancestor, &C::TargetFunc will not match, |
| 205 | // |value| will be false. This formulation only checks for whether or |
| 206 | // not TargetFunc exist directly in the class being introspected. |
| 207 | // |
| 208 | // To get around this, we play a dirty trick with multiple inheritance. |
| 209 | // First, We create a class BaseMixin that declares each function that we |
| 210 | // want to probe for. Then we create a class Base that inherits from both T |
| 211 | // (the class we wish to probe) and BaseMixin. Note that the function |
| 212 | // signature in BaseMixin does not need to match the signature of the function |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 213 | // we are probing for; thus it's easiest to just use void(). |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 214 | // |
| 215 | // Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an |
| 216 | // ambiguous resolution between BaseMixin and T. This lets us write the |
| 217 | // following: |
| 218 | // |
| 219 | // template <typename C> |
| 220 | // No GoodCheck(Helper<&C::TargetFunc>*); |
| 221 | // |
| 222 | // template <typename C> |
| 223 | // Yes GoodCheck(...); |
| 224 | // |
| 225 | // static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes); |
| 226 | // |
| 227 | // Notice here that the variadic version of GoodCheck() returns Yes here |
| 228 | // instead of No like the previous one. Also notice that we calculate |value| |
| 229 | // by specializing GoodCheck() on Base instead of T. |
| 230 | // |
| 231 | // We've reversed the roles of the variadic, and Helper overloads. |
| 232 | // GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid |
| 233 | // substitution if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve |
| 234 | // to the variadic version if T has TargetFunc. If T::TargetFunc does not |
| 235 | // exist, then &C::TargetFunc is not ambiguous, and the overload resolution |
| 236 | // will prefer GoodCheck(Helper<&C::TargetFunc>*). |
| 237 | // |
| 238 | // This method of SFINAE will correctly probe for inherited names, but it cannot |
| 239 | // typecheck those names. It's still a good enough sanity check though. |
| 240 | // |
| 241 | // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008. |
| 242 | // |
| 243 | // TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted |
| 244 | // this works well. |
brettw@chromium.org | 7c0a8b6 | 2011-05-11 06:17:48 +0900 | [diff] [blame] | 245 | // |
| 246 | // TODO(ajwong): Make this check for Release() as well. |
| 247 | // See http://crbug.com/82038. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 248 | template <typename T> |
| 249 | class SupportsAddRefAndRelease { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 250 | using Yes = char[1]; |
| 251 | using No = char[2]; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 252 | |
| 253 | struct BaseMixin { |
| 254 | void AddRef(); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 255 | }; |
| 256 | |
ajwong@chromium.org | 6299740 | 2011-04-14 07:40:46 +0900 | [diff] [blame] | 257 | // MSVC warns when you try to use Base if T has a private destructor, the |
| 258 | // common pattern for refcounted types. It does this even though no attempt to |
| 259 | // instantiate Base is made. We disable the warning for this definition. |
| 260 | #if defined(OS_WIN) |
etienneb@chromium.org | 74854fcbe | 2013-07-31 14:22:02 +0900 | [diff] [blame] | 261 | #pragma warning(push) |
ajwong@chromium.org | 6299740 | 2011-04-14 07:40:46 +0900 | [diff] [blame] | 262 | #pragma warning(disable:4624) |
| 263 | #endif |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 264 | struct Base : public T, public BaseMixin { |
| 265 | }; |
ajwong@chromium.org | 6299740 | 2011-04-14 07:40:46 +0900 | [diff] [blame] | 266 | #if defined(OS_WIN) |
etienneb@chromium.org | 74854fcbe | 2013-07-31 14:22:02 +0900 | [diff] [blame] | 267 | #pragma warning(pop) |
ajwong@chromium.org | 6299740 | 2011-04-14 07:40:46 +0900 | [diff] [blame] | 268 | #endif |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 269 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 270 | template <void(BaseMixin::*)()> struct Helper {}; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 271 | |
| 272 | template <typename C> |
brettw@chromium.org | 7c0a8b6 | 2011-05-11 06:17:48 +0900 | [diff] [blame] | 273 | static No& Check(Helper<&C::AddRef>*); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 274 | |
| 275 | template <typename > |
| 276 | static Yes& Check(...); |
| 277 | |
| 278 | public: |
ncbray@chromium.org | e931705 | 2014-08-02 07:44:13 +0900 | [diff] [blame] | 279 | enum { value = sizeof(Check<Base>(0)) == sizeof(Yes) }; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 280 | }; |
| 281 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 282 | // Helpers to assert that arguments of a recounted type are bound with a |
| 283 | // scoped_refptr. |
| 284 | template <bool IsClasstype, typename T> |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 285 | struct UnsafeBindtoRefCountedArgHelper : std::false_type { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | template <typename T> |
| 289 | struct UnsafeBindtoRefCountedArgHelper<true, T> |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 290 | : std::integral_constant<bool, SupportsAddRefAndRelease<T>::value> { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | template <typename T> |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 294 | struct UnsafeBindtoRefCountedArg : std::false_type { |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | template <typename T> |
| 298 | struct UnsafeBindtoRefCountedArg<T*> |
tzik | 56bd765 | 2016-03-10 16:17:25 +0900 | [diff] [blame] | 299 | : UnsafeBindtoRefCountedArgHelper<std::is_class<T>::value, T> { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 300 | }; |
| 301 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 302 | template <typename T> |
| 303 | class HasIsMethodTag { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 304 | using Yes = char[1]; |
| 305 | using No = char[2]; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 306 | |
| 307 | template <typename U> |
| 308 | static Yes& Check(typename U::IsMethod*); |
| 309 | |
| 310 | template <typename U> |
| 311 | static No& Check(...); |
| 312 | |
| 313 | public: |
ncbray@chromium.org | e931705 | 2014-08-02 07:44:13 +0900 | [diff] [blame] | 314 | enum { value = sizeof(Check<T>(0)) == sizeof(Yes) }; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 315 | }; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 316 | |
| 317 | template <typename T> |
| 318 | class UnretainedWrapper { |
| 319 | public: |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 320 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 321 | T* get() const { return ptr_; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 322 | private: |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 323 | T* ptr_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | template <typename T> |
| 327 | class ConstRefWrapper { |
| 328 | public: |
| 329 | explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 330 | const T& get() const { return *ptr_; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 331 | private: |
| 332 | const T* ptr_; |
| 333 | }; |
| 334 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 335 | template <typename T> |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 336 | class RetainedRefWrapper { |
| 337 | public: |
| 338 | explicit RetainedRefWrapper(T* o) : ptr_(o) {} |
| 339 | explicit RetainedRefWrapper(scoped_refptr<T> o) : ptr_(std::move(o)) {} |
| 340 | T* get() const { return ptr_.get(); } |
| 341 | private: |
| 342 | scoped_refptr<T> ptr_; |
| 343 | }; |
| 344 | |
| 345 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 346 | struct IgnoreResultHelper { |
| 347 | explicit IgnoreResultHelper(T functor) : functor_(functor) {} |
| 348 | |
| 349 | T functor_; |
| 350 | }; |
| 351 | |
| 352 | template <typename T> |
| 353 | struct IgnoreResultHelper<Callback<T> > { |
| 354 | explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} |
| 355 | |
| 356 | const Callback<T>& functor_; |
| 357 | }; |
| 358 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 359 | // An alternate implementation is to avoid the destructive copy, and instead |
| 360 | // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 361 | // a class that is essentially a std::unique_ptr<>. |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 362 | // |
| 363 | // The current implementation has the benefit though of leaving ParamTraits<> |
| 364 | // fully in callback_internal.h as well as avoiding type conversions during |
| 365 | // storage. |
| 366 | template <typename T> |
| 367 | class OwnedWrapper { |
| 368 | public: |
| 369 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| 370 | ~OwnedWrapper() { delete ptr_; } |
| 371 | T* get() const { return ptr_; } |
| 372 | OwnedWrapper(const OwnedWrapper& other) { |
| 373 | ptr_ = other.ptr_; |
| 374 | other.ptr_ = NULL; |
| 375 | } |
| 376 | |
| 377 | private: |
| 378 | mutable T* ptr_; |
| 379 | }; |
| 380 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 381 | // PassedWrapper is a copyable adapter for a scoper that ignores const. |
| 382 | // |
| 383 | // It is needed to get around the fact that Bind() takes a const reference to |
| 384 | // all its arguments. Because Bind() takes a const reference to avoid |
| 385 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| 386 | // types; doing a destructive "move" of the type into Bind() would violate |
| 387 | // the const correctness. |
| 388 | // |
| 389 | // This conundrum cannot be solved without either C++11 rvalue references or |
| 390 | // a O(2^n) blowup of Bind() templates to handle each combination of regular |
| 391 | // types and movable-but-not-copyable types. Thus we introduce a wrapper type |
| 392 | // that is copyable to transmit the correct type information down into |
| 393 | // BindState<>. Ignoring const in this type makes sense because it is only |
| 394 | // created when we are explicitly trying to do a destructive move. |
| 395 | // |
| 396 | // Two notes: |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 397 | // 1) PassedWrapper supports any type that has a move constructor, however |
| 398 | // the type will need to be specifically whitelisted in order for it to be |
| 399 | // bound to a Callback. We guard this explicitly at the call of Passed() |
| 400 | // to make for clear errors. Things not given to Passed() will be forwarded |
| 401 | // 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] | 402 | // 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL" |
| 403 | // scoper to a Callback and allow the Callback to execute once. |
| 404 | template <typename T> |
| 405 | class PassedWrapper { |
| 406 | public: |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 407 | explicit PassedWrapper(T&& scoper) |
| 408 | : is_valid_(true), scoper_(std::move(scoper)) {} |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 409 | PassedWrapper(const PassedWrapper& other) |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 410 | : is_valid_(other.is_valid_), scoper_(std::move(other.scoper_)) {} |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 411 | T Take() const { |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 412 | CHECK(is_valid_); |
| 413 | is_valid_ = false; |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 414 | return std::move(scoper_); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private: |
| 418 | mutable bool is_valid_; |
| 419 | mutable T scoper_; |
| 420 | }; |
| 421 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 422 | // Unwrap the stored parameters for the wrappers above. |
| 423 | template <typename T> |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 424 | const T& Unwrap(const T& o) { |
| 425 | return o; |
| 426 | } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 427 | |
| 428 | template <typename T> |
tzik | a84047c | 2016-06-25 18:59:34 +0900 | [diff] [blame] | 429 | T* Unwrap(const UnretainedWrapper<T>& unretained) { |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 430 | return unretained.get(); |
| 431 | } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 432 | |
| 433 | template <typename T> |
tzik | a84047c | 2016-06-25 18:59:34 +0900 | [diff] [blame] | 434 | const T& Unwrap(const ConstRefWrapper<T>& const_ref) { |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 435 | return const_ref.get(); |
| 436 | } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 437 | |
ajwong@chromium.org | 28dfb11 | 2011-10-07 09:25:29 +0900 | [diff] [blame] | 438 | template <typename T> |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 439 | T* Unwrap(const RetainedRefWrapper<T>& o) { |
| 440 | return o.get(); |
| 441 | } |
| 442 | |
| 443 | template <typename T> |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 444 | const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { |
| 445 | return o; |
| 446 | } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 447 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 448 | template <typename T> |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 449 | T* Unwrap(const OwnedWrapper<T>& o) { |
| 450 | return o.get(); |
| 451 | } |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 452 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 453 | template <typename T> |
tzik | a84047c | 2016-06-25 18:59:34 +0900 | [diff] [blame] | 454 | T Unwrap(const PassedWrapper<T>& o) { |
tzik | c9adefe | 2016-02-17 00:04:09 +0900 | [diff] [blame] | 455 | return o.Take(); |
| 456 | } |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 457 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 458 | // 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] | 459 | // method. It is used internally by Bind() to select the correct |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 460 | // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
| 461 | // the target object is invalidated. |
| 462 | // |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 463 | // The first argument should be the type of the object that will be received by |
| 464 | // the method. |
| 465 | template <bool IsMethod, typename... Args> |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 466 | struct IsWeakMethod : std::false_type {}; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 467 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 468 | template <typename T, typename... Args> |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 469 | struct IsWeakMethod<true, T, Args...> : IsWeakReceiver<T> {}; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 470 | |
| 471 | // Packs a list of types to hold them in a single type. |
| 472 | template <typename... Types> |
| 473 | struct TypeList {}; |
| 474 | |
| 475 | // Used for DropTypeListItem implementation. |
| 476 | template <size_t n, typename List> |
| 477 | struct DropTypeListItemImpl; |
| 478 | |
| 479 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 480 | template <size_t n, typename T, typename... List> |
| 481 | struct DropTypeListItemImpl<n, TypeList<T, List...>> |
| 482 | : DropTypeListItemImpl<n - 1, TypeList<List...>> {}; |
| 483 | |
| 484 | template <typename T, typename... List> |
| 485 | struct DropTypeListItemImpl<0, TypeList<T, List...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 486 | using Type = TypeList<T, List...>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | template <> |
| 490 | struct DropTypeListItemImpl<0, TypeList<>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 491 | using Type = TypeList<>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | // A type-level function that drops |n| list item from given TypeList. |
| 495 | template <size_t n, typename List> |
| 496 | using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type; |
| 497 | |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 498 | // Used for TakeTypeListItem implementation. |
| 499 | template <size_t n, typename List, typename... Accum> |
| 500 | struct TakeTypeListItemImpl; |
| 501 | |
| 502 | // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure. |
| 503 | template <size_t n, typename T, typename... List, typename... Accum> |
| 504 | struct TakeTypeListItemImpl<n, TypeList<T, List...>, Accum...> |
| 505 | : TakeTypeListItemImpl<n - 1, TypeList<List...>, Accum..., T> {}; |
| 506 | |
| 507 | template <typename T, typename... List, typename... Accum> |
| 508 | struct TakeTypeListItemImpl<0, TypeList<T, List...>, Accum...> { |
| 509 | using Type = TypeList<Accum...>; |
| 510 | }; |
| 511 | |
| 512 | template <typename... Accum> |
| 513 | struct TakeTypeListItemImpl<0, TypeList<>, Accum...> { |
| 514 | using Type = TypeList<Accum...>; |
| 515 | }; |
| 516 | |
| 517 | // A type-level function that takes first |n| list item from given TypeList. |
| 518 | // E.g. TakeTypeListItem<3, TypeList<A, B, C, D>> is evaluated to |
| 519 | // TypeList<A, B, C>. |
| 520 | template <size_t n, typename List> |
| 521 | using TakeTypeListItem = typename TakeTypeListItemImpl<n, List>::Type; |
| 522 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 523 | // Used for ConcatTypeLists implementation. |
| 524 | template <typename List1, typename List2> |
| 525 | struct ConcatTypeListsImpl; |
| 526 | |
| 527 | template <typename... Types1, typename... Types2> |
| 528 | struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 529 | using Type = TypeList<Types1..., Types2...>; |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | // A type-level function that concats two TypeLists. |
| 533 | template <typename List1, typename List2> |
| 534 | using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type; |
| 535 | |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 536 | // Used for MakeFunctionType implementation. |
| 537 | template <typename R, typename ArgList> |
| 538 | struct MakeFunctionTypeImpl; |
| 539 | |
| 540 | template <typename R, typename... Args> |
| 541 | struct MakeFunctionTypeImpl<R, TypeList<Args...>> { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 542 | // MSVC 2013 doesn't support Type Alias of function types. |
| 543 | // Revisit this after we update it to newer version. |
| 544 | typedef R Type(Args...); |
tzik | 07e9940 | 2015-02-06 04:11:26 +0900 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | // A type-level function that constructs a function type that has |R| as its |
| 548 | // return type and has TypeLists items as its arguments. |
| 549 | template <typename R, typename ArgList> |
| 550 | using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 551 | |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 552 | // Used for ExtractArgs. |
| 553 | template <typename Signature> |
| 554 | struct ExtractArgsImpl; |
| 555 | |
| 556 | template <typename R, typename... Args> |
| 557 | struct ExtractArgsImpl<R(Args...)> { |
| 558 | using Type = TypeList<Args...>; |
| 559 | }; |
| 560 | |
| 561 | // A type-level function that extracts function arguments into a TypeList. |
| 562 | // E.g. ExtractArgs<R(A, B, C)> is evaluated to TypeList<A, B, C>. |
| 563 | template <typename Signature> |
| 564 | using ExtractArgs = typename ExtractArgsImpl<Signature>::Type; |
| 565 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 566 | } // namespace internal |
| 567 | |
| 568 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 569 | static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 570 | return internal::UnretainedWrapper<T>(o); |
| 571 | } |
| 572 | |
| 573 | template <typename T> |
vmpstr | 9645ac8 | 2016-03-19 05:46:41 +0900 | [diff] [blame] | 574 | static inline internal::RetainedRefWrapper<T> RetainedRef(T* o) { |
| 575 | return internal::RetainedRefWrapper<T>(o); |
| 576 | } |
| 577 | |
| 578 | template <typename T> |
| 579 | static inline internal::RetainedRefWrapper<T> RetainedRef(scoped_refptr<T> o) { |
| 580 | return internal::RetainedRefWrapper<T>(std::move(o)); |
| 581 | } |
| 582 | |
| 583 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 584 | static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 585 | return internal::ConstRefWrapper<T>(o); |
| 586 | } |
| 587 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 588 | template <typename T> |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 589 | static inline internal::OwnedWrapper<T> Owned(T* o) { |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 590 | return internal::OwnedWrapper<T>(o); |
| 591 | } |
| 592 | |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 593 | // We offer 2 syntaxes for calling Passed(). The first takes an rvalue and |
| 594 | // is best suited for use with the return value of a function or other temporary |
| 595 | // rvalues. The second takes a pointer to the scoper and is just syntactic sugar |
| 596 | // to avoid having to write Passed(std::move(scoper)). |
| 597 | // |
| 598 | // Both versions of Passed() prevent T from being an lvalue reference. The first |
| 599 | // via use of enable_if, and the second takes a T* which will not bind to T&. |
| 600 | template <typename T, |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 601 | typename std::enable_if<!std::is_lvalue_reference<T>::value>::type* = |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 602 | nullptr> |
| 603 | static inline internal::PassedWrapper<T> Passed(T&& scoper) { |
| 604 | return internal::PassedWrapper<T>(std::move(scoper)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 605 | } |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 606 | template <typename T> |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 607 | static inline internal::PassedWrapper<T> Passed(T* scoper) { |
danakj | a7589e7 | 2015-12-08 09:44:41 +0900 | [diff] [blame] | 608 | return internal::PassedWrapper<T>(std::move(*scoper)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 609 | } |
| 610 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 611 | template <typename T> |
| 612 | static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { |
| 613 | return internal::IgnoreResultHelper<T>(data); |
| 614 | } |
| 615 | |
| 616 | template <typename T> |
| 617 | static inline internal::IgnoreResultHelper<Callback<T> > |
| 618 | IgnoreResult(const Callback<T>& data) { |
| 619 | return internal::IgnoreResultHelper<Callback<T> >(data); |
| 620 | } |
| 621 | |
ajwong@chromium.org | e4f3dc3 | 2012-01-07 07:12:28 +0900 | [diff] [blame] | 622 | BASE_EXPORT void DoNothing(); |
| 623 | |
| 624 | template<typename T> |
| 625 | void DeletePointer(T* obj) { |
| 626 | delete obj; |
| 627 | } |
| 628 | |
tzik | c44f1fd | 2016-06-14 22:17:31 +0900 | [diff] [blame] | 629 | // An injection point to control |this| pointer behavior on a method invocation. |
| 630 | // If IsWeakReceiver<> is true_type for |T| and |T| is used for a receiver of a |
| 631 | // method, base::Bind cancels the method invocation if the receiver is tested as |
| 632 | // false. |
| 633 | // E.g. Foo::bar() is not called: |
| 634 | // struct Foo : base::SupportsWeakPtr<Foo> { |
| 635 | // void bar() {} |
| 636 | // }; |
| 637 | // |
| 638 | // WeakPtr<Foo> oo = nullptr; |
| 639 | // base::Bind(&Foo::bar, oo).Run(); |
| 640 | template <typename T> |
| 641 | struct IsWeakReceiver : std::false_type {}; |
| 642 | |
| 643 | template <typename T> |
| 644 | struct IsWeakReceiver<internal::ConstRefWrapper<T>> : IsWeakReceiver<T> {}; |
| 645 | |
| 646 | template <typename T> |
| 647 | struct IsWeakReceiver<WeakPtr<T>> : std::true_type {}; |
| 648 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 649 | } // namespace base |
| 650 | |
| 651 | #endif // BASE_BIND_HELPERS_H_ |