blob: 3e76ee7c5ee97426c4deffa0ef2c8a89310d858e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
11// This file contains Macros for creating proxies for webrtc MediaStream and
12// PeerConnection classes.
deadbeefb10f32f2017-02-08 01:38:21 -080013// TODO(deadbeef): Move this to pc/; this is part of the implementation.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15//
16// Example usage:
17//
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018// class TestInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019// public:
20// std::string FooA() = 0;
21// std::string FooB(bool arg1) const = 0;
nisse72c8d2b2016-04-15 03:49:07 -070022// std::string FooC(bool arg1) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023// };
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)
deadbeefd99a2002017-01-18 08:55:23 -080032// PROXY_SIGNALING_THREAD_DESTRUCTOR()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033// PROXY_METHOD0(std::string, FooA)
34// PROXY_CONSTMETHOD1(std::string, FooB, arg1)
nisse72c8d2b2016-04-15 03:49:07 -070035// PROXY_WORKER_METHOD1(std::string, FooC, arg1)
deadbeefd99a2002017-01-18 08:55:23 -080036// END_PROXY_MAP()
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037//
deadbeefd99a2002017-01-18 08:55:23 -080038// Where the destructor and first two methods are invoked on the signaling
39// thread, and the third is invoked on the worker thread.
nisse72c8d2b2016-04-15 03:49:07 -070040//
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.
deadbeefd99a2002017-01-18 08:55:23 -080048//
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.org28e20752013-07-10 00:45:36 +000051
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#ifndef API_PROXY_H_
53#define API_PROXY_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
kwibergd1fe2812016-04-27 06:47:29 -070055#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010056#include <string>
oprypin803dc292017-02-01 01:55:59 -080057#include <utility>
kwibergd1fe2812016-04-27 06:47:29 -070058
Mirko Bonadeid9708072019-01-25 20:26:48 +010059#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080061#include "rtc_base/message_handler.h"
62#include "rtc_base/message_queue.h"
63#include "rtc_base/ref_counted_object.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020064#include "rtc_base/system/rtc_export.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
Yves Gerey3e707812018-11-28 16:47:49 +010067namespace rtc {
68class Location;
69}
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071namespace webrtc {
72
73template <typename R>
74class ReturnType {
75 public:
Yves Gerey665174f2018-06-19 15:03:05 +020076 template <typename C, typename M>
77 void Invoke(C* c, M m) {
78 r_ = (c->*m)();
79 }
deadbeefd99a2002017-01-18 08:55:23 -080080 template <typename C, typename M, typename T1>
81 void Invoke(C* c, M m, T1 a1) {
82 r_ = (c->*m)(std::move(a1));
83 }
84 template <typename C, typename M, typename T1, typename T2>
85 void Invoke(C* c, M m, T1 a1, T2 a2) {
86 r_ = (c->*m)(std::move(a1), std::move(a2));
87 }
88 template <typename C, typename M, typename T1, typename T2, typename T3>
89 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) {
90 r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3));
91 }
Yves Gerey665174f2018-06-19 15:03:05 +020092 template <typename C,
93 typename M,
94 typename T1,
95 typename T2,
96 typename T3,
97 typename T4>
perkj@webrtc.org81134d02015-01-12 08:30:16 +000098 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4) {
deadbeefd99a2002017-01-18 08:55:23 -080099 r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4));
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000100 }
Yves Gerey665174f2018-06-19 15:03:05 +0200101 template <typename C,
102 typename M,
103 typename T1,
104 typename T2,
105 typename T3,
106 typename T4,
107 typename T5>
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000108 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
deadbeefd99a2002017-01-18 08:55:23 -0800109 r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4),
110 std::move(a5));
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000111 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
deadbeefd99a2002017-01-18 08:55:23 -0800113 R moved_result() { return std::move(r_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
115 private:
116 R r_;
117};
118
119template <>
120class ReturnType<void> {
121 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200122 template <typename C, typename M>
123 void Invoke(C* c, M m) {
124 (c->*m)();
125 }
deadbeefd99a2002017-01-18 08:55:23 -0800126 template <typename C, typename M, typename T1>
127 void Invoke(C* c, M m, T1 a1) {
128 (c->*m)(std::move(a1));
129 }
130 template <typename C, typename M, typename T1, typename T2>
131 void Invoke(C* c, M m, T1 a1, T2 a2) {
132 (c->*m)(std::move(a1), std::move(a2));
133 }
134 template <typename C, typename M, typename T1, typename T2, typename T3>
135 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3) {
136 (c->*m)(std::move(a1), std::move(a2), std::move(a3));
137 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
deadbeefd99a2002017-01-18 08:55:23 -0800139 void moved_result() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000142namespace internal {
143
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200144class RTC_EXPORT SynchronousMethodCall : public rtc::MessageData,
145 public rtc::MessageHandler {
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000146 public:
Steve Antonf2737d22017-10-31 16:27:34 -0700147 explicit SynchronousMethodCall(rtc::MessageHandler* proxy);
148 ~SynchronousMethodCall() override;
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000149
Steve Antonf2737d22017-10-31 16:27:34 -0700150 void Invoke(const rtc::Location& posted_from, rtc::Thread* t);
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000151
152 private:
Steve Antonf2737d22017-10-31 16:27:34 -0700153 void OnMessage(rtc::Message*) override;
154
Niels Möller58376f32018-11-15 09:31:38 +0100155 rtc::Event e_;
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000156 rtc::MessageHandler* proxy_;
157};
158
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000159} // namespace internal
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161template <typename C, typename R>
Yves Gerey665174f2018-06-19 15:03:05 +0200162class MethodCall0 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 public:
164 typedef R (C::*Method)();
165 MethodCall0(C* c, Method m) : c_(c), m_(m) {}
166
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700167 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
168 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800169 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 }
171
172 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200173 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
175 C* c_;
176 Method m_;
177 ReturnType<R> r_;
178};
179
180template <typename C, typename R>
Yves Gerey665174f2018-06-19 15:03:05 +0200181class ConstMethodCall0 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 public:
183 typedef R (C::*Method)() const;
184 ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {}
185
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700186 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
187 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800188 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 }
190
191 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
194 C* c_;
195 Method m_;
196 ReturnType<R> r_;
197};
198
Yves Gerey665174f2018-06-19 15:03:05 +0200199template <typename C, typename R, typename T1>
200class MethodCall1 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 public:
202 typedef R (C::*Method)(T1 a1);
deadbeefd99a2002017-01-18 08:55:23 -0800203 MethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700205 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
206 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800207 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 }
209
210 private:
deadbeefd99a2002017-01-18 08:55:23 -0800211 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
213 C* c_;
214 Method m_;
215 ReturnType<R> r_;
216 T1 a1_;
217};
218
Yves Gerey665174f2018-06-19 15:03:05 +0200219template <typename C, typename R, typename T1>
220class ConstMethodCall1 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 public:
222 typedef R (C::*Method)(T1 a1) const;
deadbeefd99a2002017-01-18 08:55:23 -0800223 ConstMethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700225 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
226 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800227 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 }
229
230 private:
deadbeefd99a2002017-01-18 08:55:23 -0800231 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
233 C* c_;
234 Method m_;
235 ReturnType<R> r_;
236 T1 a1_;
237};
238
239template <typename C, typename R, typename T1, typename T2>
Yves Gerey665174f2018-06-19 15:03:05 +0200240class MethodCall2 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 public:
242 typedef R (C::*Method)(T1 a1, T2 a2);
deadbeefd99a2002017-01-18 08:55:23 -0800243 MethodCall2(C* c, Method m, T1 a1, T2 a2)
244 : c_(c), m_(m), a1_(std::move(a1)), a2_(std::move(a2)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700246 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
247 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800248 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 }
250
251 private:
deadbeefd99a2002017-01-18 08:55:23 -0800252 void OnMessage(rtc::Message*) {
253 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_));
254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 C* c_;
257 Method m_;
258 ReturnType<R> r_;
259 T1 a1_;
260 T2 a2_;
261};
262
263template <typename C, typename R, typename T1, typename T2, typename T3>
Yves Gerey665174f2018-06-19 15:03:05 +0200264class MethodCall3 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 public:
266 typedef R (C::*Method)(T1 a1, T2 a2, T3 a3);
267 MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3)
deadbeefd99a2002017-01-18 08:55:23 -0800268 : c_(c),
269 m_(m),
270 a1_(std::move(a1)),
271 a2_(std::move(a2)),
272 a3_(std::move(a3)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700274 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
275 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800276 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 }
278
279 private:
deadbeefd99a2002017-01-18 08:55:23 -0800280 void OnMessage(rtc::Message*) {
281 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_));
282 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
284 C* c_;
285 Method m_;
286 ReturnType<R> r_;
287 T1 a1_;
288 T2 a2_;
289 T3 a3_;
290};
291
Yves Gerey665174f2018-06-19 15:03:05 +0200292template <typename C,
293 typename R,
294 typename T1,
295 typename T2,
296 typename T3,
297 typename T4>
298class MethodCall4 : public rtc::Message, public rtc::MessageHandler {
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000299 public:
300 typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4);
301 MethodCall4(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4)
deadbeefd99a2002017-01-18 08:55:23 -0800302 : c_(c),
303 m_(m),
304 a1_(std::move(a1)),
305 a2_(std::move(a2)),
306 a3_(std::move(a3)),
307 a4_(std::move(a4)) {}
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000308
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700309 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
310 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800311 return r_.moved_result();
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000312 }
313
314 private:
deadbeefd99a2002017-01-18 08:55:23 -0800315 void OnMessage(rtc::Message*) {
316 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_),
317 std::move(a4_));
318 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000319
320 C* c_;
321 Method m_;
322 ReturnType<R> r_;
323 T1 a1_;
324 T2 a2_;
325 T3 a3_;
326 T4 a4_;
327};
328
Yves Gerey665174f2018-06-19 15:03:05 +0200329template <typename C,
330 typename R,
331 typename T1,
332 typename T2,
333 typename T3,
334 typename T4,
335 typename T5>
336class MethodCall5 : public rtc::Message, public rtc::MessageHandler {
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000337 public:
338 typedef R (C::*Method)(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
339 MethodCall5(C* c, Method m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
deadbeefd99a2002017-01-18 08:55:23 -0800340 : c_(c),
341 m_(m),
342 a1_(std::move(a1)),
343 a2_(std::move(a2)),
344 a3_(std::move(a3)),
345 a4_(std::move(a4)),
346 a5_(std::move(a5)) {}
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000347
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700348 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
349 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800350 return r_.moved_result();
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000351 }
352
353 private:
deadbeefd99a2002017-01-18 08:55:23 -0800354 void OnMessage(rtc::Message*) {
355 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_),
356 std::move(a4_), std::move(a5_));
357 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000358
359 C* c_;
360 Method m_;
361 ReturnType<R> r_;
362 T1 a1_;
363 T2 a2_;
364 T3 a3_;
365 T4 a4_;
366 T5 a5_;
367};
368
deadbeefd99a2002017-01-18 08:55:23 -0800369// Helper macros to reduce code duplication.
deadbeefe814a0d2017-02-25 18:15:09 -0800370#define PROXY_MAP_BOILERPLATE(c) \
371 template <class INTERNAL_CLASS> \
372 class c##ProxyWithInternal; \
373 typedef c##ProxyWithInternal<c##Interface> c##Proxy; \
374 template <class INTERNAL_CLASS> \
375 class c##ProxyWithInternal : public c##Interface { \
376 protected: \
377 typedef c##Interface C; \
378 \
379 public: \
380 const INTERNAL_CLASS* internal() const { return c_; } \
381 INTERNAL_CLASS* internal() { return c_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382
Yves Gerey665174f2018-06-19 15:03:05 +0200383// clang-format off
384// clang-format would put the semicolon alone,
385// leading to a presubmit error (cpplint.py)
oprypin803dc292017-02-01 01:55:59 -0800386#define END_PROXY_MAP() \
387 };
Yves Gerey665174f2018-06-19 15:03:05 +0200388// clang-format on
oprypin803dc292017-02-01 01:55:59 -0800389
deadbeefd99a2002017-01-18 08:55:23 -0800390#define SIGNALING_PROXY_MAP_BOILERPLATE(c) \
391 protected: \
392 c##ProxyWithInternal(rtc::Thread* signaling_thread, INTERNAL_CLASS* c) \
393 : signaling_thread_(signaling_thread), c_(c) {} \
394 \
395 private: \
396 mutable rtc::Thread* signaling_thread_;
397
398#define WORKER_PROXY_MAP_BOILERPLATE(c) \
399 protected: \
400 c##ProxyWithInternal(rtc::Thread* signaling_thread, \
401 rtc::Thread* worker_thread, INTERNAL_CLASS* c) \
402 : signaling_thread_(signaling_thread), \
403 worker_thread_(worker_thread), \
404 c_(c) {} \
405 \
406 private: \
407 mutable rtc::Thread* signaling_thread_; \
408 mutable rtc::Thread* worker_thread_;
409
410// Note that the destructor is protected so that the proxy can only be
411// destroyed via RefCountInterface.
412#define REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
413 protected: \
414 ~c##ProxyWithInternal() { \
415 MethodCall0<c##ProxyWithInternal, void> call( \
416 this, &c##ProxyWithInternal::DestroyInternal); \
417 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
418 } \
419 \
420 private: \
421 void DestroyInternal() { c_ = nullptr; } \
422 rtc::scoped_refptr<INTERNAL_CLASS> c_;
423
deadbeefe814a0d2017-02-25 18:15:09 -0800424// Note: This doesn't use a unique_ptr, because it intends to handle a corner
425// case where an object's deletion triggers a callback that calls back into
426// this proxy object. If relying on a unique_ptr to delete the object, its
427// inner pointer would be set to null before this reentrant callback would have
428// a chance to run, resulting in a segfault.
deadbeefd99a2002017-01-18 08:55:23 -0800429#define OWNED_PROXY_MAP_BOILERPLATE(c) \
430 public: \
431 ~c##ProxyWithInternal() { \
432 MethodCall0<c##ProxyWithInternal, void> call( \
433 this, &c##ProxyWithInternal::DestroyInternal); \
434 call.Marshal(RTC_FROM_HERE, destructor_thread()); \
435 } \
436 \
437 private: \
deadbeefe814a0d2017-02-25 18:15:09 -0800438 void DestroyInternal() { delete c_; } \
439 INTERNAL_CLASS* c_;
deadbeefd99a2002017-01-18 08:55:23 -0800440
441#define BEGIN_SIGNALING_PROXY_MAP(c) \
442 PROXY_MAP_BOILERPLATE(c) \
443 SIGNALING_PROXY_MAP_BOILERPLATE(c) \
444 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
445 public: \
446 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
447 rtc::Thread* signaling_thread, INTERNAL_CLASS* c) { \
448 return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
449 c); \
450 }
451
452#define BEGIN_PROXY_MAP(c) \
453 PROXY_MAP_BOILERPLATE(c) \
454 WORKER_PROXY_MAP_BOILERPLATE(c) \
455 REFCOUNTED_PROXY_MAP_BOILERPLATE(c) \
456 public: \
457 static rtc::scoped_refptr<c##ProxyWithInternal> Create( \
458 rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
459 INTERNAL_CLASS* c) { \
460 return new rtc::RefCountedObject<c##ProxyWithInternal>(signaling_thread, \
461 worker_thread, c); \
462 }
463
deadbeefe814a0d2017-02-25 18:15:09 -0800464#define BEGIN_OWNED_PROXY_MAP(c) \
465 PROXY_MAP_BOILERPLATE(c) \
466 WORKER_PROXY_MAP_BOILERPLATE(c) \
467 OWNED_PROXY_MAP_BOILERPLATE(c) \
468 public: \
469 static std::unique_ptr<c##Interface> Create( \
470 rtc::Thread* signaling_thread, rtc::Thread* worker_thread, \
471 std::unique_ptr<INTERNAL_CLASS> c) { \
472 return std::unique_ptr<c##Interface>(new c##ProxyWithInternal( \
473 signaling_thread, worker_thread, c.release())); \
deadbeefd99a2002017-01-18 08:55:23 -0800474 }
475
476#define PROXY_SIGNALING_THREAD_DESTRUCTOR() \
477 private: \
478 rtc::Thread* destructor_thread() const { return signaling_thread_; } \
479 \
oprypin803dc292017-02-01 01:55:59 -0800480 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800481
482#define PROXY_WORKER_THREAD_DESTRUCTOR() \
483 private: \
484 rtc::Thread* destructor_thread() const { return worker_thread_; } \
485 \
oprypin803dc292017-02-01 01:55:59 -0800486 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800487
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700488#define PROXY_METHOD0(r, method) \
489 r method() override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800490 MethodCall0<C, r> call(c_, &C::method); \
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700491 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700494#define PROXY_CONSTMETHOD0(r, method) \
495 r method() const override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800496 ConstMethodCall0<C, r> call(c_, &C::method); \
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700497 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499
deadbeefe814a0d2017-02-25 18:15:09 -0800500#define PROXY_METHOD1(r, method, t1) \
501 r method(t1 a1) override { \
502 MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
503 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505
deadbeefe814a0d2017-02-25 18:15:09 -0800506#define PROXY_CONSTMETHOD1(r, method, t1) \
507 r method(t1 a1) const override { \
508 ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
509 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
deadbeefe814a0d2017-02-25 18:15:09 -0800512#define PROXY_METHOD2(r, method, t1, t2) \
513 r method(t1 a1, t2 a2) override { \
514 MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
515 std::move(a2)); \
516 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000517 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000518
deadbeefe814a0d2017-02-25 18:15:09 -0800519#define PROXY_METHOD3(r, method, t1, t2, t3) \
520 r method(t1 a1, t2 a2, t3 a3) override { \
521 MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
522 std::move(a2), std::move(a3)); \
523 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800524 }
525
526#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \
527 r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800528 MethodCall4<C, r, t1, t2, t3, t4> call(c_, &C::method, std::move(a1), \
529 std::move(a2), std::move(a3), \
530 std::move(a4)); \
deadbeefd99a2002017-01-18 08:55:23 -0800531 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
532 }
533
deadbeefe814a0d2017-02-25 18:15:09 -0800534#define PROXY_METHOD5(r, method, t1, t2, t3, t4, t5) \
535 r method(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) override { \
536 MethodCall5<C, r, t1, t2, t3, t4, t5> call(c_, &C::method, std::move(a1), \
537 std::move(a2), std::move(a3), \
538 std::move(a4), std::move(a5)); \
539 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
nisse5b68ab52016-04-07 07:45:54 -0700540 }
541
542// Define methods which should be invoked on the worker thread.
deadbeefd99a2002017-01-18 08:55:23 -0800543#define PROXY_WORKER_METHOD0(r, method) \
544 r method() override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800545 MethodCall0<C, r> call(c_, &C::method); \
deadbeefd99a2002017-01-18 08:55:23 -0800546 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
nisse5b68ab52016-04-07 07:45:54 -0700547 }
548
deadbeefd99a2002017-01-18 08:55:23 -0800549#define PROXY_WORKER_CONSTMETHOD0(r, method) \
550 r method() const override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800551 ConstMethodCall0<C, r> call(c_, &C::method); \
deadbeefd99a2002017-01-18 08:55:23 -0800552 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000553 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000554
deadbeefe814a0d2017-02-25 18:15:09 -0800555#define PROXY_WORKER_METHOD1(r, method, t1) \
556 r method(t1 a1) override { \
557 MethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
558 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800559 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560
deadbeefe814a0d2017-02-25 18:15:09 -0800561#define PROXY_WORKER_CONSTMETHOD1(r, method, t1) \
562 r method(t1 a1) const override { \
563 ConstMethodCall1<C, r, t1> call(c_, &C::method, std::move(a1)); \
564 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800565 }
566
deadbeefe814a0d2017-02-25 18:15:09 -0800567#define PROXY_WORKER_METHOD2(r, method, t1, t2) \
568 r method(t1 a1, t2 a2) override { \
569 MethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
570 std::move(a2)); \
571 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800572 }
573
deadbeefe814a0d2017-02-25 18:15:09 -0800574#define PROXY_WORKER_CONSTMETHOD2(r, method, t1, t2) \
575 r method(t1 a1, t2 a2) const override { \
576 ConstMethodCall2<C, r, t1, t2> call(c_, &C::method, std::move(a1), \
577 std::move(a2)); \
578 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
579 }
580
581#define PROXY_WORKER_METHOD3(r, method, t1, t2, t3) \
582 r method(t1 a1, t2 a2, t3 a3) override { \
583 MethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
584 std::move(a2), std::move(a3)); \
585 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
586 }
587
588#define PROXY_WORKER_CONSTMETHOD3(r, method, t1, t2) \
589 r method(t1 a1, t2 a2, t3 a3) const override { \
590 ConstMethodCall3<C, r, t1, t2, t3> call(c_, &C::method, std::move(a1), \
591 std::move(a2), std::move(a3)); \
592 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
deadbeefd99a2002017-01-18 08:55:23 -0800593 }
594
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595} // namespace webrtc
596
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200597#endif // API_PROXY_H_