1 // This file was GENERATED by command:
2 // pump.py callback.h.pump
3 // DO NOT EDIT BY HAND!!!
7 * Copyright 2012 Google Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
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.
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.
32 // To generate callback.h from callback.h.pump, execute:
33 // /home/build/google3/third_party/gtest/scripts/pump.py callback.h.pump
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.
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.
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
53 // to know the resulting object type of those calls.
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.
60 // int sqr(int x) { return x * x; }
63 // int operator()(int x) const { return x + k; }
66 // Callback1<int, int> my_callback;
67 // cout << my_callback.empty() << endl; // true
69 // my_callback = Callback1<int, int>(&sqr);
70 // cout << my_callback.empty() << endl; // false
71 // cout << my_callback(3) << endl; // 9
73 // my_callback = Callback1<int, int>(add_k);
74 // cout << my_callback(10) << endl; // 15
76 // my_callback = Callback1<int, int>();
77 // cout << my_callback.empty() << endl; // true
79 #ifndef TALK_BASE_CALLBACK_H_
80 #define TALK_BASE_CALLBACK_H_
82 #include "talk/base/logging.h"
83 #include "talk/base/refcount.h"
84 #include "talk/base/scoped_ref_ptr.h"
91 // Default copy operations are appropriate for this class.
93 template <class T> Callback0(const T& functor)
94 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
98 return helper_->Run();
100 bool empty() const { return !helper_; }
103 struct Helper : RefCountInterface {
107 template <class T> struct HelperImpl : Helper {
108 explicit HelperImpl(const T& functor) : functor_(functor) {}
114 scoped_refptr<Helper> helper_;
121 // Default copy operations are appropriate for this class.
123 template <class T> Callback1(const T& functor)
124 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
125 R operator()(P1 p1) {
128 return helper_->Run(p1);
130 bool empty() const { return !helper_; }
133 struct Helper : RefCountInterface {
135 virtual R Run(P1 p1) = 0;
137 template <class T> struct HelperImpl : Helper {
138 explicit HelperImpl(const T& functor) : functor_(functor) {}
139 virtual R Run(P1 p1) {
144 scoped_refptr<Helper> helper_;
152 // Default copy operations are appropriate for this class.
154 template <class T> Callback2(const T& functor)
155 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
156 R operator()(P1 p1, P2 p2) {
159 return helper_->Run(p1, p2);
161 bool empty() const { return !helper_; }
164 struct Helper : RefCountInterface {
166 virtual R Run(P1 p1, P2 p2) = 0;
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);
175 scoped_refptr<Helper> helper_;
184 // Default copy operations are appropriate for this class.
186 template <class T> Callback3(const T& functor)
187 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
188 R operator()(P1 p1, P2 p2, P3 p3) {
191 return helper_->Run(p1, p2, p3);
193 bool empty() const { return !helper_; }
196 struct Helper : RefCountInterface {
198 virtual R Run(P1 p1, P2 p2, P3 p3) = 0;
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);
207 scoped_refptr<Helper> helper_;
217 // Default copy operations are appropriate for this class.
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) {
224 return helper_->Run(p1, p2, p3, p4);
226 bool empty() const { return !helper_; }
229 struct Helper : RefCountInterface {
231 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0;
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);
240 scoped_refptr<Helper> helper_;
251 // Default copy operations are appropriate for this class.
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) {
258 return helper_->Run(p1, p2, p3, p4, p5);
260 bool empty() const { return !helper_; }
263 struct Helper : RefCountInterface {
265 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0;
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);
274 scoped_refptr<Helper> helper_;
276 } // namespace talk_base
278 #endif // TALK_BASE_CALLBACK_H_