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