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

Unified Diff: webrtc/base/bind.h.pump

Issue 1300523004: rtc::Bind: Capture method objects as scoped_refptr if they are ref counted (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: landable patch Created 5 years, 4 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
Index: webrtc/base/bind.h.pump
diff --git a/webrtc/base/bind.h.pump b/webrtc/base/bind.h.pump
index 11767abe5044443ea282c09d26ca9264cc117b02..216adc24e44e310a4e5e71cb7fe0f18afb0ae388 100644
--- a/webrtc/base/bind.h.pump
+++ b/webrtc/base/bind.h.pump
@@ -12,12 +12,14 @@
// /home/build/google3/third_party/gtest/scripts/pump.py bind.h.pump
// Bind() is an overloaded function that converts method calls into function
-// objects (aka functors). It captures any arguments to the method by value
-// when Bind is called, producing a stateful, nullary function object. Care
-// should be taken about the lifetime of objects captured by Bind(); the
-// returned functor knows nothing about the lifetime of the method's object or
-// any arguments passed by pointer, and calling the functor with a destroyed
-// object will surely do bad things.
+// objects (aka functors). The method object is captured as a scoped_refptr<> if
+// the class inherits from RefCountInterface, and as a raw pointer otherwise.
+// Any arguments to the method are captured by value. The return value of Bind
+// is a stateful, nullary function object. Care should be taken about the
+// lifetime of objects captured by Bind(); the returned functor knows nothing
+// about the lifetime of a non ref-counted method object or any arguments passed
+// by pointer, and calling the functor with a destroyed object will surely do
+// bad things.
//
// Example usage:
// struct Foo {
@@ -34,10 +36,32 @@
// cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl;
// cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl;
// }
+//
+// Example usage for ref counted objects:
+// struct Bar : public rtc::RefCountInterface {
+// void Test() {}
+// void BindThis() {
+// // The functor passed to AsyncInvoke() will keep this object alive.
+// invoker.AsyncInvoke(rtc::Bind(&Bar::Test, this));
+// }
+// };
+//
+// int main() {
+// rtc::scoped_refptr<Bar> bar = new rtc::RefCountedObject<Bar>();
+// auto functor = rtc::Bind(&Bar::Test, bar);
+// bar = nullptr;
+// // The functor stores an internal scoped_refptr<Bar>, so this is safe.
+// functor();
+// }
+//
#ifndef WEBRTC_BASE_BIND_H_
#define WEBRTC_BASE_BIND_H_
+#include "webrtc/base/refcount.h"
+#include "webrtc/base/scoped_ref_ptr.h"
+#include "webrtc/base/template_util.h"
+
#define NONAME
namespace rtc {
@@ -49,6 +73,39 @@ namespace detail {
// references stripped. This trick allows the compiler to dictate the Bind
// parameter types rather than deduce them.
template <class T> struct identity { typedef T type; };
+
+// IsRefCounted<T>::value is a static bool that represents if T implements
+// RefCountInterface. rtc::is_convertible<> does the heavy lifting. It is a
+// substitute for C++11 std::is_convertible<>.
+template <class T>
+struct IsRefCounted {
+ static const bool value = is_convertible<T*, RefCountInterface*>::value;
+};
+
+// TernaryTypeOperator is a helper class to select a type based on a static bool
+// value.
+template <bool condition, typename IfTrueT, typename IfFalseT>
+struct TernaryTypeOperator {};
+
+template <typename IfTrueT, typename IfFalseT>
+struct TernaryTypeOperator<true, IfTrueT, IfFalseT> {
+ typedef IfTrueT type;
+};
+
+template <typename IfTrueT, typename IfFalseT>
+struct TernaryTypeOperator<false, IfTrueT, IfFalseT> {
+ typedef IfFalseT type;
+};
+
+// PointerType<T>::type will be scoped_refptr<T> for ref counted types, and T*
+// otherwise.
+template <class T>
+struct PointerType {
+ typedef typename TernaryTypeOperator<IsRefCounted<T>::value,
+ scoped_refptr<T>,
+ T*>::type type;
+};
+
} // namespace detail
$var n = 6
@@ -68,7 +125,7 @@ class MethodFunctor$i {
return (object_->*method_)($for j , [[p$(j)_]]); }
private:
MethodT method_;
- ObjectT* object_;$for j [[
+ typename detail::PointerType<ObjectT>::type object_;$for j [[
P$j p$(j)_;]]
@@ -116,6 +173,18 @@ Bind(FP_T(method), const ObjectT* object$for j [[,
}
#undef FP_T
+#define FP_T(x) R (ObjectT::*x)($for j , [[P$j]])
+
+template <class ObjectT, class R$for j [[,
+ class P$j]]>
+MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>
+Bind(FP_T(method), const scoped_refptr<ObjectT>& object$for j [[,
+ typename detail::identity<P$j>::type p$j]]) {
+ return MethodFunctor$i<ObjectT, FP_T(NONAME), R$for j [[, P$j]]>(
+ method, object.get()$for j [[, p$j]]);
+}
+
+#undef FP_T
#define FP_T(x) R (*x)($for j , [[P$j]])
template <class R$for j [[,
« webrtc/base/bind.h ('K') | « webrtc/base/bind.h ('k') | webrtc/base/bind_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698