Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1906)

Unified Diff: base/bind_helpers.h

Issue 8048008: Add new helper that can adapt Callbacks with return values for Closures. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comments. Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/bind_helpers.h
diff --git a/base/bind_helpers.h b/base/bind_helpers.h
index e4b4a174dcba980af63b664f52a8df5ff98d87d0..7eb5716caef4f7b566f0026ad57f91d7a373ace7 100644
--- a/base/bind_helpers.h
+++ b/base/bind_helpers.h
@@ -6,17 +6,22 @@
// can be used specify the refcounting and reference semantics of arguments
// that are bound by the Bind() function in base/bind.h.
//
-// The public functions are base::Unretained() and base::ConstRef().
+// The public functions are base::Unretained(), base::ConstRef(), and
+// base::IgnoreReturn().
+//
// Unretained() allows Bind() to bind a non-refcounted class.
// ConstRef() allows binding a constant reference to an argument rather
// than a copy.
+// IgnoreReturn() is used to adapt a 0-argument Callback with a return type to
+// a Closure. This is useful if you need to PostTask with a function that has
+// a return value that you don't care about.
//
//
// EXAMPLE OF Unretained():
//
// class Foo {
// public:
-// void func() { cout << "Foo:f" << endl;
+// void func() { cout << "Foo:f" << endl; }
// };
//
// // In some function somewhere.
@@ -29,7 +34,7 @@
// to compile because Foo does not support the AddRef() and Release() methods.
//
//
-// EXAMPLE OF ConstRef();
+// EXAMPLE OF ConstRef():
// void foo(int arg) { cout << arg << endl }
//
// int n = 1;
@@ -46,12 +51,21 @@
// Note that because ConstRef() takes a reference on |n|, |n| must outlive all
// its bound callbacks.
//
+//
+// EXAMPLE OF IgnoreReturn():
+// int DoSomething(int arg) { cout << arg << endl; }
+// Callback<int(void)> cb = Bind(&DoSomething, 1);
+// Closure c = IgnoreReturn(cb); // Prints "1"
+// or
+// ml->PostTask(FROM_HERE, IgnoreReturn(cb)); // Prints "1" on |ml|
#ifndef BASE_BIND_HELPERS_H_
#define BASE_BIND_HELPERS_H_
#pragma once
#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/template_util.h"
@@ -255,6 +269,11 @@ struct MaybeRefcount<base::true_type, WeakPtr<T> > {
static void Release(const WeakPtr<T>&) {}
};
+template <typename R>
+void VoidReturnAdapter(Callback<R(void)> callback) {
+ callback.Run();
+}
+
} // namespace internal
template <typename T>
@@ -267,6 +286,11 @@ inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
return internal::ConstRefWrapper<T>(o);
}
+template <typename R>
+Closure IgnoreReturn(Callback<R(void)> callback) {
+ return Bind(&internal::VoidReturnAdapter<R>, callback);
+}
+
} // namespace base
#endif // BASE_BIND_HELPERS_H_
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698