sergeyu@chromium.org | 70022fa | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
sergeyu@chromium.org | 70022fa | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 77 | #include "talk/base/refcount.h" |
| 78 | #include "talk/base/scoped_ref_ptr.h" |
| 79 | |
| 80 | namespace talk_base { |
| 81 | |
| 82 | $var n = 5 |
| 83 | $range i 0..n |
| 84 | $for i [[ |
| 85 | $range j 1..i |
| 86 | |
| 87 | template <class R$for j [[, |
| 88 | class P$j]]> |
| 89 | class Callback$i { |
| 90 | public: |
| 91 | // Default copy operations are appropriate for this class. |
| 92 | Callback$i() {} |
| 93 | template <class T> Callback$i(const T& functor) |
| 94 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 95 | R operator()($for j , [[P$j p$j]]) { |
sergeyu@chromium.org | 9d89162 | 2014-02-08 03:18:03 +0000 | [diff] [blame] | 96 | if (empty()) |
sergeyu@chromium.org | 70022fa | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 97 | return R(); |
sergeyu@chromium.org | 70022fa | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 98 | return helper_->Run($for j , [[p$j]]); |
| 99 | } |
| 100 | bool empty() const { return !helper_; } |
| 101 | |
| 102 | private: |
| 103 | struct Helper : RefCountInterface { |
| 104 | virtual ~Helper() {} |
| 105 | virtual R Run($for j , [[P$j p$j]]) = 0; |
| 106 | }; |
| 107 | template <class T> struct HelperImpl : Helper { |
| 108 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 109 | virtual R Run($for j , [[P$j p$j]]) { |
| 110 | return functor_($for j , [[p$j]]); |
| 111 | } |
| 112 | T functor_; |
| 113 | }; |
| 114 | scoped_refptr<Helper> helper_; |
| 115 | }; |
| 116 | |
| 117 | ]] |
| 118 | } // namespace talk_base |
| 119 | |
sergeyu@chromium.org | 9d89162 | 2014-02-08 03:18:03 +0000 | [diff] [blame] | 120 | #endif // TALK_BASE_CALLBACK_H_ |