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