blob: 6627aac7d471e1734d2d5b5d310ff2202bd9378e [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 Bonadei92ea95e2017-09-15 06:47:31 +020064#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
Yves Gerey3e707812018-11-28 16:47:49 +010066namespace rtc {
67class Location;
68}
69
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070namespace webrtc {
71
72template <typename R>
73class ReturnType {
74 public:
Yves Gerey665174f2018-06-19 15:03:05 +020075 template <typename C, typename M>
76 void Invoke(C* c, M m) {
77 r_ = (c->*m)();
78 }
deadbeefd99a2002017-01-18 08:55:23 -080079 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 Gerey665174f2018-06-19 15:03:05 +020091 template <typename C,
92 typename M,
93 typename T1,
94 typename T2,
95 typename T3,
96 typename T4>
perkj@webrtc.org81134d02015-01-12 08:30:16 +000097 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4) {
deadbeefd99a2002017-01-18 08:55:23 -080098 r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4));
perkj@webrtc.org81134d02015-01-12 08:30:16 +000099 }
Yves Gerey665174f2018-06-19 15:03:05 +0200100 template <typename C,
101 typename M,
102 typename T1,
103 typename T2,
104 typename T3,
105 typename T4,
106 typename T5>
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000107 void Invoke(C* c, M m, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
deadbeefd99a2002017-01-18 08:55:23 -0800108 r_ = (c->*m)(std::move(a1), std::move(a2), std::move(a3), std::move(a4),
109 std::move(a5));
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
deadbeefd99a2002017-01-18 08:55:23 -0800112 R moved_result() { return std::move(r_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 private:
115 R r_;
116};
117
118template <>
119class ReturnType<void> {
120 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200121 template <typename C, typename M>
122 void Invoke(C* c, M m) {
123 (c->*m)();
124 }
deadbeefd99a2002017-01-18 08:55:23 -0800125 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.org28e20752013-07-10 00:45:36 +0000137
deadbeefd99a2002017-01-18 08:55:23 -0800138 void moved_result() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139};
140
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000141namespace internal {
142
Yves Gerey665174f2018-06-19 15:03:05 +0200143class SynchronousMethodCall : public rtc::MessageData,
144 public rtc::MessageHandler {
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000145 public:
Steve Antonf2737d22017-10-31 16:27:34 -0700146 explicit SynchronousMethodCall(rtc::MessageHandler* proxy);
147 ~SynchronousMethodCall() override;
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000148
Steve Antonf2737d22017-10-31 16:27:34 -0700149 void Invoke(const rtc::Location& posted_from, rtc::Thread* t);
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000150
151 private:
Steve Antonf2737d22017-10-31 16:27:34 -0700152 void OnMessage(rtc::Message*) override;
153
Niels Möller58376f32018-11-15 09:31:38 +0100154 rtc::Event e_;
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000155 rtc::MessageHandler* proxy_;
156};
157
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000158} // namespace internal
tommi@webrtc.org18de6f92014-11-04 12:08:48 +0000159
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160template <typename C, typename R>
Yves Gerey665174f2018-06-19 15:03:05 +0200161class MethodCall0 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 public:
163 typedef R (C::*Method)();
164 MethodCall0(C* c, Method m) : c_(c), m_(m) {}
165
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700166 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
167 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800168 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 }
170
171 private:
Yves Gerey665174f2018-06-19 15:03:05 +0200172 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
174 C* c_;
175 Method m_;
176 ReturnType<R> r_;
177};
178
179template <typename C, typename R>
Yves Gerey665174f2018-06-19 15:03:05 +0200180class ConstMethodCall0 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 public:
182 typedef R (C::*Method)() const;
183 ConstMethodCall0(C* c, Method m) : c_(c), m_(m) {}
184
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700185 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
186 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800187 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 }
189
190 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000191 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
193 C* c_;
194 Method m_;
195 ReturnType<R> r_;
196};
197
Yves Gerey665174f2018-06-19 15:03:05 +0200198template <typename C, typename R, typename T1>
199class MethodCall1 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 public:
201 typedef R (C::*Method)(T1 a1);
deadbeefd99a2002017-01-18 08:55:23 -0800202 MethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700204 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
205 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800206 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 }
208
209 private:
deadbeefd99a2002017-01-18 08:55:23 -0800210 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
212 C* c_;
213 Method m_;
214 ReturnType<R> r_;
215 T1 a1_;
216};
217
Yves Gerey665174f2018-06-19 15:03:05 +0200218template <typename C, typename R, typename T1>
219class ConstMethodCall1 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 public:
221 typedef R (C::*Method)(T1 a1) const;
deadbeefd99a2002017-01-18 08:55:23 -0800222 ConstMethodCall1(C* c, Method m, T1 a1) : c_(c), m_(m), a1_(std::move(a1)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700224 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
225 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800226 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 }
228
229 private:
deadbeefd99a2002017-01-18 08:55:23 -0800230 void OnMessage(rtc::Message*) { r_.Invoke(c_, m_, std::move(a1_)); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231
232 C* c_;
233 Method m_;
234 ReturnType<R> r_;
235 T1 a1_;
236};
237
238template <typename C, typename R, typename T1, typename T2>
Yves Gerey665174f2018-06-19 15:03:05 +0200239class MethodCall2 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 public:
241 typedef R (C::*Method)(T1 a1, T2 a2);
deadbeefd99a2002017-01-18 08:55:23 -0800242 MethodCall2(C* c, Method m, T1 a1, T2 a2)
243 : c_(c), m_(m), a1_(std::move(a1)), a2_(std::move(a2)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700245 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
246 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800247 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 }
249
250 private:
deadbeefd99a2002017-01-18 08:55:23 -0800251 void OnMessage(rtc::Message*) {
252 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_));
253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254
255 C* c_;
256 Method m_;
257 ReturnType<R> r_;
258 T1 a1_;
259 T2 a2_;
260};
261
262template <typename C, typename R, typename T1, typename T2, typename T3>
Yves Gerey665174f2018-06-19 15:03:05 +0200263class MethodCall3 : public rtc::Message, public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 public:
265 typedef R (C::*Method)(T1 a1, T2 a2, T3 a3);
266 MethodCall3(C* c, Method m, T1 a1, T2 a2, T3 a3)
deadbeefd99a2002017-01-18 08:55:23 -0800267 : c_(c),
268 m_(m),
269 a1_(std::move(a1)),
270 a2_(std::move(a2)),
271 a3_(std::move(a3)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700273 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
274 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800275 return r_.moved_result();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 private:
deadbeefd99a2002017-01-18 08:55:23 -0800279 void OnMessage(rtc::Message*) {
280 r_.Invoke(c_, m_, std::move(a1_), std::move(a2_), std::move(a3_));
281 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
283 C* c_;
284 Method m_;
285 ReturnType<R> r_;
286 T1 a1_;
287 T2 a2_;
288 T3 a3_;
289};
290
Yves Gerey665174f2018-06-19 15:03:05 +0200291template <typename C,
292 typename R,
293 typename T1,
294 typename T2,
295 typename T3,
296 typename T4>
297class MethodCall4 : public rtc::Message, public rtc::MessageHandler {
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000298 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)
deadbeefd99a2002017-01-18 08:55:23 -0800301 : 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.org81134d02015-01-12 08:30:16 +0000307
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700308 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
309 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800310 return r_.moved_result();
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000311 }
312
313 private:
deadbeefd99a2002017-01-18 08:55:23 -0800314 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.org81134d02015-01-12 08:30:16 +0000318
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 Gerey665174f2018-06-19 15:03:05 +0200328template <typename C,
329 typename R,
330 typename T1,
331 typename T2,
332 typename T3,
333 typename T4,
334 typename T5>
335class MethodCall5 : public rtc::Message, public rtc::MessageHandler {
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000336 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)
deadbeefd99a2002017-01-18 08:55:23 -0800339 : 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.org81134d02015-01-12 08:30:16 +0000346
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700347 R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
348 internal::SynchronousMethodCall(this).Invoke(posted_from, t);
deadbeefd99a2002017-01-18 08:55:23 -0800349 return r_.moved_result();
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000350 }
351
352 private:
deadbeefd99a2002017-01-18 08:55:23 -0800353 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.org81134d02015-01-12 08:30:16 +0000357
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
deadbeefd99a2002017-01-18 08:55:23 -0800368// Helper macros to reduce code duplication.
deadbeefe814a0d2017-02-25 18:15:09 -0800369#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.org28e20752013-07-10 00:45:36 +0000381
Yves Gerey665174f2018-06-19 15:03:05 +0200382// clang-format off
383// clang-format would put the semicolon alone,
384// leading to a presubmit error (cpplint.py)
oprypin803dc292017-02-01 01:55:59 -0800385#define END_PROXY_MAP() \
386 };
Yves Gerey665174f2018-06-19 15:03:05 +0200387// clang-format on
oprypin803dc292017-02-01 01:55:59 -0800388
deadbeefd99a2002017-01-18 08:55:23 -0800389#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
deadbeefe814a0d2017-02-25 18:15:09 -0800423// 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.
deadbeefd99a2002017-01-18 08:55:23 -0800428#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: \
deadbeefe814a0d2017-02-25 18:15:09 -0800437 void DestroyInternal() { delete c_; } \
438 INTERNAL_CLASS* c_;
deadbeefd99a2002017-01-18 08:55:23 -0800439
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
deadbeefe814a0d2017-02-25 18:15:09 -0800463#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())); \
deadbeefd99a2002017-01-18 08:55:23 -0800473 }
474
475#define PROXY_SIGNALING_THREAD_DESTRUCTOR() \
476 private: \
477 rtc::Thread* destructor_thread() const { return signaling_thread_; } \
478 \
oprypin803dc292017-02-01 01:55:59 -0800479 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800480
481#define PROXY_WORKER_THREAD_DESTRUCTOR() \
482 private: \
483 rtc::Thread* destructor_thread() const { return worker_thread_; } \
484 \
oprypin803dc292017-02-01 01:55:59 -0800485 public: // NOLINTNEXTLINE
deadbeefd99a2002017-01-18 08:55:23 -0800486
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700487#define PROXY_METHOD0(r, method) \
488 r method() override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800489 MethodCall0<C, r> call(c_, &C::method); \
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700490 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000491 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700493#define PROXY_CONSTMETHOD0(r, method) \
494 r method() const override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800495 ConstMethodCall0<C, r> call(c_, &C::method); \
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700496 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000497 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498
deadbeefe814a0d2017-02-25 18:15:09 -0800499#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.org14665ff2015-03-04 12:58:35 +0000503 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
deadbeefe814a0d2017-02-25 18:15:09 -0800505#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.org14665ff2015-03-04 12:58:35 +0000509 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510
deadbeefe814a0d2017-02-25 18:15:09 -0800511#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.org14665ff2015-03-04 12:58:35 +0000516 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000517
deadbeefe814a0d2017-02-25 18:15:09 -0800518#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_); \
deadbeefd99a2002017-01-18 08:55:23 -0800523 }
524
525#define PROXY_METHOD4(r, method, t1, t2, t3, t4) \
526 r method(t1 a1, t2 a2, t3 a3, t4 a4) override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800527 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)); \
deadbeefd99a2002017-01-18 08:55:23 -0800530 return call.Marshal(RTC_FROM_HERE, signaling_thread_); \
531 }
532
deadbeefe814a0d2017-02-25 18:15:09 -0800533#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_); \
nisse5b68ab52016-04-07 07:45:54 -0700539 }
540
541// Define methods which should be invoked on the worker thread.
deadbeefd99a2002017-01-18 08:55:23 -0800542#define PROXY_WORKER_METHOD0(r, method) \
543 r method() override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800544 MethodCall0<C, r> call(c_, &C::method); \
deadbeefd99a2002017-01-18 08:55:23 -0800545 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
nisse5b68ab52016-04-07 07:45:54 -0700546 }
547
deadbeefd99a2002017-01-18 08:55:23 -0800548#define PROXY_WORKER_CONSTMETHOD0(r, method) \
549 r method() const override { \
deadbeefe814a0d2017-02-25 18:15:09 -0800550 ConstMethodCall0<C, r> call(c_, &C::method); \
deadbeefd99a2002017-01-18 08:55:23 -0800551 return call.Marshal(RTC_FROM_HERE, worker_thread_); \
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000552 }
perkj@webrtc.org81134d02015-01-12 08:30:16 +0000553
deadbeefe814a0d2017-02-25 18:15:09 -0800554#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_); \
deadbeefd99a2002017-01-18 08:55:23 -0800558 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
deadbeefe814a0d2017-02-25 18:15:09 -0800560#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_); \
deadbeefd99a2002017-01-18 08:55:23 -0800564 }
565
deadbeefe814a0d2017-02-25 18:15:09 -0800566#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_); \
deadbeefd99a2002017-01-18 08:55:23 -0800571 }
572
deadbeefe814a0d2017-02-25 18:15:09 -0800573#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_); \
deadbeefd99a2002017-01-18 08:55:23 -0800592 }
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594} // namespace webrtc
595
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200596#endif // API_PROXY_H_