blob: e77276875bdcee74e6099a3e809591b8cc530105 [file] [log] [blame]
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001// This file was GENERATED by command:
2// pump.py callback.h.pump
3// DO NOT EDIT BY HAND!!!
4
5/*
6 * libjingle
7 * Copyright 2012 Google Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
23 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32// To generate callback.h from callback.h.pump, execute:
33// /home/build/google3/third_party/gtest/scripts/pump.py callback.h.pump
34
35// Callbacks are callable object containers. They can hold a function pointer
36// or a function object and behave like a value type. Internally, data is
37// reference-counted, making copies and pass-by-value inexpensive.
38//
39// Callbacks are typed using template arguments. The format is:
40// CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
41// where N is the number of arguments supplied to the callable object.
42// Callbacks are invoked using operator(), just like a function or a function
43// object. Default-constructed callbacks are "empty," and executing an empty
44// callback does nothing. A callback can be made empty by assigning it from
45// a default-constructed callback.
46//
47// Callbacks are similar in purpose to std::function (which isn't available on
48// all platforms we support) and a lightweight alternative to sigslots. Since
49// they effectively hide the type of the object they call, they're useful in
50// breaking dependencies between objects that need to interact with one another.
51// Notably, they can hold the results of Bind(), std::bind*, etc, without
52// needing
53// to know the resulting object type of those calls.
54//
55// Sigslots, on the other hand, provide a fuller feature set, such as multiple
56// subscriptions to a signal, optional thread-safety, and lifetime tracking of
57// slots. When these features are needed, choose sigslots.
58//
59// Example:
60// int sqr(int x) { return x * x; }
61// struct AddK {
62// int k;
63// int operator()(int x) const { return x + k; }
64// } add_k = {5};
65//
66// Callback1<int, int> my_callback;
67// cout << my_callback.empty() << endl; // true
68//
69// my_callback = Callback1<int, int>(&sqr);
70// cout << my_callback.empty() << endl; // false
71// cout << my_callback(3) << endl; // 9
72//
73// my_callback = Callback1<int, int>(add_k);
74// cout << my_callback(10) << endl; // 15
75//
76// my_callback = Callback1<int, int>();
77// cout << my_callback.empty() << endl; // true
78
79#ifndef TALK_BASE_CALLBACK_H_
80#define TALK_BASE_CALLBACK_H_
81
82#include "talk/base/logging.h"
83#include "talk/base/refcount.h"
84#include "talk/base/scoped_ref_ptr.h"
85
86namespace talk_base {
87
88template <class R>
89class Callback0 {
90 public:
91 // Default copy operations are appropriate for this class.
92 Callback0() {}
93 template <class T> Callback0(const T& functor)
94 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
95 R operator()() {
96 if (empty()) {
97 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
98 return R();
99 }
100 return helper_->Run();
101 }
102 bool empty() const { return !helper_; }
103
104 private:
105 struct Helper : RefCountInterface {
106 virtual ~Helper() {}
107 virtual R Run() = 0;
108 };
109 template <class T> struct HelperImpl : Helper {
110 explicit HelperImpl(const T& functor) : functor_(functor) {}
111 virtual R Run() {
112 return functor_();
113 }
114 T functor_;
115 };
116 scoped_refptr<Helper> helper_;
117};
118
119template <class R,
120 class P1>
121class Callback1 {
122 public:
123 // Default copy operations are appropriate for this class.
124 Callback1() {}
125 template <class T> Callback1(const T& functor)
126 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
127 R operator()(P1 p1) {
128 if (empty()) {
129 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
130 return R();
131 }
132 return helper_->Run(p1);
133 }
134 bool empty() const { return !helper_; }
135
136 private:
137 struct Helper : RefCountInterface {
138 virtual ~Helper() {}
139 virtual R Run(P1 p1) = 0;
140 };
141 template <class T> struct HelperImpl : Helper {
142 explicit HelperImpl(const T& functor) : functor_(functor) {}
143 virtual R Run(P1 p1) {
144 return functor_(p1);
145 }
146 T functor_;
147 };
148 scoped_refptr<Helper> helper_;
149};
150
151template <class R,
152 class P1,
153 class P2>
154class Callback2 {
155 public:
156 // Default copy operations are appropriate for this class.
157 Callback2() {}
158 template <class T> Callback2(const T& functor)
159 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
160 R operator()(P1 p1, P2 p2) {
161 if (empty()) {
162 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
163 return R();
164 }
165 return helper_->Run(p1, p2);
166 }
167 bool empty() const { return !helper_; }
168
169 private:
170 struct Helper : RefCountInterface {
171 virtual ~Helper() {}
172 virtual R Run(P1 p1, P2 p2) = 0;
173 };
174 template <class T> struct HelperImpl : Helper {
175 explicit HelperImpl(const T& functor) : functor_(functor) {}
176 virtual R Run(P1 p1, P2 p2) {
177 return functor_(p1, p2);
178 }
179 T functor_;
180 };
181 scoped_refptr<Helper> helper_;
182};
183
184template <class R,
185 class P1,
186 class P2,
187 class P3>
188class Callback3 {
189 public:
190 // Default copy operations are appropriate for this class.
191 Callback3() {}
192 template <class T> Callback3(const T& functor)
193 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
194 R operator()(P1 p1, P2 p2, P3 p3) {
195 if (empty()) {
196 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
197 return R();
198 }
199 return helper_->Run(p1, p2, p3);
200 }
201 bool empty() const { return !helper_; }
202
203 private:
204 struct Helper : RefCountInterface {
205 virtual ~Helper() {}
206 virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
207 };
208 template <class T> struct HelperImpl : Helper {
209 explicit HelperImpl(const T& functor) : functor_(functor) {}
210 virtual R Run(P1 p1, P2 p2, P3 p3) {
211 return functor_(p1, p2, p3);
212 }
213 T functor_;
214 };
215 scoped_refptr<Helper> helper_;
216};
217
218template <class R,
219 class P1,
220 class P2,
221 class P3,
222 class P4>
223class Callback4 {
224 public:
225 // Default copy operations are appropriate for this class.
226 Callback4() {}
227 template <class T> Callback4(const T& functor)
228 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
229 R operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
230 if (empty()) {
231 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
232 return R();
233 }
234 return helper_->Run(p1, p2, p3, p4);
235 }
236 bool empty() const { return !helper_; }
237
238 private:
239 struct Helper : RefCountInterface {
240 virtual ~Helper() {}
241 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
242 };
243 template <class T> struct HelperImpl : Helper {
244 explicit HelperImpl(const T& functor) : functor_(functor) {}
245 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) {
246 return functor_(p1, p2, p3, p4);
247 }
248 T functor_;
249 };
250 scoped_refptr<Helper> helper_;
251};
252
253template <class R,
254 class P1,
255 class P2,
256 class P3,
257 class P4,
258 class P5>
259class Callback5 {
260 public:
261 // Default copy operations are appropriate for this class.
262 Callback5() {}
263 template <class T> Callback5(const T& functor)
264 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
265 R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
266 if (empty()) {
267 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
268 return R();
269 }
270 return helper_->Run(p1, p2, p3, p4, p5);
271 }
272 bool empty() const { return !helper_; }
273
274 private:
275 struct Helper : RefCountInterface {
276 virtual ~Helper() {}
277 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
278 };
279 template <class T> struct HelperImpl : Helper {
280 explicit HelperImpl(const T& functor) : functor_(functor) {}
281 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
282 return functor_(p1, p2, p3, p4, p5);
283 }
284 T functor_;
285 };
286 scoped_refptr<Helper> helper_;
287};
288} // namespace talk_base
289
290
291#endif // TALK_BASE_CALLBACK_H_