From c363982eeab3445ec960f26eea8653d3eb8c9fed Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Tue, 26 Nov 2019 15:27:50 -0800 Subject: [PATCH] Convert proxy.h helper classes to variadic templates Bug: None Change-Id: I74f4e24a8c8b5a124782e8c8294a0673acef4faf Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/160741 Commit-Queue: Steve Anton Reviewed-by: Bjorn Mellem Cr-Commit-Position: refs/heads/master@{#29925} --- api/proxy.h | 385 +++++++++------------------------- pc/peer_connection_factory.cc | 2 +- 2 files changed, 96 insertions(+), 291 deletions(-) diff --git a/api/proxy.h b/api/proxy.h index 3e76ee7c5e..7ed14d03ea 100644 --- a/api/proxy.h +++ b/api/proxy.h @@ -54,6 +54,7 @@ #include #include +#include #include #include "api/scoped_refptr.h" @@ -73,41 +74,9 @@ namespace webrtc { template class ReturnType { public: - template - void Invoke(C* c, M m) { - r_ = (c->*m)(); - } - template - void Invoke(C* c, M m, T1 a1) { - r_ = (c->*m)(std::move(a1)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2) { - r_ = (c->*m)(std::move(a1), std::move(a2)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { - r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4) { - r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4), - std::move(a5)); + template + void Invoke(C* c, M m, Args&&... args) { + r_ = (c->*m)(std::forward(args)...); } R moved_result() { return std::move(r_); } @@ -119,21 +88,9 @@ class ReturnType { template <> class ReturnType { public: - template - void Invoke(C* c, M m) { - (c->*m)(); - } - template - void Invoke(C* c, M m, T1 a1) { - (c->*m)(std::move(a1)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2) { - (c->*m)(std::move(a1), std::move(a2)); - } - template - void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { - (c->*m)(std::move(a1), std::move(a2), std::move(a3)); + template + void Invoke(C* c, M m, Args&&... args) { + (c->*m)(std::forward(args)...); } void moved_result() {} @@ -158,118 +115,14 @@ class RTC_EXPORT SynchronousMethodCall : public rtc::MessageData, } // namespace internal -template -class MethodCall0 : public rtc::Message, public rtc::MessageHandler { +template +class MethodCall : public rtc::Message, public rtc::MessageHandler { public: - typedef R (C::*Method)(); - MethodCall0(C* c, Method m) : c_(c), m_(m) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } - - C* c_; - Method m_; - ReturnType r_; -}; - -template -class ConstMethodCall0 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)() const; - ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } - - C* c_; - Method m_; - ReturnType r_; -}; - -template -class MethodCall1 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)(T1 a1); - MethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } - - C* c_; - Method m_; - ReturnType r_; - T1 a1_; -}; - -template -class ConstMethodCall1 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)(T1 a1) const; - ConstMethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } - - C* c_; - Method m_; - ReturnType r_; - T1 a1_; -}; - -template -class MethodCall2 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)(T1 a1, T2 a2); - MethodCall2(C* c, Method m, T1 a1, T2 a2) - : c_(c), m_(m), a1_(std::move(a1)), a2_(std::move(a2)) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { - r_.Invoke(c_, m_, std::move(a1_), std::move(a2_)); - } - - C* c_; - Method m_; - ReturnType r_; - T1 a1_; - T2 a2_; -}; - -template -class MethodCall3 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)(T1 a1, T2 a2, T3 a3); - MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3) + typedef R (C::*Method)(Args...); + MethodCall(C* c, Method m, Args&&... args) : c_(c), m_(m), - a1_(std::move(a1)), - a2_(std::move(a2)), - a3_(std::move(a3)) {} + args_(std::forward_as_tuple(std::forward(args)...)) {} R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { internal::SynchronousMethodCall(this).Invoke(posted_from, t); @@ -277,34 +130,27 @@ class MethodCall3 : public rtc::Message, public rtc::MessageHandler { } private: - void OnMessage(rtc::Message*) { - r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_)); + void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for()); } + + template + void Invoke(std::index_sequence) { + r_.Invoke(c_, m_, std::move(std::get(args_))...); } C* c_; Method m_; ReturnType r_; - T1 a1_; - T2 a2_; - T3 a3_; + std::tuple args_; }; -template -class MethodCall4 : public rtc::Message, public rtc::MessageHandler { +template +class ConstMethodCall : public rtc::Message, public rtc::MessageHandler { public: - typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4); - MethodCall4(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4) + typedef R (C::*Method)(Args...) const; + ConstMethodCall(const C* c, Method m, Args&&... args) : c_(c), m_(m), - a1_(std::move(a1)), - a2_(std::move(a2)), - a3_(std::move(a3)), - a4_(std::move(a4)) {} + args_(std::forward_as_tuple(std::forward(args)...)) {} R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { internal::SynchronousMethodCall(this).Invoke(posted_from, t); @@ -312,58 +158,17 @@ class MethodCall4 : public rtc::Message, public rtc::MessageHandler { } private: - void OnMessage(rtc::Message*) { - r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), - std::move(a4_)); + void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for()); } + + template + void Invoke(std::index_sequence) { + r_.Invoke(c_, m_, std::move(std::get(args_))...); } - C* c_; + const C* c_; Method m_; ReturnType r_; - T1 a1_; - T2 a2_; - T3 a3_; - T4 a4_; -}; - -template -class MethodCall5 : public rtc::Message, public rtc::MessageHandler { - public: - typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); - MethodCall5(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) - : c_(c), - m_(m), - a1_(std::move(a1)), - a2_(std::move(a2)), - a3_(std::move(a3)), - a4_(std::move(a4)), - a5_(std::move(a5)) {} - - R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { - internal::SynchronousMethodCall(this).Invoke(posted_from, t); - return r_.moved_result(); - } - - private: - void OnMessage(rtc::Message*) { - r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), - std::move(a4_), std::move(a5_)); - } - - C* c_; - Method m_; - ReturnType r_; - T1 a1_; - T2 a2_; - T3 a3_; - T4 a4_; - T5 a5_; + std::tuple args_; }; // Helper macros to reduce code duplication. @@ -412,7 +217,7 @@ class MethodCall5 : public rtc::Message, public rtc::MessageHandler { #define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ protected: \ ~c##ProxyWithInternal() { \ - MethodCall0 call( \ + MethodCall call( \ this, &c##ProxyWithInternal::DestroyInternal); \ call.Marshal(RTC_FROM_HERE, destructor_thread()); \ } \ @@ -429,7 +234,7 @@ class MethodCall5 : public rtc::Message, public rtc::MessageHandler { #define OWNED_PROXY_MAP_BOILERPLATE(c) \ public: \ ~c##ProxyWithInternal() { \ - MethodCall0 call( \ + MethodCall call( \ this, &c##ProxyWithInternal::DestroyInternal); \ call.Marshal(RTC_FROM_HERE, destructor_thread()); \ } \ @@ -487,109 +292,109 @@ class MethodCall5 : public rtc::Message, public rtc::MessageHandler { #define PROXY_METHOD0(r, method) \ r method() override { \ - MethodCall0 call(c_, &C::method); \ + MethodCall call(c_, &C::method); \ return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } #define PROXY_CONSTMETHOD0(r, method) \ r method() const override { \ - ConstMethodCall0 call(c_, &C::method); \ + ConstMethodCall call(c_, &C::method); \ return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_METHOD1(r, method, t1) \ - r method(t1 a1) override { \ - MethodCall1 call(c_, &C::method, std::move(a1)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_METHOD1(r, method, t1) \ + r method(t1 a1) override { \ + MethodCall call(c_, &C::method, std::move(a1)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_CONSTMETHOD1(r, method, t1) \ - r method(t1 a1) const override { \ - ConstMethodCall1 call(c_, &C::method, std::move(a1)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_CONSTMETHOD1(r, method, t1) \ + r method(t1 a1) const override { \ + ConstMethodCall call(c_, &C::method, std::move(a1)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_METHOD2(r, method, t1, t2) \ - r method(t1 a1, t2 a2) override { \ - MethodCall2 call(c_, &C::method, std::move(a1), \ - std::move(a2)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_METHOD2(r, method, t1, t2) \ + r method(t1 a1, t2 a2) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_METHOD3(r, method, t1, t2, t3) \ - r method(t1 a1, t2 a2, t3 a3) override { \ - MethodCall3 call(c_, &C::method, std::move(a1), \ - std::move(a2), std::move(a3)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_METHOD3(r, method, t1, t2, t3) \ + r method(t1 a1, t2 a2, t3 a3) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2), std::move(a3)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ - r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ - MethodCall4 call(c_, &C::method, std::move(a1), \ - std::move(a2), std::move(a3), \ - std::move(a4)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ + r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2), std::move(a3), \ + std::move(a4)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } -#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ - r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ - MethodCall5 call(c_, &C::method, std::move(a1), \ - std::move(a2), std::move(a3), \ - std::move(a4), std::move(a5)); \ - return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ +#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ + r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2), std::move(a3), \ + std::move(a4), std::move(a5)); \ + return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ } // Define methods which should be invoked on the worker thread. #define PROXY_WORKER_METHOD0(r, method) \ r method() override { \ - MethodCall0 call(c_, &C::method); \ + MethodCall call(c_, &C::method); \ return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } #define PROXY_WORKER_CONSTMETHOD0(r, method) \ r method() const override { \ - ConstMethodCall0 call(c_, &C::method); \ + ConstMethodCall call(c_, &C::method); \ return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } -#define PROXY_WORKER_METHOD1(r, method, t1) \ - r method(t1 a1) override { \ - MethodCall1 call(c_, &C::method, std::move(a1)); \ - return call.Marshal(RTC_FROM_HERE, worker_thread_); \ +#define PROXY_WORKER_METHOD1(r, method, t1) \ + r method(t1 a1) override { \ + MethodCall call(c_, &C::method, std::move(a1)); \ + return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } -#define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \ - r method(t1 a1) const override { \ - ConstMethodCall1 call(c_, &C::method, std::move(a1)); \ - return call.Marshal(RTC_FROM_HERE, worker_thread_); \ +#define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \ + r method(t1 a1) const override { \ + ConstMethodCall call(c_, &C::method, std::move(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 call(c_, &C::method, std::move(a1), \ - std::move(a2)); \ - return call.Marshal(RTC_FROM_HERE, worker_thread_); \ +#define PROXY_WORKER_METHOD2(r, method, t1, t2) \ + r method(t1 a1, t2 a2) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2)); \ + return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } -#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \ - r method(t1 a1, t2 a2) const override { \ - ConstMethodCall2 call(c_, &C::method, std::move(a1), \ - std::move(a2)); \ - return call.Marshal(RTC_FROM_HERE, worker_thread_); \ - } - -#define PROXY_WORKER_METHOD3(r, method, t1, t2, t3) \ - r method(t1 a1, t2 a2, t3 a3) override { \ - MethodCall3 call(c_, &C::method, std::move(a1), \ - std::move(a2), std::move(a3)); \ +#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \ + r method(t1 a1, t2 a2) const override { \ + ConstMethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2)); \ return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } -#define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2) \ - r method(t1 a1, t2 a2, t3 a3) const override { \ - ConstMethodCall3 call(c_, &C::method, std::move(a1), \ - std::move(a2), std::move(a3)); \ - return call.Marshal(RTC_FROM_HERE, worker_thread_); \ +#define PROXY_WORKER_METHOD3(r, method, t1, t2, t3) \ + r method(t1 a1, t2 a2, t3 a3) override { \ + MethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2), std::move(a3)); \ + return call.Marshal(RTC_FROM_HERE, worker_thread_); \ + } + +#define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2) \ + r method(t1 a1, t2 a2, t3 a3) const override { \ + ConstMethodCall call(c_, &C::method, std::move(a1), \ + std::move(a2), std::move(a3)); \ + return call.Marshal(RTC_FROM_HERE, worker_thread_); \ } } // namespace webrtc diff --git a/pc/peer_connection_factory.cc b/pc/peer_connection_factory.cc index 1e9c24e977..534c89274e 100644 --- a/pc/peer_connection_factory.cc +++ b/pc/peer_connection_factory.cc @@ -54,7 +54,7 @@ CreateModularPeerConnectionFactory( std::move(dependencies))); // Call Initialize synchronously but make sure it is executed on // |signaling_thread|. - MethodCall0 call( + MethodCall call( pc_factory.get(), &PeerConnectionFactory::Initialize); bool result = call.Marshal(RTC_FROM_HERE, pc_factory->signaling_thread());