blob: 11fbf86badb2d635049a24ab1046ffa919213d23 [file] [log] [blame]
sergeyu@chromium.org70022fa2014-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()() {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +000096 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +000097 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +000098 return helper_->Run();
99 }
100 bool empty() const { return !helper_; }
101
102 private:
103 struct Helper : RefCountInterface {
104 virtual ~Helper() {}
105 virtual R Run() = 0;
106 };
107 template <class T> struct HelperImpl : Helper {
108 explicit HelperImpl(const T& functor) : functor_(functor) {}
109 virtual R Run() {
110 return functor_();
111 }
112 T functor_;
113 };
114 scoped_refptr<Helper> helper_;
115};
116
117template <class R,
118 class P1>
119class Callback1 {
120 public:
121 // Default copy operations are appropriate for this class.
122 Callback1() {}
123 template <class T> Callback1(const T& functor)
124 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
125 R operator()(P1 p1) {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +0000126 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000127 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000128 return helper_->Run(p1);
129 }
130 bool empty() const { return !helper_; }
131
132 private:
133 struct Helper : RefCountInterface {
134 virtual ~Helper() {}
135 virtual R Run(P1 p1) = 0;
136 };
137 template <class T> struct HelperImpl : Helper {
138 explicit HelperImpl(const T& functor) : functor_(functor) {}
139 virtual R Run(P1 p1) {
140 return functor_(p1);
141 }
142 T functor_;
143 };
144 scoped_refptr<Helper> helper_;
145};
146
147template <class R,
148 class P1,
149 class P2>
150class Callback2 {
151 public:
152 // Default copy operations are appropriate for this class.
153 Callback2() {}
154 template <class T> Callback2(const T& functor)
155 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
156 R operator()(P1 p1, P2 p2) {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +0000157 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000158 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000159 return helper_->Run(p1, p2);
160 }
161 bool empty() const { return !helper_; }
162
163 private:
164 struct Helper : RefCountInterface {
165 virtual ~Helper() {}
166 virtual R Run(P1 p1, P2 p2) = 0;
167 };
168 template <class T> struct HelperImpl : Helper {
169 explicit HelperImpl(const T& functor) : functor_(functor) {}
170 virtual R Run(P1 p1, P2 p2) {
171 return functor_(p1, p2);
172 }
173 T functor_;
174 };
175 scoped_refptr<Helper> helper_;
176};
177
178template <class R,
179 class P1,
180 class P2,
181 class P3>
182class Callback3 {
183 public:
184 // Default copy operations are appropriate for this class.
185 Callback3() {}
186 template <class T> Callback3(const T& functor)
187 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
188 R operator()(P1 p1, P2 p2, P3 p3) {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +0000189 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000190 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000191 return helper_->Run(p1, p2, p3);
192 }
193 bool empty() const { return !helper_; }
194
195 private:
196 struct Helper : RefCountInterface {
197 virtual ~Helper() {}
198 virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
199 };
200 template <class T> struct HelperImpl : Helper {
201 explicit HelperImpl(const T& functor) : functor_(functor) {}
202 virtual R Run(P1 p1, P2 p2, P3 p3) {
203 return functor_(p1, p2, p3);
204 }
205 T functor_;
206 };
207 scoped_refptr<Helper> helper_;
208};
209
210template <class R,
211 class P1,
212 class P2,
213 class P3,
214 class P4>
215class Callback4 {
216 public:
217 // Default copy operations are appropriate for this class.
218 Callback4() {}
219 template <class T> Callback4(const T& functor)
220 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
221 R operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +0000222 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000223 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000224 return helper_->Run(p1, p2, p3, p4);
225 }
226 bool empty() const { return !helper_; }
227
228 private:
229 struct Helper : RefCountInterface {
230 virtual ~Helper() {}
231 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
232 };
233 template <class T> struct HelperImpl : Helper {
234 explicit HelperImpl(const T& functor) : functor_(functor) {}
235 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) {
236 return functor_(p1, p2, p3, p4);
237 }
238 T functor_;
239 };
240 scoped_refptr<Helper> helper_;
241};
242
243template <class R,
244 class P1,
245 class P2,
246 class P3,
247 class P4,
248 class P5>
249class Callback5 {
250 public:
251 // Default copy operations are appropriate for this class.
252 Callback5() {}
253 template <class T> Callback5(const T& functor)
254 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
255 R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
sergeyu@chromium.org9d891622014-02-08 03:18:03 +0000256 if (empty())
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000257 return R();
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000258 return helper_->Run(p1, p2, p3, p4, p5);
259 }
260 bool empty() const { return !helper_; }
261
262 private:
263 struct Helper : RefCountInterface {
264 virtual ~Helper() {}
265 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
266 };
267 template <class T> struct HelperImpl : Helper {
268 explicit HelperImpl(const T& functor) : functor_(functor) {}
269 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
270 return functor_(p1, p2, p3, p4, p5);
271 }
272 T functor_;
273 };
274 scoped_refptr<Helper> helper_;
275};
276} // namespace talk_base
277
sergeyu@chromium.org70022fa2014-02-07 19:03:26 +0000278#endif // TALK_BASE_CALLBACK_H_