blob: 65b7c9cdf35ae9c813b89ef542829c9aabf3b923 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001// This file was GENERATED by command:
2// pump.py callback.h.pump
3// DO NOT EDIT BY HAND!!!
4
5/*
6 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
7 *
8 * Use of this source code is governed by a BSD-style license
9 * that can be found in the LICENSE file in the root of the source
10 * tree. An additional intellectual property rights grant can be found
11 * in the file PATENTS. All contributing project authors may
12 * be found in the AUTHORS file in the root of the source tree.
13 */
14
15// To generate callback.h from callback.h.pump, execute:
Niels Möller84255bb2017-10-06 13:43:23 +020016// ../third_party/googletest/src/googletest/scripts/pump.py callback.h.pump
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18// Callbacks are callable object containers. They can hold a function pointer
19// or a function object and behave like a value type. Internally, data is
20// reference-counted, making copies and pass-by-value inexpensive.
21//
22// Callbacks are typed using template arguments. The format is:
23// CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
24// where N is the number of arguments supplied to the callable object.
25// Callbacks are invoked using operator(), just like a function or a function
26// object. Default-constructed callbacks are "empty," and executing an empty
27// callback does nothing. A callback can be made empty by assigning it from
28// a default-constructed callback.
29//
30// Callbacks are similar in purpose to std::function (which isn't available on
31// all platforms we support) and a lightweight alternative to sigslots. Since
32// they effectively hide the type of the object they call, they're useful in
33// breaking dependencies between objects that need to interact with one another.
34// Notably, they can hold the results of Bind(), std::bind*, etc, without
35// needing
36// to know the resulting object type of those calls.
37//
38// Sigslots, on the other hand, provide a fuller feature set, such as multiple
39// subscriptions to a signal, optional thread-safety, and lifetime tracking of
40// slots. When these features are needed, choose sigslots.
41//
42// Example:
43// int sqr(int x) { return x * x; }
44// struct AddK {
45// int k;
46// int operator()(int x) const { return x + k; }
47// } add_k = {5};
48//
49// Callback1<int, int> my_callback;
50// cout << my_callback.empty() << endl; // true
51//
52// my_callback = Callback1<int, int>(&sqr);
53// cout << my_callback.empty() << endl; // false
54// cout << my_callback(3) << endl; // 9
55//
56// my_callback = Callback1<int, int>(add_k);
57// cout << my_callback(10) << endl; // 15
58//
59// my_callback = Callback1<int, int>();
60// cout << my_callback.empty() << endl; // true
61
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#ifndef RTC_BASE_CALLBACK_H_
63#define RTC_BASE_CALLBACK_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020066#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#include "rtc_base/scoped_ref_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069namespace rtc {
70
71template <class R>
72class Callback0 {
73 public:
74 // Default copy operations are appropriate for this class.
75 Callback0() {}
Yves Gerey665174f2018-06-19 15:03:05 +020076 template <class T>
77 Callback0(const T& functor)
78 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 R operator()() {
80 if (empty())
81 return R();
82 return helper_->Run();
83 }
84 bool empty() const { return !helper_; }
85
86 private:
87 struct Helper : RefCountInterface {
88 virtual ~Helper() {}
89 virtual R Run() = 0;
90 };
Yves Gerey665174f2018-06-19 15:03:05 +020091 template <class T>
92 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093 explicit HelperImpl(const T& functor) : functor_(functor) {}
Yves Gerey665174f2018-06-19 15:03:05 +020094 virtual R Run() { return functor_(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 T functor_;
96 };
97 scoped_refptr<Helper> helper_;
98};
99
Yves Gerey665174f2018-06-19 15:03:05 +0200100template <class R, class P1>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101class Callback1 {
102 public:
103 // Default copy operations are appropriate for this class.
104 Callback1() {}
Yves Gerey665174f2018-06-19 15:03:05 +0200105 template <class T>
106 Callback1(const T& functor)
107 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108 R operator()(P1 p1) {
109 if (empty())
110 return R();
111 return helper_->Run(p1);
112 }
113 bool empty() const { return !helper_; }
114
115 private:
116 struct Helper : RefCountInterface {
117 virtual ~Helper() {}
118 virtual R Run(P1 p1) = 0;
119 };
Yves Gerey665174f2018-06-19 15:03:05 +0200120 template <class T>
121 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 explicit HelperImpl(const T& functor) : functor_(functor) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200123 virtual R Run(P1 p1) { return functor_(p1); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 T functor_;
125 };
126 scoped_refptr<Helper> helper_;
127};
128
Yves Gerey665174f2018-06-19 15:03:05 +0200129template <class R, class P1, class P2>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200130class Callback2 {
131 public:
132 // Default copy operations are appropriate for this class.
133 Callback2() {}
Yves Gerey665174f2018-06-19 15:03:05 +0200134 template <class T>
135 Callback2(const T& functor)
136 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137 R operator()(P1 p1, P2 p2) {
138 if (empty())
139 return R();
140 return helper_->Run(p1, p2);
141 }
142 bool empty() const { return !helper_; }
143
144 private:
145 struct Helper : RefCountInterface {
146 virtual ~Helper() {}
147 virtual R Run(P1 p1, P2 p2) = 0;
148 };
Yves Gerey665174f2018-06-19 15:03:05 +0200149 template <class T>
150 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 explicit HelperImpl(const T& functor) : functor_(functor) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200152 virtual R Run(P1 p1, P2 p2) { return functor_(p1, p2); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153 T functor_;
154 };
155 scoped_refptr<Helper> helper_;
156};
157
Yves Gerey665174f2018-06-19 15:03:05 +0200158template <class R, class P1, class P2, class P3>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159class Callback3 {
160 public:
161 // Default copy operations are appropriate for this class.
162 Callback3() {}
Yves Gerey665174f2018-06-19 15:03:05 +0200163 template <class T>
164 Callback3(const T& functor)
165 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200166 R operator()(P1 p1, P2 p2, P3 p3) {
167 if (empty())
168 return R();
169 return helper_->Run(p1, p2, p3);
170 }
171 bool empty() const { return !helper_; }
172
173 private:
174 struct Helper : RefCountInterface {
175 virtual ~Helper() {}
176 virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
177 };
Yves Gerey665174f2018-06-19 15:03:05 +0200178 template <class T>
179 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200180 explicit HelperImpl(const T& functor) : functor_(functor) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200181 virtual R Run(P1 p1, P2 p2, P3 p3) { return functor_(p1, p2, p3); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200182 T functor_;
183 };
184 scoped_refptr<Helper> helper_;
185};
186
Yves Gerey665174f2018-06-19 15:03:05 +0200187template <class R, class P1, class P2, class P3, class P4>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200188class Callback4 {
189 public:
190 // Default copy operations are appropriate for this class.
191 Callback4() {}
Yves Gerey665174f2018-06-19 15:03:05 +0200192 template <class T>
193 Callback4(const T& functor)
194 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200195 R operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
196 if (empty())
197 return R();
198 return helper_->Run(p1, p2, p3, p4);
199 }
200 bool empty() const { return !helper_; }
201
202 private:
203 struct Helper : RefCountInterface {
204 virtual ~Helper() {}
205 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
206 };
Yves Gerey665174f2018-06-19 15:03:05 +0200207 template <class T>
208 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200209 explicit HelperImpl(const T& functor) : functor_(functor) {}
210 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) {
211 return functor_(p1, p2, p3, p4);
212 }
213 T functor_;
214 };
215 scoped_refptr<Helper> helper_;
216};
217
Yves Gerey665174f2018-06-19 15:03:05 +0200218template <class R, class P1, class P2, class P3, class P4, class P5>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200219class Callback5 {
220 public:
221 // Default copy operations are appropriate for this class.
222 Callback5() {}
Yves Gerey665174f2018-06-19 15:03:05 +0200223 template <class T>
224 Callback5(const T& functor)
225 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200226 R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
227 if (empty())
228 return R();
229 return helper_->Run(p1, p2, p3, p4, p5);
230 }
231 bool empty() const { return !helper_; }
232
233 private:
234 struct Helper : RefCountInterface {
235 virtual ~Helper() {}
236 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
237 };
Yves Gerey665174f2018-06-19 15:03:05 +0200238 template <class T>
239 struct HelperImpl : Helper {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200240 explicit HelperImpl(const T& functor) : functor_(functor) {}
241 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
242 return functor_(p1, p2, p3, p4, p5);
243 }
244 T functor_;
245 };
246 scoped_refptr<Helper> helper_;
247};
248} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200250#endif // RTC_BASE_CALLBACK_H_