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 // ../third_party/googletest/src/googletest/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 RTC_BASE_CALLBACK_H_ 63 #define RTC_BASE_CALLBACK_H_ 64 65 #include "api/scoped_refptr.h" 66 #include "rtc_base/ref_count.h" 67 #include "rtc_base/ref_counted_object.h" 68 69 namespace rtc { 70 71 template <class R> 72 class Callback0 { 73 public: 74 // Default copy operations are appropriate for this class. Callback0()75 Callback0() {} 76 template <class T> Callback0(const T & functor)77 Callback0(const T& functor) 78 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()79 R operator()() { 80 if (empty()) 81 return R(); 82 return helper_->Run(); 83 } empty()84 bool empty() const { return !helper_; } 85 86 private: 87 struct Helper : RefCountInterface { ~HelperHelper88 virtual ~Helper() {} 89 virtual R Run() = 0; 90 }; 91 template <class T> 92 struct HelperImpl : Helper { HelperImplHelperImpl93 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl94 virtual R Run() { return functor_(); } 95 T functor_; 96 }; 97 scoped_refptr<Helper> helper_; 98 }; 99 100 template <class R, class P1> 101 class Callback1 { 102 public: 103 // Default copy operations are appropriate for this class. Callback1()104 Callback1() {} 105 template <class T> Callback1(const T & functor)106 Callback1(const T& functor) 107 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()108 R operator()(P1 p1) { 109 if (empty()) 110 return R(); 111 return helper_->Run(p1); 112 } empty()113 bool empty() const { return !helper_; } 114 115 private: 116 struct Helper : RefCountInterface { ~HelperHelper117 virtual ~Helper() {} 118 virtual R Run(P1 p1) = 0; 119 }; 120 template <class T> 121 struct HelperImpl : Helper { HelperImplHelperImpl122 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl123 virtual R Run(P1 p1) { return functor_(p1); } 124 T functor_; 125 }; 126 scoped_refptr<Helper> helper_; 127 }; 128 129 template <class R, class P1, class P2> 130 class Callback2 { 131 public: 132 // Default copy operations are appropriate for this class. Callback2()133 Callback2() {} 134 template <class T> Callback2(const T & functor)135 Callback2(const T& functor) 136 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()137 R operator()(P1 p1, P2 p2) { 138 if (empty()) 139 return R(); 140 return helper_->Run(p1, p2); 141 } empty()142 bool empty() const { return !helper_; } 143 144 private: 145 struct Helper : RefCountInterface { ~HelperHelper146 virtual ~Helper() {} 147 virtual R Run(P1 p1, P2 p2) = 0; 148 }; 149 template <class T> 150 struct HelperImpl : Helper { HelperImplHelperImpl151 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl152 virtual R Run(P1 p1, P2 p2) { return functor_(p1, p2); } 153 T functor_; 154 }; 155 scoped_refptr<Helper> helper_; 156 }; 157 158 template <class R, class P1, class P2, class P3> 159 class Callback3 { 160 public: 161 // Default copy operations are appropriate for this class. Callback3()162 Callback3() {} 163 template <class T> Callback3(const T & functor)164 Callback3(const T& functor) 165 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()166 R operator()(P1 p1, P2 p2, P3 p3) { 167 if (empty()) 168 return R(); 169 return helper_->Run(p1, p2, p3); 170 } empty()171 bool empty() const { return !helper_; } 172 173 private: 174 struct Helper : RefCountInterface { ~HelperHelper175 virtual ~Helper() {} 176 virtual R Run(P1 p1, P2 p2, P3 p3) = 0; 177 }; 178 template <class T> 179 struct HelperImpl : Helper { HelperImplHelperImpl180 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl181 virtual R Run(P1 p1, P2 p2, P3 p3) { return functor_(p1, p2, p3); } 182 T functor_; 183 }; 184 scoped_refptr<Helper> helper_; 185 }; 186 187 template <class R, class P1, class P2, class P3, class P4> 188 class Callback4 { 189 public: 190 // Default copy operations are appropriate for this class. Callback4()191 Callback4() {} 192 template <class T> Callback4(const T & functor)193 Callback4(const T& functor) 194 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()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 } empty()200 bool empty() const { return !helper_; } 201 202 private: 203 struct Helper : RefCountInterface { ~HelperHelper204 virtual ~Helper() {} 205 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4) = 0; 206 }; 207 template <class T> 208 struct HelperImpl : Helper { HelperImplHelperImpl209 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl210 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 218 template <class R, class P1, class P2, class P3, class P4, class P5> 219 class Callback5 { 220 public: 221 // Default copy operations are appropriate for this class. Callback5()222 Callback5() {} 223 template <class T> Callback5(const T & functor)224 Callback5(const T& functor) 225 : helper_(new RefCountedObject<HelperImpl<T> >(functor)) {} operator()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 } empty()231 bool empty() const { return !helper_; } 232 233 private: 234 struct Helper : RefCountInterface { ~HelperHelper235 virtual ~Helper() {} 236 virtual R Run(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) = 0; 237 }; 238 template <class T> 239 struct HelperImpl : Helper { HelperImplHelperImpl240 explicit HelperImpl(const T& functor) : functor_(functor) {} RunHelperImpl241 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 249 250 #endif // RTC_BASE_CALLBACK_H_ 251