Line data Source code
1 : // 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:
16 : // /home/build/google3/third_party/gtest/scripts/pump.py callback.h.pump
17 :
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 :
62 : #ifndef WEBRTC_BASE_CALLBACK_H_
63 : #define WEBRTC_BASE_CALLBACK_H_
64 :
65 : #include "webrtc/base/refcount.h"
66 : #include "webrtc/base/scoped_ref_ptr.h"
67 :
68 : namespace rtc {
69 :
70 : template <class R>
71 0 : class Callback0 {
72 : public:
73 : // Default copy operations are appropriate for this class.
74 0 : Callback0() {}
75 0 : template <class T> Callback0(const T& functor)
76 0 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
77 0 : R operator()() {
78 0 : if (empty())
79 0 : return R();
80 0 : return helper_->Run();
81 : }
82 0 : bool empty() const { return !helper_; }
83 :
84 : private:
85 0 : struct Helper : RefCountInterface {
86 0 : virtual ~Helper() {}
87 : virtual R Run() = 0;
88 : };
89 0 : template <class T> struct HelperImpl : Helper {
90 0 : explicit HelperImpl(const T& functor) : functor_(functor) {}
91 0 : virtual R Run() {
92 0 : return functor_();
93 : }
94 : T functor_;
95 : };
96 : scoped_refptr<Helper> helper_;
97 : };
98 :
99 : template <class R,
100 : class P1>
101 : class Callback1 {
102 : public:
103 : // Default copy operations are appropriate for this class.
104 : Callback1() {}
105 : template <class T> Callback1(const T& functor)
106 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
107 : R operator()(P1 p1) {
108 : if (empty())
109 : return R();
110 : return helper_->Run(p1);
111 : }
112 : bool empty() const { return !helper_; }
113 :
114 : private:
115 : struct Helper : RefCountInterface {
116 : virtual ~Helper() {}
117 : virtual R Run(P1 p1) = 0;
118 : };
119 : template <class T> struct HelperImpl : Helper {
120 : explicit HelperImpl(const T& functor) : functor_(functor) {}
121 : virtual R Run(P1 p1) {
122 : return functor_(p1);
123 : }
124 : T functor_;
125 : };
126 : scoped_refptr<Helper> helper_;
127 : };
128 :
129 : template <class R,
130 : class P1,
131 : class P2>
132 : class Callback2 {
133 : public:
134 : // Default copy operations are appropriate for this class.
135 : Callback2() {}
136 : template <class T> Callback2(const T& functor)
137 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
138 : R operator()(P1 p1, P2 p2) {
139 : if (empty())
140 : return R();
141 : return helper_->Run(p1, p2);
142 : }
143 : bool empty() const { return !helper_; }
144 :
145 : private:
146 : struct Helper : RefCountInterface {
147 : virtual ~Helper() {}
148 : virtual R Run(P1 p1, P2 p2) = 0;
149 : };
150 : template <class T> struct HelperImpl : Helper {
151 : explicit HelperImpl(const T& functor) : functor_(functor) {}
152 : virtual R Run(P1 p1, P2 p2) {
153 : return functor_(p1, p2);
154 : }
155 : T functor_;
156 : };
157 : scoped_refptr<Helper> helper_;
158 : };
159 :
160 : template <class R,
161 : class P1,
162 : class P2,
163 : class P3>
164 : class Callback3 {
165 : public:
166 : // Default copy operations are appropriate for this class.
167 : Callback3() {}
168 : template <class T> Callback3(const T& functor)
169 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
170 : R operator()(P1 p1, P2 p2, P3 p3) {
171 : if (empty())
172 : return R();
173 : return helper_->Run(p1, p2, p3);
174 : }
175 : bool empty() const { return !helper_; }
176 :
177 : private:
178 : struct Helper : RefCountInterface {
179 : virtual ~Helper() {}
180 : virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
181 : };
182 : template <class T> struct HelperImpl : Helper {
183 : explicit HelperImpl(const T& functor) : functor_(functor) {}
184 : virtual R Run(P1 p1, P2 p2, P3 p3) {
185 : return functor_(p1, p2, p3);
186 : }
187 : T functor_;
188 : };
189 : scoped_refptr<Helper> helper_;
190 : };
191 :
192 : template <class R,
193 : class P1,
194 : class P2,
195 : class P3,
196 : class P4>
197 : class Callback4 {
198 : public:
199 : // Default copy operations are appropriate for this class.
200 : Callback4() {}
201 : template <class T> Callback4(const T& functor)
202 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
203 : R operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
204 : if (empty())
205 : return R();
206 : return helper_->Run(p1, p2, p3, p4);
207 : }
208 : bool empty() const { return !helper_; }
209 :
210 : private:
211 : struct Helper : RefCountInterface {
212 : virtual ~Helper() {}
213 : virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
214 : };
215 : template <class T> struct HelperImpl : Helper {
216 : explicit HelperImpl(const T& functor) : functor_(functor) {}
217 : virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) {
218 : return functor_(p1, p2, p3, p4);
219 : }
220 : T functor_;
221 : };
222 : scoped_refptr<Helper> helper_;
223 : };
224 :
225 : template <class R,
226 : class P1,
227 : class P2,
228 : class P3,
229 : class P4,
230 : class P5>
231 : class Callback5 {
232 : public:
233 : // Default copy operations are appropriate for this class.
234 : Callback5() {}
235 : template <class T> Callback5(const T& functor)
236 : : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
237 : R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
238 : if (empty())
239 : return R();
240 : return helper_->Run(p1, p2, p3, p4, p5);
241 : }
242 : bool empty() const { return !helper_; }
243 :
244 : private:
245 : struct Helper : RefCountInterface {
246 : virtual ~Helper() {}
247 : virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
248 : };
249 : template <class T> struct HelperImpl : Helper {
250 : explicit HelperImpl(const T& functor) : functor_(functor) {}
251 : virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
252 : return functor_(p1, p2, p3, p4, p5);
253 : }
254 : T functor_;
255 : };
256 : scoped_refptr<Helper> helper_;
257 : };
258 : } // namespace rtc
259 :
260 : #endif // WEBRTC_BASE_CALLBACK_H_
|