henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 1 | // This file was GENERATED by command: |
| 2 | // pump.py callback.h.pump |
| 3 | // DO NOT EDIT BY HAND!!! |
| 4 | |
| 5 | /* |
| 6 | * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 7 | * |
| 8 | * Use of this source code is governed by a BSD-style license |
| 9 | * that can be found in the LICENSE file in the root of the source |
| 10 | * tree. An additional intellectual property rights grant can be found |
| 11 | * in the file PATENTS. All contributing project authors may |
| 12 | * be found in the AUTHORS file in the root of the source tree. |
| 13 | */ |
| 14 | |
| 15 | // To generate callback.h from callback.h.pump, execute: |
| 16 | // /home/build/google3/third_party/gtest/scripts/pump.py callback.h.pump |
| 17 | |
| 18 | // Callbacks are callable object containers. They can hold a function pointer |
| 19 | // or a function object and behave like a value type. Internally, data is |
| 20 | // reference-counted, making copies and pass-by-value inexpensive. |
| 21 | // |
| 22 | // Callbacks are typed using template arguments. The format is: |
| 23 | // CallbackN<ReturnType, ParamType1, ..., ParamTypeN> |
| 24 | // where N is the number of arguments supplied to the callable object. |
| 25 | // Callbacks are invoked using operator(), just like a function or a function |
| 26 | // object. Default-constructed callbacks are "empty," and executing an empty |
| 27 | // callback does nothing. A callback can be made empty by assigning it from |
| 28 | // a default-constructed callback. |
| 29 | // |
| 30 | // Callbacks are similar in purpose to std::function (which isn't available on |
| 31 | // all platforms we support) and a lightweight alternative to sigslots. Since |
| 32 | // they effectively hide the type of the object they call, they're useful in |
| 33 | // breaking dependencies between objects that need to interact with one another. |
| 34 | // Notably, they can hold the results of Bind(), std::bind*, etc, without |
| 35 | // needing |
| 36 | // to know the resulting object type of those calls. |
| 37 | // |
| 38 | // Sigslots, on the other hand, provide a fuller feature set, such as multiple |
| 39 | // subscriptions to a signal, optional thread-safety, and lifetime tracking of |
| 40 | // slots. When these features are needed, choose sigslots. |
| 41 | // |
| 42 | // Example: |
| 43 | // int sqr(int x) { return x * x; } |
| 44 | // struct AddK { |
| 45 | // int k; |
| 46 | // int operator()(int x) const { return x + k; } |
| 47 | // } add_k = {5}; |
| 48 | // |
| 49 | // Callback1<int, int> my_callback; |
| 50 | // cout << my_callback.empty() << endl; // true |
| 51 | // |
| 52 | // my_callback = Callback1<int, int>(&sqr); |
| 53 | // cout << my_callback.empty() << endl; // false |
| 54 | // cout << my_callback(3) << endl; // 9 |
| 55 | // |
| 56 | // my_callback = Callback1<int, int>(add_k); |
| 57 | // cout << my_callback(10) << endl; // 15 |
| 58 | // |
| 59 | // my_callback = Callback1<int, int>(); |
| 60 | // cout << my_callback.empty() << endl; // true |
| 61 | |
| 62 | #ifndef WEBRTC_BASE_CALLBACK_H_ |
| 63 | #define WEBRTC_BASE_CALLBACK_H_ |
| 64 | |
| 65 | #include "webrtc/base/logging.h" |
| 66 | #include "webrtc/base/refcount.h" |
| 67 | #include "webrtc/base/scoped_ref_ptr.h" |
| 68 | |
| 69 | namespace rtc { |
| 70 | |
| 71 | template <class R> |
| 72 | class Callback0 { |
| 73 | public: |
| 74 | // Default copy operations are appropriate for this class. |
| 75 | Callback0() {} |
| 76 | template <class T> Callback0(const T& functor) |
| 77 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 78 | R operator()() { |
| 79 | if (empty()) |
| 80 | return R(); |
| 81 | return helper_->Run(); |
| 82 | } |
| 83 | bool empty() const { return !helper_; } |
| 84 | |
| 85 | private: |
| 86 | struct Helper : RefCountInterface { |
| 87 | virtual ~Helper() {} |
| 88 | virtual R Run() = 0; |
| 89 | }; |
| 90 | template <class T> struct HelperImpl : Helper { |
| 91 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 92 | virtual R Run() { |
| 93 | return functor_(); |
| 94 | } |
| 95 | T functor_; |
| 96 | }; |
| 97 | scoped_refptr<Helper> helper_; |
| 98 | }; |
| 99 | |
| 100 | template <class R, |
| 101 | class P1> |
| 102 | class Callback1 { |
| 103 | public: |
| 104 | // Default copy operations are appropriate for this class. |
| 105 | Callback1() {} |
| 106 | template <class T> Callback1(const T& functor) |
| 107 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 108 | R operator()(P1 p1) { |
| 109 | if (empty()) |
| 110 | return R(); |
| 111 | return helper_->Run(p1); |
| 112 | } |
| 113 | bool empty() const { return !helper_; } |
| 114 | |
| 115 | private: |
| 116 | struct Helper : RefCountInterface { |
| 117 | virtual ~Helper() {} |
| 118 | virtual R Run(P1 p1) = 0; |
| 119 | }; |
| 120 | template <class T> struct HelperImpl : Helper { |
| 121 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 122 | virtual R Run(P1 p1) { |
| 123 | return functor_(p1); |
| 124 | } |
| 125 | T functor_; |
| 126 | }; |
| 127 | scoped_refptr<Helper> helper_; |
| 128 | }; |
| 129 | |
| 130 | template <class R, |
| 131 | class P1, |
| 132 | class P2> |
| 133 | class Callback2 { |
| 134 | public: |
| 135 | // Default copy operations are appropriate for this class. |
| 136 | Callback2() {} |
| 137 | template <class T> Callback2(const T& functor) |
| 138 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 139 | R operator()(P1 p1, P2 p2) { |
| 140 | if (empty()) |
| 141 | return R(); |
| 142 | return helper_->Run(p1, p2); |
| 143 | } |
| 144 | bool empty() const { return !helper_; } |
| 145 | |
| 146 | private: |
| 147 | struct Helper : RefCountInterface { |
| 148 | virtual ~Helper() {} |
| 149 | virtual R Run(P1 p1, P2 p2) = 0; |
| 150 | }; |
| 151 | template <class T> struct HelperImpl : Helper { |
| 152 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 153 | virtual R Run(P1 p1, P2 p2) { |
| 154 | return functor_(p1, p2); |
| 155 | } |
| 156 | T functor_; |
| 157 | }; |
| 158 | scoped_refptr<Helper> helper_; |
| 159 | }; |
| 160 | |
| 161 | template <class R, |
| 162 | class P1, |
| 163 | class P2, |
| 164 | class P3> |
| 165 | class Callback3 { |
| 166 | public: |
| 167 | // Default copy operations are appropriate for this class. |
| 168 | Callback3() {} |
| 169 | template <class T> Callback3(const T& functor) |
| 170 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 171 | R operator()(P1 p1, P2 p2, P3 p3) { |
| 172 | if (empty()) |
| 173 | return R(); |
| 174 | return helper_->Run(p1, p2, p3); |
| 175 | } |
| 176 | bool empty() const { return !helper_; } |
| 177 | |
| 178 | private: |
| 179 | struct Helper : RefCountInterface { |
| 180 | virtual ~Helper() {} |
| 181 | virtual R Run(P1 p1, P2 p2, P3 p3) = 0; |
| 182 | }; |
| 183 | template <class T> struct HelperImpl : Helper { |
| 184 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 185 | virtual R Run(P1 p1, P2 p2, P3 p3) { |
| 186 | return functor_(p1, p2, p3); |
| 187 | } |
| 188 | T functor_; |
| 189 | }; |
| 190 | scoped_refptr<Helper> helper_; |
| 191 | }; |
| 192 | |
| 193 | template <class R, |
| 194 | class P1, |
| 195 | class P2, |
| 196 | class P3, |
| 197 | class P4> |
| 198 | class Callback4 { |
| 199 | public: |
| 200 | // Default copy operations are appropriate for this class. |
| 201 | Callback4() {} |
| 202 | template <class T> Callback4(const T& functor) |
| 203 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 204 | R operator()(P1 p1, P2 p2, P3 p3, P4 p4) { |
| 205 | if (empty()) |
| 206 | return R(); |
| 207 | return helper_->Run(p1, p2, p3, p4); |
| 208 | } |
| 209 | bool empty() const { return !helper_; } |
| 210 | |
| 211 | private: |
| 212 | struct Helper : RefCountInterface { |
| 213 | virtual ~Helper() {} |
| 214 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0; |
| 215 | }; |
| 216 | template <class T> struct HelperImpl : Helper { |
| 217 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 218 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) { |
| 219 | return functor_(p1, p2, p3, p4); |
| 220 | } |
| 221 | T functor_; |
| 222 | }; |
| 223 | scoped_refptr<Helper> helper_; |
| 224 | }; |
| 225 | |
| 226 | template <class R, |
| 227 | class P1, |
| 228 | class P2, |
| 229 | class P3, |
| 230 | class P4, |
| 231 | class P5> |
| 232 | class Callback5 { |
| 233 | public: |
| 234 | // Default copy operations are appropriate for this class. |
| 235 | Callback5() {} |
| 236 | template <class T> Callback5(const T& functor) |
| 237 | : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {} |
| 238 | R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { |
| 239 | if (empty()) |
| 240 | return R(); |
| 241 | return helper_->Run(p1, p2, p3, p4, p5); |
| 242 | } |
| 243 | bool empty() const { return !helper_; } |
| 244 | |
| 245 | private: |
| 246 | struct Helper : RefCountInterface { |
| 247 | virtual ~Helper() {} |
| 248 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0; |
| 249 | }; |
| 250 | template <class T> struct HelperImpl : Helper { |
| 251 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 252 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { |
| 253 | return functor_(p1, p2, p3, p4, p5); |
| 254 | } |
| 255 | T functor_; |
| 256 | }; |
| 257 | scoped_refptr<Helper> helper_; |
| 258 | }; |
| 259 | } // namespace rtc |
| 260 | |
| 261 | #endif // WEBRTC_BASE_CALLBACK_H_ |