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

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: Remove silly comment + address tommi@s comments 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
« no previous file with comments | « webrtc/base/bind.h ('k') | webrtc/base/bind_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/bind.h.pump
diff --git a/webrtc/base/bind.h.pump b/webrtc/base/bind.h.pump
index 11767abe5044443ea282c09d26ca9264cc117b02..9a4bc664c39145c416c13f01073f3da10ced71f4 100644
--- a/webrtc/base/bind.h.pump
+++ b/webrtc/base/bind.h.pump
@@ -12,12 +12,13 @@
// /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
+// possible, 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 +35,33 @@
// cout << rtc::Bind(&Foo::Test3, &foo, 3)() << endl;
// cout << rtc::Bind(&Foo::Test4, &foo, 7, 8.5f)() << endl;
// }
+//
+// Example usage of ref counted objects:
+// struct Bar {
+// int AddRef();
+// int Release();
+//
+// 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/scoped_ref_ptr.h"
+
#define NONAME
namespace rtc {
@@ -49,6 +73,57 @@ 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 will be true for types that can be used in
+// rtc::scoped_refptr<T>, i.e. types that implements nullary functions AddRef()
+// and Release(), regardless of their return types. AddRef() and Release() can
+// be defined in T or any superclass of T.
+template <typename T>
+class IsRefCounted {
+ // This is a complex implementation detail done with SFINAE.
+
+ // Define types such that sizeof(Yes) != sizeof(No).
+ struct Yes { char dummy[1]; };
+ struct No { char dummy[2]; };
+ // Define two overloaded template functions with return types of different
+ // size. This way, we can use sizeof() on the return type to determine which
+ // function the compiler would have chosen. One function will be preferred
+ // over the other if it is possible to create it without compiler errors,
+ // otherwise the compiler will simply remove it, and default to the less
+ // preferred function.
+ template <typename R>
+ static Yes test(R* r, decltype(r->AddRef(), r->Release(), 42));
+ template <typename C> static No test(...);
+
+public:
+ // Trick the compiler to tell if it's possible to call AddRef() and Release().
+ static const bool value = sizeof(test<T>((T*)nullptr, 42)) == sizeof(Yes);
+};
+
+// 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 +143,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 +191,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 [[,
« no previous file with comments | « 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