henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains Macros for creating proxies for webrtc MediaStream and |
| 12 | // PeerConnection classes. |
| 13 | |
| 14 | // |
| 15 | // Example usage: |
| 16 | // |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 17 | // class TestInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | // public: |
| 19 | // std::string FooA() = 0; |
| 20 | // std::string FooB(bool arg1) const = 0; |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 21 | // std::string FooC(bool arg1) = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | // }; |
| 23 | // |
| 24 | // Note that return types can not be a const reference. |
| 25 | // |
| 26 | // class Test : public TestInterface { |
| 27 | // ... implementation of the interface. |
| 28 | // }; |
| 29 | // |
| 30 | // BEGIN_PROXY_MAP(Test) |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 31 | // PROXY_SIGNALING_THREAD_DESTRUCTOR() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | // PROXY_METHOD0(std::string, FooA) |
| 33 | // PROXY_CONSTMETHOD1(std::string, FooB, arg1) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 34 | // PROXY_WORKER_METHOD1(std::string, FooC, arg1) |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 35 | // END_PROXY_MAP() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | // |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 37 | // Where the destructor and first two methods are invoked on the signaling |
| 38 | // thread, and the third is invoked on the worker thread. |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 39 | // |
| 40 | // The proxy can be created using |
| 41 | // |
| 42 | // TestProxy::Create(Thread* signaling_thread, Thread* worker_thread, |
| 43 | // TestInterface*). |
| 44 | // |
| 45 | // The variant defined with BEGIN_SIGNALING_PROXY_MAP is unaware of |
| 46 | // the worker thread, and invokes all methods on the signaling thread. |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 47 | // |
| 48 | // The variant defined with BEGIN_OWNED_PROXY_MAP does not use |
| 49 | // refcounting, and instead just takes ownership of the object being proxied. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 51 | #ifndef WEBRTC_API_PROXY_H_ |
| 52 | #define WEBRTC_API_PROXY_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 54 | #include <memory> |
| 55 | |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 56 | #include "webrtc/base/event.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 57 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | |
| 59 | namespace webrtc { |
| 60 | |
| 61 | template <typename R> |
| 62 | class ReturnType { |
| 63 | public: |
| 64 | template<typename C, typename M> |
| 65 | void Invoke(C* c, M m) { r_ = (c->*m)(); } |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 66 | template <typename C, typename M, typename T1> |
| 67 | void Invoke(C* c, M m, T1 a1) { |
| 68 | r_ = (c->*m)(std::move(a1)); |
| 69 | } |
| 70 | template <typename C, typename M, typename T1, typename T2> |
| 71 | void Invoke(C* c, M m, T1 a1, T2 a2) { |
| 72 | r_ = (c->*m)(std::move(a1), std::move(a2)); |
| 73 | } |
| 74 | template <typename C, typename M, typename T1, typename T2, typename T3> |
| 75 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { |
| 76 | r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3)); |
| 77 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 78 | template<typename C, typename M, typename T1, typename T2, typename T3, |
| 79 | typename T4> |
| 80 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4) { |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 81 | r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4)); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 82 | } |
| 83 | template<typename C, typename M, typename T1, typename T2, typename T3, |
| 84 | typename T4, typename T5> |
| 85 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 86 | r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4), |
| 87 | std::move(a5)); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 88 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 90 | R moved_result() { return std::move(r_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | R r_; |
| 94 | }; |
| 95 | |
| 96 | template <> |
| 97 | class ReturnType<void> { |
| 98 | public: |
| 99 | template<typename C, typename M> |
| 100 | void Invoke(C* c, M m) { (c->*m)(); } |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 101 | template <typename C, typename M, typename T1> |
| 102 | void Invoke(C* c, M m, T1 a1) { |
| 103 | (c->*m)(std::move(a1)); |
| 104 | } |
| 105 | template <typename C, typename M, typename T1, typename T2> |
| 106 | void Invoke(C* c, M m, T1 a1, T2 a2) { |
| 107 | (c->*m)(std::move(a1), std::move(a2)); |
| 108 | } |
| 109 | template <typename C, typename M, typename T1, typename T2, typename T3> |
| 110 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { |
| 111 | (c->*m)(std::move(a1), std::move(a2), std::move(a3)); |
| 112 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 114 | void moved_result() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 117 | namespace internal { |
| 118 | |
| 119 | class SynchronousMethodCall |
| 120 | : public rtc::MessageData, |
| 121 | public rtc::MessageHandler { |
| 122 | public: |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 123 | explicit SynchronousMethodCall(rtc::MessageHandler* proxy) |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 124 | : e_(), proxy_(proxy) {} |
| 125 | ~SynchronousMethodCall() {} |
| 126 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 127 | void Invoke(const rtc::Location& posted_from, rtc::Thread* t) { |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 128 | if (t->IsCurrent()) { |
| 129 | proxy_->OnMessage(NULL); |
| 130 | } else { |
| 131 | e_.reset(new rtc::Event(false, false)); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 132 | t->Post(posted_from, this, 0); |
andresp@webrtc.org | 53d9012 | 2015-02-09 14:19:09 +0000 | [diff] [blame] | 133 | e_->Wait(rtc::Event::kForever); |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | void OnMessage(rtc::Message*) { proxy_->OnMessage(NULL); e_->Set(); } |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 139 | std::unique_ptr<rtc::Event> e_; |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 140 | rtc::MessageHandler* proxy_; |
| 141 | }; |
| 142 | |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 143 | } // namespace internal |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 144 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | template <typename C, typename R> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 146 | class MethodCall0 : public rtc::Message, |
| 147 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | public: |
| 149 | typedef R (C::*Method)(); |
| 150 | MethodCall0(C* c, Method m) : c_(c), m_(m) {} |
| 151 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 152 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 153 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 154 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 +0000 | [diff] [blame] | 158 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | |
| 160 | C* c_; |
| 161 | Method m_; |
| 162 | ReturnType<R> r_; |
| 163 | }; |
| 164 | |
| 165 | template <typename C, typename R> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 166 | class ConstMethodCall0 : public rtc::Message, |
| 167 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 168 | public: |
| 169 | typedef R (C::*Method)() const; |
| 170 | ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {} |
| 171 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 172 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 173 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 174 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 178 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | |
| 180 | C* c_; |
| 181 | Method m_; |
| 182 | ReturnType<R> r_; |
| 183 | }; |
| 184 | |
| 185 | template <typename C, typename R, typename T1> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 186 | class MethodCall1 : public rtc::Message, |
| 187 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 188 | public: |
| 189 | typedef R (C::*Method)(T1 a1); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 190 | MethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 191 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 192 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 193 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 194 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 198 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | |
| 200 | C* c_; |
| 201 | Method m_; |
| 202 | ReturnType<R> r_; |
| 203 | T1 a1_; |
| 204 | }; |
| 205 | |
| 206 | template <typename C, typename R, typename T1> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 207 | class ConstMethodCall1 : public rtc::Message, |
| 208 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 209 | public: |
| 210 | typedef R (C::*Method)(T1 a1) const; |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 211 | ConstMethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 213 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 214 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 215 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 219 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 220 | |
| 221 | C* c_; |
| 222 | Method m_; |
| 223 | ReturnType<R> r_; |
| 224 | T1 a1_; |
| 225 | }; |
| 226 | |
| 227 | template <typename C, typename R, typename T1, typename T2> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 228 | class MethodCall2 : public rtc::Message, |
| 229 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | public: |
| 231 | typedef R (C::*Method)(T1 a1, T2 a2); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 232 | MethodCall2(C* c, Method m, T1 a1, T2 a2) |
| 233 | : c_(c), m_(m), a1_(std::move(a1)), a2_(std::move(a2)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 235 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 236 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 237 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 241 | void OnMessage(rtc::Message*) { |
| 242 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_)); |
| 243 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | |
| 245 | C* c_; |
| 246 | Method m_; |
| 247 | ReturnType<R> r_; |
| 248 | T1 a1_; |
| 249 | T2 a2_; |
| 250 | }; |
| 251 | |
| 252 | template <typename C, typename R, typename T1, typename T2, typename T3> |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 253 | class MethodCall3 : public rtc::Message, |
| 254 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | public: |
| 256 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3); |
| 257 | MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3) |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 258 | : c_(c), |
| 259 | m_(m), |
| 260 | a1_(std::move(a1)), |
| 261 | a2_(std::move(a2)), |
| 262 | a3_(std::move(a3)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 263 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 264 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 265 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 266 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 270 | void OnMessage(rtc::Message*) { |
| 271 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_)); |
| 272 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | |
| 274 | C* c_; |
| 275 | Method m_; |
| 276 | ReturnType<R> r_; |
| 277 | T1 a1_; |
| 278 | T2 a2_; |
| 279 | T3 a3_; |
| 280 | }; |
| 281 | |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 282 | template <typename C, typename R, typename T1, typename T2, typename T3, |
| 283 | typename T4> |
| 284 | class MethodCall4 : public rtc::Message, |
| 285 | public rtc::MessageHandler { |
| 286 | public: |
| 287 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4); |
| 288 | MethodCall4(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4) |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 289 | : c_(c), |
| 290 | m_(m), |
| 291 | a1_(std::move(a1)), |
| 292 | a2_(std::move(a2)), |
| 293 | a3_(std::move(a3)), |
| 294 | a4_(std::move(a4)) {} |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 295 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 296 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 297 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 298 | return r_.moved_result(); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 302 | void OnMessage(rtc::Message*) { |
| 303 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), |
| 304 | std::move(a4_)); |
| 305 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 306 | |
| 307 | C* c_; |
| 308 | Method m_; |
| 309 | ReturnType<R> r_; |
| 310 | T1 a1_; |
| 311 | T2 a2_; |
| 312 | T3 a3_; |
| 313 | T4 a4_; |
| 314 | }; |
| 315 | |
| 316 | template <typename C, typename R, typename T1, typename T2, typename T3, |
| 317 | typename T4, typename T5> |
| 318 | class MethodCall5 : public rtc::Message, |
| 319 | public rtc::MessageHandler { |
| 320 | public: |
| 321 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); |
| 322 | MethodCall5(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 323 | : c_(c), |
| 324 | m_(m), |
| 325 | a1_(std::move(a1)), |
| 326 | a2_(std::move(a2)), |
| 327 | a3_(std::move(a3)), |
| 328 | a4_(std::move(a4)), |
| 329 | a5_(std::move(a5)) {} |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 330 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 331 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 332 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 333 | return r_.moved_result(); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private: |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 337 | void OnMessage(rtc::Message*) { |
| 338 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), |
| 339 | std::move(a4_), std::move(a5_)); |
| 340 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 341 | |
| 342 | C* c_; |
| 343 | Method m_; |
| 344 | ReturnType<R> r_; |
| 345 | T1 a1_; |
| 346 | T2 a2_; |
| 347 | T3 a3_; |
| 348 | T4 a4_; |
| 349 | T5 a5_; |
| 350 | }; |
| 351 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 352 | // Helper macros to reduce code duplication. |
| 353 | #define PROXY_MAP_BOILERPLATE(c) \ |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 354 | template <class INTERNAL_CLASS> \ |
| 355 | class c##ProxyWithInternal; \ |
| 356 | typedef c##ProxyWithInternal<c##Interface> c##Proxy; \ |
| 357 | template <class INTERNAL_CLASS> \ |
| 358 | class c##ProxyWithInternal : public c##Interface { \ |
| 359 | protected: \ |
| 360 | typedef c##Interface C; \ |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 361 | \ |
| 362 | public: \ |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 363 | const INTERNAL_CLASS* internal() const { return c_.get(); } \ |
| 364 | INTERNAL_CLASS* internal() { return c_.get(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 366 | #define SIGNALING_PROXY_MAP_BOILERPLATE(c) \ |
| 367 | protected: \ |
| 368 | c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \ |
| 369 | : signaling_thread_(signaling_thread), c_(c) {} \ |
| 370 | \ |
| 371 | private: \ |
| 372 | mutable rtc::Thread* signaling_thread_; |
| 373 | |
| 374 | #define WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 375 | protected: \ |
| 376 | c##ProxyWithInternal(rtc::Thread* signaling_thread, \ |
| 377 | rtc::Thread* worker_thread, INTERNAL_CLASS* c) \ |
| 378 | : signaling_thread_(signaling_thread), \ |
| 379 | worker_thread_(worker_thread), \ |
| 380 | c_(c) {} \ |
| 381 | \ |
| 382 | private: \ |
| 383 | mutable rtc::Thread* signaling_thread_; \ |
| 384 | mutable rtc::Thread* worker_thread_; |
| 385 | |
| 386 | // Note that the destructor is protected so that the proxy can only be |
| 387 | // destroyed via RefCountInterface. |
| 388 | #define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 389 | protected: \ |
| 390 | ~c##ProxyWithInternal() { \ |
| 391 | MethodCall0<c##ProxyWithInternal, void> call( \ |
| 392 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 393 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 394 | } \ |
| 395 | \ |
| 396 | private: \ |
| 397 | void DestroyInternal() { c_ = nullptr; } \ |
| 398 | rtc::scoped_refptr<INTERNAL_CLASS> c_; |
| 399 | |
| 400 | #define OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 401 | public: \ |
| 402 | ~c##ProxyWithInternal() { \ |
| 403 | MethodCall0<c##ProxyWithInternal, void> call( \ |
| 404 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 405 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 406 | } \ |
| 407 | \ |
| 408 | private: \ |
| 409 | void DestroyInternal() { c_.reset(nullptr); } \ |
| 410 | std::unique_ptr<INTERNAL_CLASS> c_; |
| 411 | |
| 412 | #define BEGIN_SIGNALING_PROXY_MAP(c) \ |
| 413 | PROXY_MAP_BOILERPLATE(c) \ |
| 414 | SIGNALING_PROXY_MAP_BOILERPLATE(c) \ |
| 415 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 416 | public: \ |
| 417 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 418 | rtc::Thread* signaling_thread, INTERNAL_CLASS* c) { \ |
| 419 | return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \ |
| 420 | c); \ |
| 421 | } |
| 422 | |
| 423 | #define BEGIN_PROXY_MAP(c) \ |
| 424 | PROXY_MAP_BOILERPLATE(c) \ |
| 425 | WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 426 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 427 | public: \ |
| 428 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 429 | rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \ |
| 430 | INTERNAL_CLASS* c) { \ |
| 431 | return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \ |
| 432 | worker_thread, c); \ |
| 433 | } |
| 434 | |
| 435 | #define BEGIN_OWNED_PROXY_MAP(c) \ |
| 436 | PROXY_MAP_BOILERPLATE(c) \ |
| 437 | WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 438 | OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 439 | public: \ |
| 440 | static std::unique_ptr<c##ProxyWithInternal> Create( \ |
| 441 | rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \ |
| 442 | INTERNAL_CLASS* c) { \ |
| 443 | return std::unique_ptr<c##ProxyWithInternal>( \ |
| 444 | new c##ProxyWithInternal(signaling_thread, worker_thread, c)); \ |
| 445 | } |
| 446 | |
| 447 | #define PROXY_SIGNALING_THREAD_DESTRUCTOR() \ |
| 448 | private: \ |
| 449 | rtc::Thread* destructor_thread() const { return signaling_thread_; } \ |
| 450 | \ |
| 451 | public: |
| 452 | |
| 453 | #define PROXY_WORKER_THREAD_DESTRUCTOR() \ |
| 454 | private: \ |
| 455 | rtc::Thread* destructor_thread() const { return worker_thread_; } \ |
| 456 | \ |
| 457 | public: |
| 458 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 459 | #define PROXY_METHOD0(r, method) \ |
| 460 | r method() override { \ |
| 461 | MethodCall0<C, r> call(c_.get(), &C::method); \ |
| 462 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 463 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 465 | #define PROXY_CONSTMETHOD0(r, method) \ |
| 466 | r method() const override { \ |
| 467 | ConstMethodCall0<C, r> call(c_.get(), &C::method); \ |
| 468 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 469 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 470 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 471 | #define PROXY_METHOD1(r, method, t1) \ |
| 472 | r method(t1 a1) override { \ |
| 473 | MethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \ |
| 474 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 475 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 477 | #define PROXY_CONSTMETHOD1(r, method, t1) \ |
| 478 | r method(t1 a1) const override { \ |
| 479 | ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \ |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 480 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 481 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 482 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 483 | #define PROXY_METHOD2(r, method, t1, t2) \ |
| 484 | r method(t1 a1, t2 a2) override { \ |
| 485 | MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \ |
| 486 | std::move(a2)); \ |
| 487 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 488 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 489 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 490 | #define PROXY_METHOD3(r, method, t1, t2, t3) \ |
| 491 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 492 | MethodCall3<C, r, t1, t2, t3> call(c_.get(), &C::method, std::move(a1), \ |
| 493 | std::move(a2), std::move(a3)); \ |
| 494 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
| 495 | } |
| 496 | |
| 497 | #define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ |
| 498 | r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ |
| 499 | MethodCall4<C, r, t1, t2, t3, t4> call(c_.get(), &C::method, \ |
| 500 | std::move(a1), std::move(a2), \ |
| 501 | std::move(a3), std::move(a4)); \ |
| 502 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
| 503 | } |
| 504 | |
| 505 | #define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ |
| 506 | r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ |
| 507 | MethodCall5<C, r, t1, t2, t3, t4, t5> call( \ |
| 508 | c_.get(), &C::method, std::move(a1), std::move(a2), std::move(a3), \ |
| 509 | std::move(a4), std::move(a5)); \ |
| 510 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // Define methods which should be invoked on the worker thread. |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 514 | #define PROXY_WORKER_METHOD0(r, method) \ |
| 515 | r method() override { \ |
| 516 | MethodCall0<C, r> call(c_.get(), &C::method); \ |
| 517 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 518 | } |
| 519 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 520 | #define PROXY_WORKER_CONSTMETHOD0(r, method) \ |
| 521 | r method() const override { \ |
| 522 | ConstMethodCall0<C, r> call(c_.get(), &C::method); \ |
| 523 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 524 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame] | 525 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 526 | #define PROXY_WORKER_METHOD1(r, method, t1) \ |
| 527 | r method(t1 a1) override { \ |
| 528 | MethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \ |
| 529 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 530 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 531 | |
deadbeef | d99a200 | 2017-01-18 08:55:23 -0800 | [diff] [blame^] | 532 | #define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \ |
| 533 | r method(t1 a1) const override { \ |
| 534 | ConstMethodCall1<C, r, t1> call(c_.get(), &C::method, std::move(a1)); \ |
| 535 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 536 | } |
| 537 | |
| 538 | #define PROXY_WORKER_METHOD2(r, method, t1, t2) \ |
| 539 | r method(t1 a1, t2 a2) override { \ |
| 540 | MethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \ |
| 541 | std::move(a2)); \ |
| 542 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 543 | } |
| 544 | |
| 545 | #define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \ |
| 546 | r method(t1 a1, t2 a2) const override { \ |
| 547 | ConstMethodCall2<C, r, t1, t2> call(c_.get(), &C::method, std::move(a1), \ |
| 548 | std::move(a2)); \ |
| 549 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 550 | } |
| 551 | |
| 552 | #define END_PROXY_MAP() \ |
| 553 | } \ |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 554 | ; |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 555 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 556 | } // namespace webrtc |
| 557 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 558 | #endif // WEBRTC_API_PROXY_H_ |