[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 | // |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 31 | // ConstRef() allows binding a constant reference to an argument rather |
| 32 | // than a copy. |
| 33 | // |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 34 | // IgnoreResult() is used to adapt a function or Callback with a return type to |
| 35 | // one with a void return. This is most useful if you have a function with, |
| 36 | // say, a pesky ignorable bool return that you want to use with PostTask or |
| 37 | // something else that expect a Callback with a void return. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 38 | // |
| 39 | // EXAMPLE OF Unretained(): |
| 40 | // |
| 41 | // class Foo { |
| 42 | // public: |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 43 | // void func() { cout << "Foo:f" << endl; } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 44 | // }; |
| 45 | // |
| 46 | // // In some function somewhere. |
| 47 | // Foo foo; |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 48 | // Closure foo_callback = |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 49 | // Bind(&Foo::func, Unretained(&foo)); |
| 50 | // foo_callback.Run(); // Prints "Foo:f". |
| 51 | // |
| 52 | // Without the Unretained() wrapper on |&foo|, the above call would fail |
| 53 | // to compile because Foo does not support the AddRef() and Release() methods. |
| 54 | // |
| 55 | // |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 56 | // EXAMPLE OF Owned(): |
| 57 | // |
| 58 | // void foo(int* arg) { cout << *arg << endl } |
| 59 | // |
| 60 | // int* pn = new int(1); |
| 61 | // Closure foo_callback = Bind(&foo, Owned(pn)); |
| 62 | // |
| 63 | // foo_callback.Run(); // Prints "1" |
| 64 | // foo_callback.Run(); // Prints "1" |
| 65 | // *n = 2; |
| 66 | // foo_callback.Run(); // Prints "2" |
| 67 | // |
| 68 | // foo_callback.Reset(); // |pn| is deleted. Also will happen when |
| 69 | // // |foo_callback| goes out of scope. |
| 70 | // |
| 71 | // Without Owned(), someone would have to know to delete |pn| when the last |
| 72 | // reference to the Callback is deleted. |
| 73 | // |
| 74 | // |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 75 | // EXAMPLE OF ConstRef(): |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 76 | // |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 77 | // void foo(int arg) { cout << arg << endl } |
| 78 | // |
| 79 | // int n = 1; |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 80 | // Closure no_ref = Bind(&foo, n); |
| 81 | // Closure has_ref = Bind(&foo, ConstRef(n)); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 82 | // |
| 83 | // no_ref.Run(); // Prints "1" |
| 84 | // has_ref.Run(); // Prints "1" |
| 85 | // |
| 86 | // n = 2; |
| 87 | // no_ref.Run(); // Prints "1" |
| 88 | // has_ref.Run(); // Prints "2" |
| 89 | // |
| 90 | // Note that because ConstRef() takes a reference on |n|, |n| must outlive all |
| 91 | // its bound callbacks. |
| 92 | // |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 93 | // |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 94 | // EXAMPLE OF IgnoreResult(): |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 95 | // |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 96 | // int DoSomething(int arg) { cout << arg << endl; } |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 97 | // |
| 98 | // // Assign to a Callback with a void return type. |
| 99 | // Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething)); |
| 100 | // cb->Run(1); // Prints "1". |
| 101 | // |
| 102 | // // Prints "1" on |ml|. |
| 103 | // ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1); |
| 104 | // |
| 105 | // |
| 106 | // EXAMPLE OF Passed(): |
| 107 | // |
| 108 | // void TakesOwnership(scoped_ptr<Foo> arg) { } |
| 109 | // scoped_ptr<Foo> CreateFoo() { return scoped_ptr<Foo>(new Foo()); } |
| 110 | // |
| 111 | // scoped_ptr<Foo> f(new Foo()); |
| 112 | // |
| 113 | // // |cb| is given ownership of Foo(). |f| is now NULL. |
| 114 | // // You can use f.Pass() in place of &f, but it's more verbose. |
| 115 | // Closure cb = Bind(&TakesOwnership, Passed(&f)); |
| 116 | // |
| 117 | // // Run was never called so |cb| still owns Foo() and deletes |
| 118 | // // it on Reset(). |
| 119 | // cb.Reset(); |
| 120 | // |
| 121 | // // |cb| is given a new Foo created by CreateFoo(). |
| 122 | // cb = Bind(&TakesOwnership, Passed(CreateFoo())); |
| 123 | // |
| 124 | // // |arg| in TakesOwnership() is given ownership of Foo(). |cb| |
| 125 | // // no longer owns Foo() and, if reset, would not delete Foo(). |
| 126 | // cb.Run(); // Foo() is now transferred to |arg| and deleted. |
| 127 | // cb.Run(); // This CHECK()s since Foo() already been used once. |
| 128 | // |
| 129 | // Passed() is particularly useful with PostTask() when you are transferring |
| 130 | // ownership of an argument into a task, but don't necessarily know if the |
| 131 | // task will always be executed. This can happen if the task is cancellable |
| 132 | // or if it is posted to a MessageLoopProxy. |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 133 | // |
| 134 | // |
| 135 | // SIMPLE FUNCTIONS AND UTILITIES. |
| 136 | // |
| 137 | // DoNothing() - Useful for creating a Closure that does nothing when called. |
| 138 | // DeletePointer<T>() - Useful for creating a Closure that will delete a |
| 139 | // pointer when invoked. Only use this when necessary. |
| 140 | // In most cases MessageLoop::DeleteSoon() is a better |
| 141 | // fit. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 142 | |
| 143 | #ifndef BASE_BIND_HELPERS_H_ |
| 144 | #define BASE_BIND_HELPERS_H_ |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 145 | |
| 146 | #include "base/basictypes.h" |
[email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 147 | #include "base/callback.h" |
[email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 148 | #include "base/memory/weak_ptr.h" |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 149 | #include "base/template_util.h" |
| 150 | |
| 151 | namespace base { |
| 152 | namespace internal { |
| 153 | |
| 154 | // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T |
| 155 | // for the existence of AddRef() and Release() functions of the correct |
| 156 | // signature. |
| 157 | // |
| 158 | // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error |
| 159 | // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence |
| 160 | // http://stackoverflow.com/questions/4358584/sfinae-approach-comparison |
| 161 | // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions |
| 162 | // |
| 163 | // The last link in particular show the method used below. |
| 164 | // |
| 165 | // For SFINAE to work with inherited methods, we need to pull some extra tricks |
| 166 | // with multiple inheritance. In the more standard formulation, the overloads |
| 167 | // of Check would be: |
| 168 | // |
| 169 | // template <typename C> |
| 170 | // Yes NotTheCheckWeWant(Helper<&C::TargetFunc>*); |
| 171 | // |
| 172 | // template <typename C> |
| 173 | // No NotTheCheckWeWant(...); |
| 174 | // |
| 175 | // static const bool value = sizeof(NotTheCheckWeWant<T>(0)) == sizeof(Yes); |
| 176 | // |
| 177 | // The problem here is that template resolution will not match |
| 178 | // C::TargetFunc if TargetFunc does not exist directly in C. That is, if |
| 179 | // TargetFunc in inherited from an ancestor, &C::TargetFunc will not match, |
| 180 | // |value| will be false. This formulation only checks for whether or |
| 181 | // not TargetFunc exist directly in the class being introspected. |
| 182 | // |
| 183 | // To get around this, we play a dirty trick with multiple inheritance. |
| 184 | // First, We create a class BaseMixin that declares each function that we |
| 185 | // want to probe for. Then we create a class Base that inherits from both T |
| 186 | // (the class we wish to probe) and BaseMixin. Note that the function |
| 187 | // signature in BaseMixin does not need to match the signature of the function |
| 188 | // we are probing for; thus it's easiest to just use void(void). |
| 189 | // |
| 190 | // Now, if TargetFunc exists somewhere in T, then &Base::TargetFunc has an |
| 191 | // ambiguous resolution between BaseMixin and T. This lets us write the |
| 192 | // following: |
| 193 | // |
| 194 | // template <typename C> |
| 195 | // No GoodCheck(Helper<&C::TargetFunc>*); |
| 196 | // |
| 197 | // template <typename C> |
| 198 | // Yes GoodCheck(...); |
| 199 | // |
| 200 | // static const bool value = sizeof(GoodCheck<Base>(0)) == sizeof(Yes); |
| 201 | // |
| 202 | // Notice here that the variadic version of GoodCheck() returns Yes here |
| 203 | // instead of No like the previous one. Also notice that we calculate |value| |
| 204 | // by specializing GoodCheck() on Base instead of T. |
| 205 | // |
| 206 | // We've reversed the roles of the variadic, and Helper overloads. |
| 207 | // GoodCheck(Helper<&C::TargetFunc>*), when C = Base, fails to be a valid |
| 208 | // substitution if T::TargetFunc exists. Thus GoodCheck<Base>(0) will resolve |
| 209 | // to the variadic version if T has TargetFunc. If T::TargetFunc does not |
| 210 | // exist, then &C::TargetFunc is not ambiguous, and the overload resolution |
| 211 | // will prefer GoodCheck(Helper<&C::TargetFunc>*). |
| 212 | // |
| 213 | // This method of SFINAE will correctly probe for inherited names, but it cannot |
| 214 | // typecheck those names. It's still a good enough sanity check though. |
| 215 | // |
| 216 | // Works on gcc-4.2, gcc-4.4, and Visual Studio 2008. |
| 217 | // |
| 218 | // TODO(ajwong): Move to ref_counted.h or template_util.h when we've vetted |
| 219 | // this works well. |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 220 | // |
| 221 | // TODO(ajwong): Make this check for Release() as well. |
| 222 | // See http://crbug.com/82038. |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 223 | template <typename T> |
| 224 | class SupportsAddRefAndRelease { |
| 225 | typedef char Yes[1]; |
| 226 | typedef char No[2]; |
| 227 | |
| 228 | struct BaseMixin { |
| 229 | void AddRef(); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 230 | }; |
| 231 | |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 232 | // MSVC warns when you try to use Base if T has a private destructor, the |
| 233 | // common pattern for refcounted types. It does this even though no attempt to |
| 234 | // instantiate Base is made. We disable the warning for this definition. |
| 235 | #if defined(OS_WIN) |
[email protected] | 793b6c2 | 2013-07-31 05:22:02 | [diff] [blame] | 236 | #pragma warning(push) |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 237 | #pragma warning(disable:4624) |
| 238 | #endif |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 239 | struct Base : public T, public BaseMixin { |
| 240 | }; |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 241 | #if defined(OS_WIN) |
[email protected] | 793b6c2 | 2013-07-31 05:22:02 | [diff] [blame] | 242 | #pragma warning(pop) |
[email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 243 | #endif |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 244 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 245 | template <void(BaseMixin::*)(void)> struct Helper {}; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 246 | |
| 247 | template <typename C> |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 248 | static No& Check(Helper<&C::AddRef>*); |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 249 | |
| 250 | template <typename > |
| 251 | static Yes& Check(...); |
| 252 | |
| 253 | public: |
[email protected] | e160b44 | 2014-08-01 22:44:13 | [diff] [blame] | 254 | enum { value = sizeof(Check<Base>(0)) == sizeof(Yes) }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 255 | }; |
| 256 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 257 | // Helpers to assert that arguments of a recounted type are bound with a |
| 258 | // scoped_refptr. |
| 259 | template <bool IsClasstype, typename T> |
| 260 | struct UnsafeBindtoRefCountedArgHelper : false_type { |
| 261 | }; |
| 262 | |
| 263 | template <typename T> |
| 264 | struct UnsafeBindtoRefCountedArgHelper<true, T> |
| 265 | : integral_constant<bool, SupportsAddRefAndRelease<T>::value> { |
| 266 | }; |
| 267 | |
| 268 | template <typename T> |
[email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 269 | struct UnsafeBindtoRefCountedArg : false_type { |
| 270 | }; |
| 271 | |
| 272 | template <typename T> |
| 273 | struct UnsafeBindtoRefCountedArg<T*> |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 274 | : UnsafeBindtoRefCountedArgHelper<is_class<T>::value, T> { |
| 275 | }; |
| 276 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 277 | template <typename T> |
| 278 | class HasIsMethodTag { |
| 279 | typedef char Yes[1]; |
| 280 | typedef char No[2]; |
| 281 | |
| 282 | template <typename U> |
| 283 | static Yes& Check(typename U::IsMethod*); |
| 284 | |
| 285 | template <typename U> |
| 286 | static No& Check(...); |
| 287 | |
| 288 | public: |
[email protected] | e160b44 | 2014-08-01 22:44:13 | [diff] [blame] | 289 | enum { value = sizeof(Check<T>(0)) == sizeof(Yes) }; |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 290 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 291 | |
| 292 | template <typename T> |
| 293 | class UnretainedWrapper { |
| 294 | public: |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 295 | explicit UnretainedWrapper(T* o) : ptr_(o) {} |
| 296 | T* get() const { return ptr_; } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 297 | private: |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 298 | T* ptr_; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | template <typename T> |
| 302 | class ConstRefWrapper { |
| 303 | public: |
| 304 | explicit ConstRefWrapper(const T& o) : ptr_(&o) {} |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 305 | const T& get() const { return *ptr_; } |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 306 | private: |
| 307 | const T* ptr_; |
| 308 | }; |
| 309 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 310 | template <typename T> |
| 311 | struct IgnoreResultHelper { |
| 312 | explicit IgnoreResultHelper(T functor) : functor_(functor) {} |
| 313 | |
| 314 | T functor_; |
| 315 | }; |
| 316 | |
| 317 | template <typename T> |
| 318 | struct IgnoreResultHelper<Callback<T> > { |
| 319 | explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} |
| 320 | |
| 321 | const Callback<T>& functor_; |
| 322 | }; |
| 323 | |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 324 | // An alternate implementation is to avoid the destructive copy, and instead |
| 325 | // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
| 326 | // a class that is essentially a scoped_ptr<>. |
| 327 | // |
| 328 | // The current implementation has the benefit though of leaving ParamTraits<> |
| 329 | // fully in callback_internal.h as well as avoiding type conversions during |
| 330 | // storage. |
| 331 | template <typename T> |
| 332 | class OwnedWrapper { |
| 333 | public: |
| 334 | explicit OwnedWrapper(T* o) : ptr_(o) {} |
| 335 | ~OwnedWrapper() { delete ptr_; } |
| 336 | T* get() const { return ptr_; } |
| 337 | OwnedWrapper(const OwnedWrapper& other) { |
| 338 | ptr_ = other.ptr_; |
| 339 | other.ptr_ = NULL; |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | mutable T* ptr_; |
| 344 | }; |
| 345 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 346 | // PassedWrapper is a copyable adapter for a scoper that ignores const. |
| 347 | // |
| 348 | // It is needed to get around the fact that Bind() takes a const reference to |
| 349 | // all its arguments. Because Bind() takes a const reference to avoid |
| 350 | // unnecessary copies, it is incompatible with movable-but-not-copyable |
| 351 | // types; doing a destructive "move" of the type into Bind() would violate |
| 352 | // the const correctness. |
| 353 | // |
| 354 | // This conundrum cannot be solved without either C++11 rvalue references or |
| 355 | // a O(2^n) blowup of Bind() templates to handle each combination of regular |
| 356 | // types and movable-but-not-copyable types. Thus we introduce a wrapper type |
| 357 | // that is copyable to transmit the correct type information down into |
| 358 | // BindState<>. Ignoring const in this type makes sense because it is only |
| 359 | // created when we are explicitly trying to do a destructive move. |
| 360 | // |
| 361 | // Two notes: |
| 362 | // 1) PassedWrapper supports any type that has a "Pass()" function. |
| 363 | // This is intentional. The whitelisting of which specific types we |
| 364 | // support is maintained by CallbackParamTraits<>. |
| 365 | // 2) is_valid_ is distinct from NULL because it is valid to bind a "NULL" |
| 366 | // scoper to a Callback and allow the Callback to execute once. |
| 367 | template <typename T> |
| 368 | class PassedWrapper { |
| 369 | public: |
| 370 | explicit PassedWrapper(T scoper) : is_valid_(true), scoper_(scoper.Pass()) {} |
| 371 | PassedWrapper(const PassedWrapper& other) |
| 372 | : is_valid_(other.is_valid_), scoper_(other.scoper_.Pass()) { |
| 373 | } |
| 374 | T Pass() const { |
| 375 | CHECK(is_valid_); |
| 376 | is_valid_ = false; |
| 377 | return scoper_.Pass(); |
| 378 | } |
| 379 | |
| 380 | private: |
| 381 | mutable bool is_valid_; |
| 382 | mutable T scoper_; |
| 383 | }; |
| 384 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 385 | // Unwrap the stored parameters for the wrappers above. |
| 386 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 387 | struct UnwrapTraits { |
| 388 | typedef const T& ForwardType; |
| 389 | static ForwardType Unwrap(const T& o) { return o; } |
| 390 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 391 | |
| 392 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 393 | struct UnwrapTraits<UnretainedWrapper<T> > { |
| 394 | typedef T* ForwardType; |
| 395 | static ForwardType Unwrap(UnretainedWrapper<T> unretained) { |
| 396 | return unretained.get(); |
| 397 | } |
| 398 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 399 | |
| 400 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 401 | struct UnwrapTraits<ConstRefWrapper<T> > { |
| 402 | typedef const T& ForwardType; |
| 403 | static ForwardType Unwrap(ConstRefWrapper<T> const_ref) { |
| 404 | return const_ref.get(); |
| 405 | } |
| 406 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 407 | |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 408 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 409 | struct UnwrapTraits<scoped_refptr<T> > { |
| 410 | typedef T* ForwardType; |
| 411 | static ForwardType Unwrap(const scoped_refptr<T>& o) { return o.get(); } |
| 412 | }; |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 413 | |
| 414 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 415 | struct UnwrapTraits<WeakPtr<T> > { |
| 416 | typedef const WeakPtr<T>& ForwardType; |
| 417 | static ForwardType Unwrap(const WeakPtr<T>& o) { return o; } |
| 418 | }; |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 419 | |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 420 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 421 | struct UnwrapTraits<OwnedWrapper<T> > { |
| 422 | typedef T* ForwardType; |
| 423 | static ForwardType Unwrap(const OwnedWrapper<T>& o) { |
| 424 | return o.get(); |
| 425 | } |
| 426 | }; |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 427 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 428 | template <typename T> |
| 429 | struct UnwrapTraits<PassedWrapper<T> > { |
| 430 | typedef T ForwardType; |
| 431 | static T Unwrap(PassedWrapper<T>& o) { |
| 432 | return o.Pass(); |
| 433 | } |
| 434 | }; |
| 435 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 436 | // Utility for handling different refcounting semantics in the Bind() |
| 437 | // function. |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 438 | template <bool is_method, typename T> |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 439 | struct MaybeRefcount; |
| 440 | |
| 441 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 442 | struct MaybeRefcount<false, T> { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 443 | static void AddRef(const T&) {} |
| 444 | static void Release(const T&) {} |
| 445 | }; |
| 446 | |
| 447 | template <typename T, size_t n> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 448 | struct MaybeRefcount<false, T[n]> { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 449 | static void AddRef(const T*) {} |
| 450 | static void Release(const T*) {} |
| 451 | }; |
| 452 | |
| 453 | template <typename T> |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 454 | struct MaybeRefcount<true, T> { |
| 455 | static void AddRef(const T&) {} |
| 456 | static void Release(const T&) {} |
| 457 | }; |
| 458 | |
| 459 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 460 | struct MaybeRefcount<true, T*> { |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 461 | static void AddRef(T* o) { o->AddRef(); } |
| 462 | static void Release(T* o) { o->Release(); } |
| 463 | }; |
| 464 | |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 465 | // No need to additionally AddRef() and Release() since we are storing a |
| 466 | // scoped_refptr<> inside the storage object already. |
| 467 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 468 | struct MaybeRefcount<true, scoped_refptr<T> > { |
[email protected] | 7a15d117 | 2011-10-07 00:25:29 | [diff] [blame] | 469 | static void AddRef(const scoped_refptr<T>& o) {} |
| 470 | static void Release(const scoped_refptr<T>& o) {} |
| 471 | }; |
| 472 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 473 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 474 | struct MaybeRefcount<true, const T*> { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 475 | static void AddRef(const T* o) { o->AddRef(); } |
| 476 | static void Release(const T* o) { o->Release(); } |
| 477 | }; |
| 478 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 479 | // 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] | 480 | // method. It is used internally by Bind() to select the correct |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 481 | // InvokeHelper that will no-op itself in the event the WeakPtr<> for |
| 482 | // the target object is invalidated. |
| 483 | // |
| 484 | // P1 should be the type of the object that will be received of the method. |
| 485 | template <bool IsMethod, typename P1> |
| 486 | struct IsWeakMethod : public false_type {}; |
| 487 | |
| 488 | template <typename T> |
| 489 | struct IsWeakMethod<true, WeakPtr<T> > : public true_type {}; |
| 490 | |
| 491 | template <typename T> |
| 492 | struct IsWeakMethod<true, ConstRefWrapper<WeakPtr<T> > > : public true_type {}; |
| 493 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 494 | } // namespace internal |
| 495 | |
| 496 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 497 | static inline internal::UnretainedWrapper<T> Unretained(T* o) { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 498 | return internal::UnretainedWrapper<T>(o); |
| 499 | } |
| 500 | |
| 501 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 502 | static inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 503 | return internal::ConstRefWrapper<T>(o); |
| 504 | } |
| 505 | |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 506 | template <typename T> |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 507 | static inline internal::OwnedWrapper<T> Owned(T* o) { |
[email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 508 | return internal::OwnedWrapper<T>(o); |
| 509 | } |
| 510 | |
[email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 511 | // We offer 2 syntaxes for calling Passed(). The first takes a temporary and |
| 512 | // is best suited for use with the return value of a function. The second |
| 513 | // takes a pointer to the scoper and is just syntactic sugar to avoid having |
| 514 | // to write Passed(scoper.Pass()). |
| 515 | template <typename T> |
| 516 | static inline internal::PassedWrapper<T> Passed(T scoper) { |
| 517 | return internal::PassedWrapper<T>(scoper.Pass()); |
| 518 | } |
| 519 | template <typename T> |
| 520 | static inline internal::PassedWrapper<T> Passed(T* scoper) { |
| 521 | return internal::PassedWrapper<T>(scoper->Pass()); |
| 522 | } |
| 523 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 524 | template <typename T> |
| 525 | static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) { |
| 526 | return internal::IgnoreResultHelper<T>(data); |
| 527 | } |
| 528 | |
| 529 | template <typename T> |
| 530 | static inline internal::IgnoreResultHelper<Callback<T> > |
| 531 | IgnoreResult(const Callback<T>& data) { |
| 532 | return internal::IgnoreResultHelper<Callback<T> >(data); |
| 533 | } |
| 534 | |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 535 | BASE_EXPORT void DoNothing(); |
| 536 | |
| 537 | template<typename T> |
| 538 | void DeletePointer(T* obj) { |
| 539 | delete obj; |
| 540 | } |
| 541 | |
[email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 542 | } // namespace base |
| 543 | |
| 544 | #endif // BASE_BIND_HELPERS_H_ |