henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [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 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains Macros for creating proxies for webrtc MediaStream and |
| 12 | // PeerConnection classes. |
deadbeef | b10f32f | 2017-02-08 09:38:21 | [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 | [diff] [blame] | 14 | |
| 15 | // |
| 16 | // Example usage: |
| 17 | // |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 18 | // class TestInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 19 | // public: |
| 20 | // std::string FooA() = 0; |
| 21 | // std::string FooB(bool arg1) const = 0; |
nisse | 72c8d2b | 2016-04-15 10:49:07 | [diff] [blame] | 22 | // std::string FooC(bool arg1) = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [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 16:55:23 | [diff] [blame] | 32 | // PROXY_SIGNALING_THREAD_DESTRUCTOR() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 33 | // PROXY_METHOD0(std::string, FooA) |
| 34 | // PROXY_CONSTMETHOD1(std::string, FooB, arg1) |
nisse | 72c8d2b | 2016-04-15 10:49:07 | [diff] [blame] | 35 | // PROXY_WORKER_METHOD1(std::string, FooC, arg1) |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 36 | // END_PROXY_MAP() |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 37 | // |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [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 10:49:07 | [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 16:55:23 | [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 | [diff] [blame] | 51 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 52 | #ifndef API_PROXY_H_ |
| 53 | #define API_PROXY_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 54 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 55 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 56 | #include <string> |
oprypin | 803dc29 | 2017-02-01 09:55:59 | [diff] [blame] | 57 | #include <utility> |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 58 | |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 59 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 60 | #include "rtc_base/event.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 61 | #include "rtc_base/message_handler.h" |
| 62 | #include "rtc_base/message_queue.h" |
| 63 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 64 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 65 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 66 | namespace rtc { |
| 67 | class Location; |
| 68 | } |
| 69 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 70 | namespace webrtc { |
| 71 | |
| 72 | template <typename R> |
| 73 | class ReturnType { |
| 74 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 75 | template <typename C, typename M> |
| 76 | void Invoke(C* c, M m) { |
| 77 | r_ = (c->*m)(); |
| 78 | } |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 79 | template <typename C, typename M, typename T1> |
| 80 | void Invoke(C* c, M m, T1 a1) { |
| 81 | r_ = (c->*m)(std::move(a1)); |
| 82 | } |
| 83 | template <typename C, typename M, typename T1, typename T2> |
| 84 | void Invoke(C* c, M m, T1 a1, T2 a2) { |
| 85 | r_ = (c->*m)(std::move(a1), std::move(a2)); |
| 86 | } |
| 87 | template <typename C, typename M, typename T1, typename T2, typename T3> |
| 88 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { |
| 89 | r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3)); |
| 90 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 91 | template <typename C, |
| 92 | typename M, |
| 93 | typename T1, |
| 94 | typename T2, |
| 95 | typename T3, |
| 96 | typename T4> |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 97 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4) { |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 98 | 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 | [diff] [blame] | 99 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 100 | template <typename C, |
| 101 | typename M, |
| 102 | typename T1, |
| 103 | typename T2, |
| 104 | typename T3, |
| 105 | typename T4, |
| 106 | typename T5> |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 107 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 108 | r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4), |
| 109 | std::move(a5)); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 110 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 111 | |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 112 | R moved_result() { return std::move(r_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | R r_; |
| 116 | }; |
| 117 | |
| 118 | template <> |
| 119 | class ReturnType<void> { |
| 120 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 121 | template <typename C, typename M> |
| 122 | void Invoke(C* c, M m) { |
| 123 | (c->*m)(); |
| 124 | } |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 125 | template <typename C, typename M, typename T1> |
| 126 | void Invoke(C* c, M m, T1 a1) { |
| 127 | (c->*m)(std::move(a1)); |
| 128 | } |
| 129 | template <typename C, typename M, typename T1, typename T2> |
| 130 | void Invoke(C* c, M m, T1 a1, T2 a2) { |
| 131 | (c->*m)(std::move(a1), std::move(a2)); |
| 132 | } |
| 133 | template <typename C, typename M, typename T1, typename T2, typename T3> |
| 134 | void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) { |
| 135 | (c->*m)(std::move(a1), std::move(a2), std::move(a3)); |
| 136 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 137 | |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 138 | void moved_result() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 139 | }; |
| 140 | |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 141 | namespace internal { |
| 142 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 143 | class SynchronousMethodCall : public rtc::MessageData, |
| 144 | public rtc::MessageHandler { |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 145 | public: |
Steve Anton | f2737d2 | 2017-10-31 23:27:34 | [diff] [blame] | 146 | explicit SynchronousMethodCall(rtc::MessageHandler* proxy); |
| 147 | ~SynchronousMethodCall() override; |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 148 | |
Steve Anton | f2737d2 | 2017-10-31 23:27:34 | [diff] [blame] | 149 | void Invoke(const rtc::Location& posted_from, rtc::Thread* t); |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 150 | |
| 151 | private: |
Steve Anton | f2737d2 | 2017-10-31 23:27:34 | [diff] [blame] | 152 | void OnMessage(rtc::Message*) override; |
| 153 | |
Niels Möller | 58376f3 | 2018-11-15 08:31:38 | [diff] [blame] | 154 | rtc::Event e_; |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 155 | rtc::MessageHandler* proxy_; |
| 156 | }; |
| 157 | |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 158 | } // namespace internal |
tommi@webrtc.org | 18de6f9 | 2014-11-04 12:08:48 | [diff] [blame] | 159 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 160 | template <typename C, typename R> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 161 | class MethodCall0 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 162 | public: |
| 163 | typedef R (C::*Method)(); |
| 164 | MethodCall0(C* c, Method m) : c_(c), m_(m) {} |
| 165 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 166 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 167 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 168 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 172 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 173 | |
| 174 | C* c_; |
| 175 | Method m_; |
| 176 | ReturnType<R> r_; |
| 177 | }; |
| 178 | |
| 179 | template <typename C, typename R> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 180 | class ConstMethodCall0 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 181 | public: |
| 182 | typedef R (C::*Method)() const; |
| 183 | ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {} |
| 184 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 185 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 186 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 187 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | private: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 191 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 192 | |
| 193 | C* c_; |
| 194 | Method m_; |
| 195 | ReturnType<R> r_; |
| 196 | }; |
| 197 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 198 | template <typename C, typename R, typename T1> |
| 199 | class MethodCall1 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 200 | public: |
| 201 | typedef R (C::*Method)(T1 a1); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 202 | 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 | [diff] [blame] | 203 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 204 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 205 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 206 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 210 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 211 | |
| 212 | C* c_; |
| 213 | Method m_; |
| 214 | ReturnType<R> r_; |
| 215 | T1 a1_; |
| 216 | }; |
| 217 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 218 | template <typename C, typename R, typename T1> |
| 219 | class ConstMethodCall1 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 220 | public: |
| 221 | typedef R (C::*Method)(T1 a1) const; |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 222 | 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 | [diff] [blame] | 223 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 224 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 225 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 226 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 230 | void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 231 | |
| 232 | C* c_; |
| 233 | Method m_; |
| 234 | ReturnType<R> r_; |
| 235 | T1 a1_; |
| 236 | }; |
| 237 | |
| 238 | template <typename C, typename R, typename T1, typename T2> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 239 | class MethodCall2 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 240 | public: |
| 241 | typedef R (C::*Method)(T1 a1, T2 a2); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 242 | MethodCall2(C* c, Method m, T1 a1, T2 a2) |
| 243 | : c_(c), m_(m), a1_(std::move(a1)), a2_(std::move(a2)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 244 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 245 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 246 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 247 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 251 | void OnMessage(rtc::Message*) { |
| 252 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_)); |
| 253 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 254 | |
| 255 | C* c_; |
| 256 | Method m_; |
| 257 | ReturnType<R> r_; |
| 258 | T1 a1_; |
| 259 | T2 a2_; |
| 260 | }; |
| 261 | |
| 262 | template <typename C, typename R, typename T1, typename T2, typename T3> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 263 | class MethodCall3 : public rtc::Message, public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 264 | public: |
| 265 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3); |
| 266 | MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3) |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 267 | : c_(c), |
| 268 | m_(m), |
| 269 | a1_(std::move(a1)), |
| 270 | a2_(std::move(a2)), |
| 271 | a3_(std::move(a3)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 272 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 273 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 274 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 275 | return r_.moved_result(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 279 | void OnMessage(rtc::Message*) { |
| 280 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_)); |
| 281 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 282 | |
| 283 | C* c_; |
| 284 | Method m_; |
| 285 | ReturnType<R> r_; |
| 286 | T1 a1_; |
| 287 | T2 a2_; |
| 288 | T3 a3_; |
| 289 | }; |
| 290 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 291 | template <typename C, |
| 292 | typename R, |
| 293 | typename T1, |
| 294 | typename T2, |
| 295 | typename T3, |
| 296 | typename T4> |
| 297 | class MethodCall4 : public rtc::Message, public rtc::MessageHandler { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 298 | public: |
| 299 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4); |
| 300 | MethodCall4(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4) |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 301 | : c_(c), |
| 302 | m_(m), |
| 303 | a1_(std::move(a1)), |
| 304 | a2_(std::move(a2)), |
| 305 | a3_(std::move(a3)), |
| 306 | a4_(std::move(a4)) {} |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 307 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 308 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 309 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 310 | return r_.moved_result(); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 314 | void OnMessage(rtc::Message*) { |
| 315 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), |
| 316 | std::move(a4_)); |
| 317 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 318 | |
| 319 | C* c_; |
| 320 | Method m_; |
| 321 | ReturnType<R> r_; |
| 322 | T1 a1_; |
| 323 | T2 a2_; |
| 324 | T3 a3_; |
| 325 | T4 a4_; |
| 326 | }; |
| 327 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 328 | template <typename C, |
| 329 | typename R, |
| 330 | typename T1, |
| 331 | typename T2, |
| 332 | typename T3, |
| 333 | typename T4, |
| 334 | typename T5> |
| 335 | class MethodCall5 : public rtc::Message, public rtc::MessageHandler { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 336 | public: |
| 337 | typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); |
| 338 | MethodCall5(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 339 | : c_(c), |
| 340 | m_(m), |
| 341 | a1_(std::move(a1)), |
| 342 | a2_(std::move(a2)), |
| 343 | a3_(std::move(a3)), |
| 344 | a4_(std::move(a4)), |
| 345 | a5_(std::move(a5)) {} |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 346 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 347 | R Marshal(const rtc::Location& posted_from, rtc::Thread* t) { |
| 348 | internal::SynchronousMethodCall(this).Invoke(posted_from, t); |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 349 | return r_.moved_result(); |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private: |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 353 | void OnMessage(rtc::Message*) { |
| 354 | r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_), |
| 355 | std::move(a4_), std::move(a5_)); |
| 356 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 357 | |
| 358 | C* c_; |
| 359 | Method m_; |
| 360 | ReturnType<R> r_; |
| 361 | T1 a1_; |
| 362 | T2 a2_; |
| 363 | T3 a3_; |
| 364 | T4 a4_; |
| 365 | T5 a5_; |
| 366 | }; |
| 367 | |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 368 | // Helper macros to reduce code duplication. |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 369 | #define PROXY_MAP_BOILERPLATE(c) \ |
| 370 | template <class INTERNAL_CLASS> \ |
| 371 | class c##ProxyWithInternal; \ |
| 372 | typedef c##ProxyWithInternal<c##Interface> c##Proxy; \ |
| 373 | template <class INTERNAL_CLASS> \ |
| 374 | class c##ProxyWithInternal : public c##Interface { \ |
| 375 | protected: \ |
| 376 | typedef c##Interface C; \ |
| 377 | \ |
| 378 | public: \ |
| 379 | const INTERNAL_CLASS* internal() const { return c_; } \ |
| 380 | INTERNAL_CLASS* internal() { return c_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 381 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 382 | // clang-format off |
| 383 | // clang-format would put the semicolon alone, |
| 384 | // leading to a presubmit error (cpplint.py) |
oprypin | 803dc29 | 2017-02-01 09:55:59 | [diff] [blame] | 385 | #define END_PROXY_MAP() \ |
| 386 | }; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 387 | // clang-format on |
oprypin | 803dc29 | 2017-02-01 09:55:59 | [diff] [blame] | 388 | |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 389 | #define SIGNALING_PROXY_MAP_BOILERPLATE(c) \ |
| 390 | protected: \ |
| 391 | c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \ |
| 392 | : signaling_thread_(signaling_thread), c_(c) {} \ |
| 393 | \ |
| 394 | private: \ |
| 395 | mutable rtc::Thread* signaling_thread_; |
| 396 | |
| 397 | #define WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 398 | protected: \ |
| 399 | c##ProxyWithInternal(rtc::Thread* signaling_thread, \ |
| 400 | rtc::Thread* worker_thread, INTERNAL_CLASS* c) \ |
| 401 | : signaling_thread_(signaling_thread), \ |
| 402 | worker_thread_(worker_thread), \ |
| 403 | c_(c) {} \ |
| 404 | \ |
| 405 | private: \ |
| 406 | mutable rtc::Thread* signaling_thread_; \ |
| 407 | mutable rtc::Thread* worker_thread_; |
| 408 | |
| 409 | // Note that the destructor is protected so that the proxy can only be |
| 410 | // destroyed via RefCountInterface. |
| 411 | #define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 412 | protected: \ |
| 413 | ~c##ProxyWithInternal() { \ |
| 414 | MethodCall0<c##ProxyWithInternal, void> call( \ |
| 415 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 416 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 417 | } \ |
| 418 | \ |
| 419 | private: \ |
| 420 | void DestroyInternal() { c_ = nullptr; } \ |
| 421 | rtc::scoped_refptr<INTERNAL_CLASS> c_; |
| 422 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 423 | // Note: This doesn't use a unique_ptr, because it intends to handle a corner |
| 424 | // case where an object's deletion triggers a callback that calls back into |
| 425 | // this proxy object. If relying on a unique_ptr to delete the object, its |
| 426 | // inner pointer would be set to null before this reentrant callback would have |
| 427 | // a chance to run, resulting in a segfault. |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 428 | #define OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 429 | public: \ |
| 430 | ~c##ProxyWithInternal() { \ |
| 431 | MethodCall0<c##ProxyWithInternal, void> call( \ |
| 432 | this, &c##ProxyWithInternal::DestroyInternal); \ |
| 433 | call.Marshal(RTC_FROM_HERE, destructor_thread()); \ |
| 434 | } \ |
| 435 | \ |
| 436 | private: \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 437 | void DestroyInternal() { delete c_; } \ |
| 438 | INTERNAL_CLASS* c_; |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 439 | |
| 440 | #define BEGIN_SIGNALING_PROXY_MAP(c) \ |
| 441 | PROXY_MAP_BOILERPLATE(c) \ |
| 442 | SIGNALING_PROXY_MAP_BOILERPLATE(c) \ |
| 443 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 444 | public: \ |
| 445 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 446 | rtc::Thread* signaling_thread, INTERNAL_CLASS* c) { \ |
| 447 | return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \ |
| 448 | c); \ |
| 449 | } |
| 450 | |
| 451 | #define BEGIN_PROXY_MAP(c) \ |
| 452 | PROXY_MAP_BOILERPLATE(c) \ |
| 453 | WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 454 | REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \ |
| 455 | public: \ |
| 456 | static rtc::scoped_refptr<c##ProxyWithInternal> Create( \ |
| 457 | rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \ |
| 458 | INTERNAL_CLASS* c) { \ |
| 459 | return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \ |
| 460 | worker_thread, c); \ |
| 461 | } |
| 462 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 463 | #define BEGIN_OWNED_PROXY_MAP(c) \ |
| 464 | PROXY_MAP_BOILERPLATE(c) \ |
| 465 | WORKER_PROXY_MAP_BOILERPLATE(c) \ |
| 466 | OWNED_PROXY_MAP_BOILERPLATE(c) \ |
| 467 | public: \ |
| 468 | static std::unique_ptr<c##Interface> Create( \ |
| 469 | rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \ |
| 470 | std::unique_ptr<INTERNAL_CLASS> c) { \ |
| 471 | return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \ |
| 472 | signaling_thread, worker_thread, c.release())); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | #define PROXY_SIGNALING_THREAD_DESTRUCTOR() \ |
| 476 | private: \ |
| 477 | rtc::Thread* destructor_thread() const { return signaling_thread_; } \ |
| 478 | \ |
oprypin | 803dc29 | 2017-02-01 09:55:59 | [diff] [blame] | 479 | public: // NOLINTNEXTLINE |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 480 | |
| 481 | #define PROXY_WORKER_THREAD_DESTRUCTOR() \ |
| 482 | private: \ |
| 483 | rtc::Thread* destructor_thread() const { return worker_thread_; } \ |
| 484 | \ |
oprypin | 803dc29 | 2017-02-01 09:55:59 | [diff] [blame] | 485 | public: // NOLINTNEXTLINE |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 486 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 487 | #define PROXY_METHOD0(r, method) \ |
| 488 | r method() override { \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 489 | MethodCall0<C, r> call(c_, &C::method); \ |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 490 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 491 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 492 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 493 | #define PROXY_CONSTMETHOD0(r, method) \ |
| 494 | r method() const override { \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 495 | ConstMethodCall0<C, r> call(c_, &C::method); \ |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 496 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 497 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 498 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 499 | #define PROXY_METHOD1(r, method, t1) \ |
| 500 | r method(t1 a1) override { \ |
| 501 | MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 502 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 503 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 504 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 505 | #define PROXY_CONSTMETHOD1(r, method, t1) \ |
| 506 | r method(t1 a1) const override { \ |
| 507 | ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 508 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 509 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 510 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 511 | #define PROXY_METHOD2(r, method, t1, t2) \ |
| 512 | r method(t1 a1, t2 a2) override { \ |
| 513 | MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 514 | std::move(a2)); \ |
| 515 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 516 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 517 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 518 | #define PROXY_METHOD3(r, method, t1, t2, t3) \ |
| 519 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 520 | MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 521 | std::move(a2), std::move(a3)); \ |
| 522 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | #define PROXY_METHOD4(r, method, t1, t2, t3, t4) \ |
| 526 | r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 527 | MethodCall4<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \ |
| 528 | std::move(a2), std::move(a3), \ |
| 529 | std::move(a4)); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 530 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
| 531 | } |
| 532 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 533 | #define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \ |
| 534 | r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \ |
| 535 | MethodCall5<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \ |
| 536 | std::move(a2), std::move(a3), \ |
| 537 | std::move(a4), std::move(a5)); \ |
| 538 | return call.Marshal(RTC_FROM_HERE, signaling_thread_); \ |
nisse | 5b68ab5 | 2016-04-07 14:45:54 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // Define methods which should be invoked on the worker thread. |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 542 | #define PROXY_WORKER_METHOD0(r, method) \ |
| 543 | r method() override { \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 544 | MethodCall0<C, r> call(c_, &C::method); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 545 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
nisse | 5b68ab5 | 2016-04-07 14:45:54 | [diff] [blame] | 546 | } |
| 547 | |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 548 | #define PROXY_WORKER_CONSTMETHOD0(r, method) \ |
| 549 | r method() const override { \ |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 550 | ConstMethodCall0<C, r> call(c_, &C::method); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 551 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 552 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 | [diff] [blame] | 553 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 554 | #define PROXY_WORKER_METHOD1(r, method, t1) \ |
| 555 | r method(t1 a1) override { \ |
| 556 | MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 557 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 558 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 559 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 560 | #define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \ |
| 561 | r method(t1 a1) const override { \ |
| 562 | ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \ |
| 563 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 564 | } |
| 565 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 566 | #define PROXY_WORKER_METHOD2(r, method, t1, t2) \ |
| 567 | r method(t1 a1, t2 a2) override { \ |
| 568 | MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 569 | std::move(a2)); \ |
| 570 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 571 | } |
| 572 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 573 | #define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \ |
| 574 | r method(t1 a1, t2 a2) const override { \ |
| 575 | ConstMethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \ |
| 576 | std::move(a2)); \ |
| 577 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 578 | } |
| 579 | |
| 580 | #define PROXY_WORKER_METHOD3(r, method, t1, t2, t3) \ |
| 581 | r method(t1 a1, t2 a2, t3 a3) override { \ |
| 582 | MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 583 | std::move(a2), std::move(a3)); \ |
| 584 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
| 585 | } |
| 586 | |
| 587 | #define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2) \ |
| 588 | r method(t1 a1, t2 a2, t3 a3) const override { \ |
| 589 | ConstMethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \ |
| 590 | std::move(a2), std::move(a3)); \ |
| 591 | return call.Marshal(RTC_FROM_HERE, worker_thread_); \ |
deadbeef | d99a200 | 2017-01-18 16:55:23 | [diff] [blame] | 592 | } |
| 593 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 594 | } // namespace webrtc |
| 595 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 596 | #endif // API_PROXY_H_ |