henrike@webrtc.org | f048872 | 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: |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 16 | // ../third_party/googletest/src/googletest/scripts/pump.py callback.h.pump |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 62 | #ifndef RTC_BASE_CALLBACK_H_ |
| 63 | #define RTC_BASE_CALLBACK_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 65 | #include "rtc_base/refcount.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 66 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 67 | #include "rtc_base/scoped_ref_ptr.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | namespace rtc { |
| 70 | |
| 71 | template <class R> |
| 72 | class Callback0 { |
| 73 | public: |
| 74 | // Default copy operations are appropriate for this class. |
| 75 | Callback0() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | template <class T> |
| 77 | Callback0(const T& functor) |
| 78 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | R operator()() { |
| 80 | if (empty()) |
| 81 | return R(); |
| 82 | return helper_->Run(); |
| 83 | } |
| 84 | bool empty() const { return !helper_; } |
| 85 | |
| 86 | private: |
| 87 | struct Helper : RefCountInterface { |
| 88 | virtual ~Helper() {} |
| 89 | virtual R Run() = 0; |
| 90 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 91 | template <class T> |
| 92 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | virtual R Run() { return functor_(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | T functor_; |
| 96 | }; |
| 97 | scoped_refptr<Helper> helper_; |
| 98 | }; |
| 99 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | template <class R, class P1> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 101 | class Callback1 { |
| 102 | public: |
| 103 | // Default copy operations are appropriate for this class. |
| 104 | Callback1() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | template <class T> |
| 106 | Callback1(const T& functor) |
| 107 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | template <class T> |
| 121 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | virtual R Run(P1 p1) { return functor_(p1); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 124 | T functor_; |
| 125 | }; |
| 126 | scoped_refptr<Helper> helper_; |
| 127 | }; |
| 128 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 129 | template <class R, class P1, class P2> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 130 | class Callback2 { |
| 131 | public: |
| 132 | // Default copy operations are appropriate for this class. |
| 133 | Callback2() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 134 | template <class T> |
| 135 | Callback2(const T& functor) |
| 136 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | R operator()(P1 p1, P2 p2) { |
| 138 | if (empty()) |
| 139 | return R(); |
| 140 | return helper_->Run(p1, p2); |
| 141 | } |
| 142 | bool empty() const { return !helper_; } |
| 143 | |
| 144 | private: |
| 145 | struct Helper : RefCountInterface { |
| 146 | virtual ~Helper() {} |
| 147 | virtual R Run(P1 p1, P2 p2) = 0; |
| 148 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 149 | template <class T> |
| 150 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 151 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 152 | virtual R Run(P1 p1, P2 p2) { return functor_(p1, p2); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 153 | T functor_; |
| 154 | }; |
| 155 | scoped_refptr<Helper> helper_; |
| 156 | }; |
| 157 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 158 | template <class R, class P1, class P2, class P3> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 159 | class Callback3 { |
| 160 | public: |
| 161 | // Default copy operations are appropriate for this class. |
| 162 | Callback3() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 163 | template <class T> |
| 164 | Callback3(const T& functor) |
| 165 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 166 | R operator()(P1 p1, P2 p2, P3 p3) { |
| 167 | if (empty()) |
| 168 | return R(); |
| 169 | return helper_->Run(p1, p2, p3); |
| 170 | } |
| 171 | bool empty() const { return !helper_; } |
| 172 | |
| 173 | private: |
| 174 | struct Helper : RefCountInterface { |
| 175 | virtual ~Helper() {} |
| 176 | virtual R Run(P1 p1, P2 p2, P3 p3) = 0; |
| 177 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 178 | template <class T> |
| 179 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 180 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 181 | virtual R Run(P1 p1, P2 p2, P3 p3) { return functor_(p1, p2, p3); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 182 | T functor_; |
| 183 | }; |
| 184 | scoped_refptr<Helper> helper_; |
| 185 | }; |
| 186 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 187 | template <class R, class P1, class P2, class P3, class P4> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 188 | class Callback4 { |
| 189 | public: |
| 190 | // Default copy operations are appropriate for this class. |
| 191 | Callback4() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 192 | template <class T> |
| 193 | Callback4(const T& functor) |
| 194 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 195 | R operator()(P1 p1, P2 p2, P3 p3, P4 p4) { |
| 196 | if (empty()) |
| 197 | return R(); |
| 198 | return helper_->Run(p1, p2, p3, p4); |
| 199 | } |
| 200 | bool empty() const { return !helper_; } |
| 201 | |
| 202 | private: |
| 203 | struct Helper : RefCountInterface { |
| 204 | virtual ~Helper() {} |
| 205 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0; |
| 206 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 207 | template <class T> |
| 208 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 209 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 210 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) { |
| 211 | return functor_(p1, p2, p3, p4); |
| 212 | } |
| 213 | T functor_; |
| 214 | }; |
| 215 | scoped_refptr<Helper> helper_; |
| 216 | }; |
| 217 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 218 | template <class R, class P1, class P2, class P3, class P4, class P5> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 219 | class Callback5 { |
| 220 | public: |
| 221 | // Default copy operations are appropriate for this class. |
| 222 | Callback5() {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 223 | template <class T> |
| 224 | Callback5(const T& functor) |
| 225 | : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 226 | R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { |
| 227 | if (empty()) |
| 228 | return R(); |
| 229 | return helper_->Run(p1, p2, p3, p4, p5); |
| 230 | } |
| 231 | bool empty() const { return !helper_; } |
| 232 | |
| 233 | private: |
| 234 | struct Helper : RefCountInterface { |
| 235 | virtual ~Helper() {} |
| 236 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0; |
| 237 | }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 238 | template <class T> |
| 239 | struct HelperImpl : Helper { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 240 | explicit HelperImpl(const T& functor) : functor_(functor) {} |
| 241 | virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { |
| 242 | return functor_(p1, p2, p3, p4, p5); |
| 243 | } |
| 244 | T functor_; |
| 245 | }; |
| 246 | scoped_refptr<Helper> helper_; |
| 247 | }; |
| 248 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 250 | #endif // RTC_BASE_CALLBACK_H_ |