blob: ae9ab87de000676d7ce4bdab7158a5664ec62a3c [file] [log] [blame]
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001/*
2 * libjingle
3 * Copyright 2012 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// To generate callback.h from callback.h.pump, execute:
29// /home/build/google3/third_party/gtest/scripts/pump.py callback.h.pump
30
31// Callbacks are callable object containers. They can hold a function pointer
32// or a function object and behave like a value type. Internally, data is
33// reference-counted, making copies and pass-by-value inexpensive.
34//
35// Callbacks are typed using template arguments. The format is:
36// CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
37// where N is the number of arguments supplied to the callable object.
38// Callbacks are invoked using operator(), just like a function or a function
39// object. Default-constructed callbacks are "empty," and executing an empty
40// callback does nothing. A callback can be made empty by assigning it from
41// a default-constructed callback.
42//
43// Callbacks are similar in purpose to std::function (which isn't available on
44// all platforms we support) and a lightweight alternative to sigslots. Since
45// they effectively hide the type of the object they call, they're useful in
46// breaking dependencies between objects that need to interact with one another.
47// Notably, they can hold the results of Bind(), std::bind*, etc, without needing
48// to know the resulting object type of those calls.
49//
50// Sigslots, on the other hand, provide a fuller feature set, such as multiple
51// subscriptions to a signal, optional thread-safety, and lifetime tracking of
52// slots. When these features are needed, choose sigslots.
53//
54// Example:
55// int sqr(int x) { return x * x; }
56// struct AddK {
57// int k;
58// int operator()(int x) const { return x + k; }
59// } add_k = {5};
60//
61// Callback1<int, int> my_callback;
62// cout << my_callback.empty() << endl; // true
63//
64// my_callback = Callback1<int, int>(&sqr);
65// cout << my_callback.empty() << endl; // false
66// cout << my_callback(3) << endl; // 9
67//
68// my_callback = Callback1<int, int>(add_k);
69// cout << my_callback(10) << endl; // 15
70//
71// my_callback = Callback1<int, int>();
72// cout << my_callback.empty() << endl; // true
73
74#ifndef TALK_BASE_CALLBACK_H_
75#define TALK_BASE_CALLBACK_H_
76
77#include "talk/base/logging.h"
78#include "talk/base/refcount.h"
79#include "talk/base/scoped_ref_ptr.h"
80
81namespace talk_base {
82
83$var n = 5
84$range i 0..n
85$for i [[
86$range j 1..i
87
88template <class R$for j [[,
89 class P$j]]>
90class Callback$i {
91 public:
92 // Default copy operations are appropriate for this class.
93 Callback$i() {}
94 template <class T> Callback$i(const T& functor)
95 : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
96 R operator()($for j , [[P$j p$j]]) {
97 if (empty()) {
98 LOG_F(LS_WARNING) << "Tried to execute an empty callback.";
99 return R();
100 }
101 return helper_->Run($for j , [[p$j]]);
102 }
103 bool empty() const { return !helper_; }
104
105 private:
106 struct Helper : RefCountInterface {
107 virtual ~Helper() {}
108 virtual R Run($for j , [[P$j p$j]]) = 0;
109 };
110 template <class T> struct HelperImpl : Helper {
111 explicit HelperImpl(const T& functor) : functor_(functor) {}
112 virtual R Run($for j , [[P$j p$j]]) {
113 return functor_($for j , [[p$j]]);
114 }
115 T functor_;
116 };
117 scoped_refptr<Helper> helper_;
118};
119
120]]
121} // namespace talk_base
122
123#endif // TALK_BASE_CALLBACK_H_