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

Unified Diff: webrtc/api/proxy.h

Issue 2628343003: Adding some features to proxy.h, and restructuring the macros. (Closed)
Patch Set: Created 3 years, 11 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/api/proxy.h
diff --git a/webrtc/api/proxy.h b/webrtc/api/proxy.h
index 746a758ba4315181cfb04688487fa1d70179eb0c..c776ecc88ac257e1fcfe5913884985d5992e8c3a 100644
--- a/webrtc/api/proxy.h
+++ b/webrtc/api/proxy.h
@@ -28,13 +28,14 @@
// };
//
// BEGIN_PROXY_MAP(Test)
+// PROXY_SIGNALING_THREAD_DESTRUCTOR()
pthatcher1 2017/01/17 19:44:00 What happens if one doesn't include this line? Wo
Taylor Brandstetter 2017/01/18 01:17:14 You'd get a compile error: "use of undeclared iden
// PROXY_METHOD0(std::string, FooA)
// PROXY_CONSTMETHOD1(std::string, FooB, arg1)
// PROXY_WORKER_METHOD1(std::string, FooC, arg1)
-// END_PROXY()
+// END_PROXY_MAP()
//
-// where the first two methods are invoked on the signaling thread,
-// and the third is invoked on the worker thread.
+// Where the destructor and first two methods are invoked on the signaling
+// thread, and the third is invoked on the worker thread.
//
// The proxy can be created using
//
@@ -43,6 +44,9 @@
//
// The variant defined with BEGIN_SIGNALING_PROXY_MAP is unaware of
// the worker thread, and invokes all methods on the signaling thread.
+//
+// The variant defined with BEGIN_NON_REFCOUNTED_PROXY_MAP does not use
pthatcher1 2017/01/17 19:44:00 Would BEGIN_OWNED_PROXY_MAP be a better name?
Taylor Brandstetter 2017/01/18 01:17:14 Done.
+// refcounting, and instead just takes ownership of the object being proxied.
#ifndef WEBRTC_API_PROXY_H_
#define WEBRTC_API_PROXY_H_
@@ -306,33 +310,8 @@ class MethodCall5 : public rtc::Message,
T5 a5_;
};
-#define BEGIN_SIGNALING_PROXY_MAP(c) \
- template <class INTERNAL_CLASS> \
- class c##ProxyWithInternal; \
- typedef c##ProxyWithInternal<c##Interface> c##Proxy; \
- template <class INTERNAL_CLASS> \
- class c##ProxyWithInternal : public c##Interface { \
- protected: \
- typedef c##Interface C; \
- c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \
- : signaling_thread_(signaling_thread), c_(c) {} \
- ~c##ProxyWithInternal() { \
- MethodCall0<c##ProxyWithInternal, void> call( \
- this, &c##ProxyWithInternal::Release_s); \
- call.Marshal(RTC_FROM_HERE, signaling_thread_); \
- } \
- \
- public: \
- static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
- rtc::Thread* signaling_thread, \
- INTERNAL_CLASS* c) { \
- return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
- c); \
- } \
- const INTERNAL_CLASS* internal() const { return c_.get(); } \
- INTERNAL_CLASS* internal() { return c_.get(); }
-
-#define BEGIN_PROXY_MAP(c) \
+// Helper macros to reduce code duplication.
+#define PROXY_MAP_BOILERPLATE(c) \
template <class INTERNAL_CLASS> \
class c##ProxyWithInternal; \
typedef c##ProxyWithInternal<c##Interface> c##Proxy; \
@@ -340,29 +319,104 @@ class MethodCall5 : public rtc::Message,
class c##ProxyWithInternal : public c##Interface { \
protected: \
typedef c##Interface C; \
- c##ProxyWithInternal(rtc::Thread* signaling_thread, \
- rtc::Thread* worker_thread, \
- INTERNAL_CLASS* c) \
- : signaling_thread_(signaling_thread), \
- worker_thread_(worker_thread), \
- c_(c) {} \
- ~c##ProxyWithInternal() { \
- MethodCall0<c##ProxyWithInternal, void> call( \
- this, &c##ProxyWithInternal::Release_s); \
- call.Marshal(RTC_FROM_HERE, signaling_thread_); \
- } \
\
public: \
- static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
- rtc::Thread* signaling_thread, \
- rtc::Thread* worker_thread, \
- INTERNAL_CLASS* c) { \
- return new rtc::RefCountedObject<c##ProxyWithInternal>( \
- signaling_thread, worker_thread, c); \
- } \
const INTERNAL_CLASS* internal() const { return c_.get(); } \
INTERNAL_CLASS* internal() { return c_.get(); }
+#define SIGNALING_PROXY_MAP_BOILERPLATE(c) \
+ protected: \
+ c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \
+ : signaling_thread_(signaling_thread), c_(c) {} \
+ \
+ private: \
+ mutable rtc::Thread* signaling_thread_;
+
+#define WORKER_PROXY_MAP_BOILERPLATE(c) \
+ protected: \
+ c##ProxyWithInternal(rtc::Thread* signaling_thread, \
+ rtc::Thread* worker_thread, INTERNAL_CLASS* c) \
+ : signaling_thread_(signaling_thread), \
+ worker_thread_(worker_thread), \
+ c_(c) {} \
+ \
+ private: \
+ mutable rtc::Thread* signaling_thread_; \
+ mutable rtc::Thread* worker_thread_;
+
+// Note that the destructor is protected so that the proxy can only be
+// destroyed via RefCountInterface.
+#define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
+ protected: \
+ ~c##ProxyWithInternal() { \
+ MethodCall0<c##ProxyWithInternal, void> call( \
+ this, &c##ProxyWithInternal::DestroyInternal); \
+ call.Marshal(RTC_FROM_HERE, destructor_thread()); \
+ } \
+ \
+ private: \
+ void DestroyInternal() { c_ = nullptr; } \
+ rtc::scoped_refptr<INTERNAL_CLASS> c_;
+
+#define NON_REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
+ public: \
+ ~c##ProxyWithInternal() { \
+ MethodCall0<c##ProxyWithInternal, void> call( \
+ this, &c##ProxyWithInternal::DestroyInternal); \
+ call.Marshal(RTC_FROM_HERE, destructor_thread()); \
+ } \
+ \
+ private: \
+ void DestroyInternal() { c_.reset(nullptr); } \
+ std::unique_ptr<INTERNAL_CLASS> c_;
+
+#define BEGIN_SIGNALING_PROXY_MAP(c) \
+ PROXY_MAP_BOILERPLATE(c) \
+ SIGNALING_PROXY_MAP_BOILERPLATE(c) \
+ REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
+ public: \
+ static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
+ rtc::Thread* signaling_thread, INTERNAL_CLASS* c) { \
+ return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
+ c); \
+ }
+
+#define BEGIN_PROXY_MAP(c) \
+ PROXY_MAP_BOILERPLATE(c) \
+ WORKER_PROXY_MAP_BOILERPLATE(c) \
+ REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
+ public: \
+ static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
+ rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
+ INTERNAL_CLASS* c) { \
+ return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
+ worker_thread, c); \
+ }
+
+#define BEGIN_NON_REFCOUNTED_PROXY_MAP(c) \
+ PROXY_MAP_BOILERPLATE(c) \
+ WORKER_PROXY_MAP_BOILERPLATE(c) \
+ NON_REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
+ public: \
+ static std::unique_ptr<c##ProxyWithInternal> Create( \
+ rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
+ INTERNAL_CLASS* c) { \
+ return std::unique_ptr<c##ProxyWithInternal>( \
+ new c##ProxyWithInternal(signaling_thread, worker_thread, c)); \
+ }
+
+#define PROXY_SIGNALING_THREAD_DESTRUCTOR() \
+ private: \
+ rtc::Thread* destructor_thread() const { return signaling_thread_; } \
+ \
+ public:
+
+#define PROXY_WORKER_THREAD_DESTRUCTOR() \
+ private: \
+ rtc::Thread* destructor_thread() const { return worker_thread_; } \
+ \
+ public:
+
#define PROXY_METHOD0(r, method) \
r method() override { \
MethodCall0<C, r> call(c_.get(), &C::method); \
@@ -414,33 +468,44 @@ class MethodCall5 : public rtc::Message,
}
// Define methods which should be invoked on the worker thread.
+#define PROXY_WORKER_METHOD0(r, method) \
+ r method() override { \
+ MethodCall0<C, r> call(c_.get(), &C::method); \
+ return call.Marshal(RTC_FROM_HERE, worker_thread_); \
+ }
+
+#define PROXY_WORKER_CONSTMETHOD0(r, method) \
+ r method() const override { \
+ ConstMethodCall0<C, r> call(c_.get(), &C::method); \
+ return call.Marshal(RTC_FROM_HERE, worker_thread_); \
+ }
+
#define PROXY_WORKER_METHOD1(r, method, t1) \
r method(t1 a1) override { \
MethodCall1<C, r, t1> call(c_.get(), &C::method, a1); \
return call.Marshal(RTC_FROM_HERE, worker_thread_); \
}
+#define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \
+ r method(t1 a1) const override { \
+ ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, a1); \
+ return call.Marshal(RTC_FROM_HERE, worker_thread_); \
+ }
+
#define PROXY_WORKER_METHOD2(r, method, t1, t2) \
r method(t1 a1, t2 a2) override { \
MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, a1, a2); \
return call.Marshal(RTC_FROM_HERE, worker_thread_); \
}
-#define END_SIGNALING_PROXY() \
- private: \
- void Release_s() { c_ = NULL; } \
- mutable rtc::Thread* signaling_thread_; \
- rtc::scoped_refptr<INTERNAL_CLASS> c_; \
- } \
- ;
+#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \
+ r method(t1 a1, t2 a2) const override { \
+ ConstMethodCall2<C, r, t1, t2> call(c_.get(), &C::method, a1, a2); \
+ return call.Marshal(RTC_FROM_HERE, worker_thread_); \
+ }
-#define END_PROXY() \
- private: \
- void Release_s() { c_ = NULL; } \
- mutable rtc::Thread* signaling_thread_; \
- mutable rtc::Thread* worker_thread_; \
- rtc::scoped_refptr<INTERNAL_CLASS> c_; \
- } \
+#define END_PROXY_MAP() \
+ } \
;
} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698