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