xref: /aosp_15_r20/external/webrtc/rtc_base/sigslottester.h.pump (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
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.
9 */
10
11#ifndef RTC_BASE_SIGSLOTTESTER_H_
12#define RTC_BASE_SIGSLOTTESTER_H_
13
14// To generate sigslottester.h from sigslottester.h.pump, execute:
15// /home/build/google3/third_party/gtest/scripts/pump.py sigslottester.h.pump
16
17
18// SigslotTester(s) are utility classes to check if signals owned by an
19// object are being invoked at the right time and with the right arguments.
20// They are meant to be used in tests. Tests must provide "capture" pointers
21// (i.e. address of variables) where the arguments from the signal callback
22// can be stored.
23//
24// Example:
25//   /* Some signal */
26//   sigslot::signal1<const std::string&> foo;
27//
28//   /* We want to monitor foo in some test. Note how signal argument is
29//      const std::string&, but capture-type is std::string. Capture type
30//      must be type that can be assigned to. */
31//   std::string capture;
32//   SigslotTester1<const std::string&, std::string> slot(&foo, &capture);
33//   foo.emit("hello");
34//   EXPECT_EQ(1, slot.callback_count());
35//   EXPECT_EQ("hello", capture);
36//   /* See unit-tests for more examples */
37
38#include "rtc_base/third_party/sigslot/sigslot.h"
39
40namespace rtc {
41
42// Base version for testing signals that passes no arguments.
43class SigslotTester0 : public sigslot::has_slots<> {
44 public:
45  explicit SigslotTester0(sigslot::signal0<>* signal) : callback_count_(0) {
46    signal->connect(this, &SigslotTester0::OnSignalCallback);
47  }
48
49  SigslotTester0(const SigslotTester0&) = delete;
50  SigslotTester0& operator=(const SigslotTester0&) = delete;
51
52  int callback_count() const { return callback_count_; }
53
54 private:
55  void OnSignalCallback() { callback_count_++; }
56  int callback_count_;
57};
58
59// Versions below are for testing signals that pass arguments. For all the
60// templates below:
61// - A1-A5 is the type of the argument i in the callback. Signals may and often
62//   do use const-references here for efficiency.
63// - C1-C5 is the type of the variable to capture argument i. These should be
64//   non-const value types suitable for use as lvalues.
65
66$var n = 5
67$range i 1..n
68$for i [[
69$range j 1..i
70
71template <$for j , [[class A$j]], $for j , [[class C$j]]>
72class SigslotTester$i : public sigslot::has_slots<> {
73 public:
74  SigslotTester$i(sigslot::signal$i<$for j , [[A$j]]>* signal,
75                $for j , [[C$j* capture$j]])
76      : callback_count_(0),
77      $for j , [[capture$j[[]]_(capture$j)]] {
78    signal->connect(this, &SigslotTester$i::OnSignalCallback);
79  }
80
81  SigslotTester$i(const SigslotTester$i&) = delete;
82  SigslotTester$i& operator=(const SigslotTester$i&) = delete;
83
84  int callback_count() const { return callback_count_; }
85
86 private:
87  void OnSignalCallback($for j , [[A$j arg$j]]) {
88    callback_count_++;$for j [[
89
90    *capture$j[[]]_ = arg$j;]]
91
92  }
93
94  int callback_count_;$for j [[
95
96  C$j* capture$j[[]]_;]]
97};
98
99]]
100}  // namespace rtc
101
102#endif  // RTC_BASE_SIGSLOTTESTER_H_
103