security/sandbox/chromium/base/bind_helpers.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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

mercurial