Flexible templated function class and related utilities that avoid dynamic memory allocation without limiting functionality
FuncPtr provides a flexible templated function class and related utilities while avoiding dynamic memory allocation and avoiding limited functionality.
FuncPtr provides an intuitive template interface:
FuncPtr<void(const char *)> func(puts); func("hello!\n"); // prints hello!
Several function types are supported by FuncPtr:
// Simple C functions void func(); FuncPtr<void()> fp(func); // C++ Methods struct Thing { void func(); }; Thing thing; FuncPtr<void()> fp(&thing, &Thing::func); // C functions with context void func(Thing *); FuncPtr<void()> fp(&thing, func); // Function objects struct Thing { void operator()(); }; Thing thing; FuncPtr<void()> fp(&thing);
There is no dynamic memory allocation, managing memory is placed entirely on the user. More advanced function manipulation can be accomplished with statically allocated classes:
// Function binding Binder<void(const char *), const char *> bind(putc, "hi!"); bind(); // prints hi! // Function composition Composer<int(const char *), const char *(int)> comp(puts, itoa); comp(10); // prints 10 // Function chaining Chainer<void(const char *), 2> chain; chain.attach(puts); chain.attach(puts); chain("hi!\n"); // prints hi! twice
FuncPtr allows easy support of a large range of function types in C++ APIs with very few lines of code:
class Thing { public: // The following two methods are sufficient for supporting // every supported function type void attach(FuncPtr<void()> func) { _func.attach(func); } template<typename T, typename M> void attach(T *obj, M method) { attach(FuncPtr<void()>(obj, method)); } private: FuncPtr<void()> _func; }
Additionally, FuncPtrs have several utilities for easy integration with C APIs:
// C style callbacks void register_callback(void (*callback)(void *), void *data); register_callback(&FuncPtr<void()>::thunk, &func); // C style functions without context void register_callback(void (*callback)()); Thunker thunk(func); register_callback(thunk); // mbed style callbacks void register_callback(T *obj, void (T::*M)()); register_callback(&func, &FuncPtr<void()>::call);
Binder.h@13:4d8a50d4967e, 2016-04-17 (annotated)
- Committer:
- Christopher Haster
- Date:
- Sun Apr 17 21:15:30 2016 -0500
- Revision:
- 13:4d8a50d4967e
- Parent:
- 8:71037a47492d
- Child:
- 14:79be4e700cc9
Added funcptr namespace
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
3:84b61e1b050c | 1 | /* Binder |
Christopher Haster |
3:84b61e1b050c | 2 | * Static function binding |
Christopher Haster |
3:84b61e1b050c | 3 | */ |
Christopher Haster |
3:84b61e1b050c | 4 | #ifndef BINDER_H |
Christopher Haster |
3:84b61e1b050c | 5 | #define BINDER_H |
Christopher Haster |
3:84b61e1b050c | 6 | |
Christopher Haster |
3:84b61e1b050c | 7 | #include "FuncPtr.h" |
Christopher Haster |
3:84b61e1b050c | 8 | |
Christopher Haster |
13:4d8a50d4967e | 9 | namespace funcptr { |
Christopher Haster |
13:4d8a50d4967e | 10 | |
Christopher Haster |
3:84b61e1b050c | 11 | |
Christopher Haster |
3:84b61e1b050c | 12 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 13 | */ |
Christopher Haster |
3:84b61e1b050c | 14 | template <typename F, typename B0=void, typename B1=void, typename B2=void, typename B3=void> |
Christopher Haster |
3:84b61e1b050c | 15 | class Binder; |
Christopher Haster |
3:84b61e1b050c | 16 | |
Christopher Haster |
3:84b61e1b050c | 17 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 18 | */ |
Christopher Haster |
3:84b61e1b050c | 19 | template <typename R, typename B0, typename B1, typename B2, typename B3> |
Christopher Haster |
3:84b61e1b050c | 20 | class Binder<R(B0, B1, B2, B3), B0, B1, B2, B3> { |
Christopher Haster |
3:84b61e1b050c | 21 | public: |
Christopher Haster |
3:84b61e1b050c | 22 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 23 | */ |
Christopher Haster |
3:84b61e1b050c | 24 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 25 | |
Christopher Haster |
3:84b61e1b050c | 26 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 27 | */ |
Christopher Haster |
3:84b61e1b050c | 28 | Binder(FuncPtr<R(B0, B1, B2, B3)> func, B0 b0, B1 b1, B2 b2, B3 b3) { |
Christopher Haster |
3:84b61e1b050c | 29 | attach(func, b0, b1, b2, b3); |
Christopher Haster |
3:84b61e1b050c | 30 | } |
Christopher Haster |
3:84b61e1b050c | 31 | |
Christopher Haster |
3:84b61e1b050c | 32 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 33 | */ |
Christopher Haster |
3:84b61e1b050c | 34 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 35 | Binder(T *obj, M method, B0 b0, B1 b1, B2 b2, B3 b3) { |
Christopher Haster |
3:84b61e1b050c | 36 | attach(obj, method, b0, b1, b2, b3); |
Christopher Haster |
3:84b61e1b050c | 37 | } |
Christopher Haster |
3:84b61e1b050c | 38 | |
Christopher Haster |
3:84b61e1b050c | 39 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 40 | */ |
Christopher Haster |
3:84b61e1b050c | 41 | void attach(FuncPtr<R(B0, B1, B2, B3)> func, B0 b0, B1 b1, B2 b2, B3 b3) { |
Christopher Haster |
3:84b61e1b050c | 42 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 43 | _b0 = b0; _b1 = b1; _b2 = b2; _b3 = b3; |
Christopher Haster |
3:84b61e1b050c | 44 | } |
Christopher Haster |
3:84b61e1b050c | 45 | |
Christopher Haster |
3:84b61e1b050c | 46 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 47 | */ |
Christopher Haster |
3:84b61e1b050c | 48 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 49 | void attach(T *obj, M method, B0 b0, B1 b1, B2 b2, B3 b3) { |
Christopher Haster |
3:84b61e1b050c | 50 | attach(FuncPtr<R(B0, B1, B2, B3)>(obj, method), b0, b1, b2, b3); |
Christopher Haster |
3:84b61e1b050c | 51 | } |
Christopher Haster |
3:84b61e1b050c | 52 | |
Christopher Haster |
3:84b61e1b050c | 53 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 54 | */ |
Christopher Haster |
3:84b61e1b050c | 55 | R call() { |
Christopher Haster |
3:84b61e1b050c | 56 | return _func(_b0, _b1, _b2, _b3); |
Christopher Haster |
3:84b61e1b050c | 57 | } |
Christopher Haster |
3:84b61e1b050c | 58 | |
Christopher Haster |
3:84b61e1b050c | 59 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 60 | */ |
Christopher Haster |
3:84b61e1b050c | 61 | R operator()() { |
Christopher Haster |
3:84b61e1b050c | 62 | return call(); |
Christopher Haster |
3:84b61e1b050c | 63 | } |
Christopher Haster |
3:84b61e1b050c | 64 | |
Christopher Haster |
3:84b61e1b050c | 65 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 66 | */ |
Christopher Haster |
8:71037a47492d | 67 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 68 | return _func; |
Christopher Haster |
3:84b61e1b050c | 69 | } |
Christopher Haster |
3:84b61e1b050c | 70 | |
Christopher Haster |
3:84b61e1b050c | 71 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 72 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 73 | */ |
Christopher Haster |
3:84b61e1b050c | 74 | static R thunk(void *func) { |
Christopher Haster |
3:84b61e1b050c | 75 | return static_cast<Binder<R(B0, B1, B2, B3), B0, B1, B2, B3>*>(func) |
Christopher Haster |
3:84b61e1b050c | 76 | ->call(); |
Christopher Haster |
3:84b61e1b050c | 77 | } |
Christopher Haster |
3:84b61e1b050c | 78 | |
Christopher Haster |
3:84b61e1b050c | 79 | private: |
Christopher Haster |
3:84b61e1b050c | 80 | FuncPtr<R(B0, B1, B2, B3)> _func; |
Christopher Haster |
3:84b61e1b050c | 81 | B0 _b0; B1 _b1; B2 _b2; B3 _b3; |
Christopher Haster |
3:84b61e1b050c | 82 | }; |
Christopher Haster |
3:84b61e1b050c | 83 | |
Christopher Haster |
3:84b61e1b050c | 84 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 85 | */ |
Christopher Haster |
3:84b61e1b050c | 86 | template <typename R, typename B0, typename B1, typename B2, typename A0> |
Christopher Haster |
3:84b61e1b050c | 87 | class Binder<R(B0, B1, B2, A0), B0, B1, B2> { |
Christopher Haster |
3:84b61e1b050c | 88 | public: |
Christopher Haster |
3:84b61e1b050c | 89 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 90 | */ |
Christopher Haster |
3:84b61e1b050c | 91 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 92 | |
Christopher Haster |
3:84b61e1b050c | 93 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 94 | */ |
Christopher Haster |
3:84b61e1b050c | 95 | Binder(FuncPtr<R(B0, B1, B2, A0)> func, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 96 | attach(func, b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 97 | } |
Christopher Haster |
3:84b61e1b050c | 98 | |
Christopher Haster |
3:84b61e1b050c | 99 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 100 | */ |
Christopher Haster |
3:84b61e1b050c | 101 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 102 | Binder(T *obj, M method, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 103 | attach(obj, method, b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 104 | } |
Christopher Haster |
3:84b61e1b050c | 105 | |
Christopher Haster |
3:84b61e1b050c | 106 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 107 | */ |
Christopher Haster |
3:84b61e1b050c | 108 | void attach(FuncPtr<R(B0, B1, B2, A0)> func, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 109 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 110 | _b0 = b0; _b1 = b1; _b2 = b2; |
Christopher Haster |
3:84b61e1b050c | 111 | } |
Christopher Haster |
3:84b61e1b050c | 112 | |
Christopher Haster |
3:84b61e1b050c | 113 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 114 | */ |
Christopher Haster |
3:84b61e1b050c | 115 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 116 | void attach(T *obj, M method, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 117 | attach(FuncPtr<R(B0, B1, B2, A0)>(obj, method), b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 118 | } |
Christopher Haster |
3:84b61e1b050c | 119 | |
Christopher Haster |
3:84b61e1b050c | 120 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 121 | */ |
Christopher Haster |
3:84b61e1b050c | 122 | R call(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 123 | return _func(_b0, _b1, _b2, a0); |
Christopher Haster |
3:84b61e1b050c | 124 | } |
Christopher Haster |
3:84b61e1b050c | 125 | |
Christopher Haster |
3:84b61e1b050c | 126 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 127 | */ |
Christopher Haster |
3:84b61e1b050c | 128 | R operator()(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 129 | return call(a0); |
Christopher Haster |
3:84b61e1b050c | 130 | } |
Christopher Haster |
3:84b61e1b050c | 131 | |
Christopher Haster |
3:84b61e1b050c | 132 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 133 | */ |
Christopher Haster |
8:71037a47492d | 134 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 135 | return _func; |
Christopher Haster |
3:84b61e1b050c | 136 | } |
Christopher Haster |
3:84b61e1b050c | 137 | |
Christopher Haster |
3:84b61e1b050c | 138 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 139 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 140 | */ |
Christopher Haster |
3:84b61e1b050c | 141 | static R thunk(void *func, A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 142 | return static_cast<Binder<R(B0, B1, B2, A0), B0, B1, B2>*>(func) |
Christopher Haster |
3:84b61e1b050c | 143 | ->call(a0); |
Christopher Haster |
3:84b61e1b050c | 144 | } |
Christopher Haster |
3:84b61e1b050c | 145 | |
Christopher Haster |
3:84b61e1b050c | 146 | private: |
Christopher Haster |
3:84b61e1b050c | 147 | FuncPtr<R(B0, B1, B2, A0)> _func; |
Christopher Haster |
3:84b61e1b050c | 148 | B0 _b0; B1 _b1; B2 _b2; |
Christopher Haster |
3:84b61e1b050c | 149 | }; |
Christopher Haster |
3:84b61e1b050c | 150 | |
Christopher Haster |
3:84b61e1b050c | 151 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 152 | */ |
Christopher Haster |
3:84b61e1b050c | 153 | template <typename R, typename B0, typename B1, typename A0, typename A1> |
Christopher Haster |
3:84b61e1b050c | 154 | class Binder<R(B0, B1, A0, A1), B0, B1> { |
Christopher Haster |
3:84b61e1b050c | 155 | public: |
Christopher Haster |
3:84b61e1b050c | 156 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 157 | */ |
Christopher Haster |
3:84b61e1b050c | 158 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 159 | |
Christopher Haster |
3:84b61e1b050c | 160 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 161 | */ |
Christopher Haster |
3:84b61e1b050c | 162 | Binder(FuncPtr<R(B0, B1, A0, A1)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 163 | attach(func, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 164 | } |
Christopher Haster |
3:84b61e1b050c | 165 | |
Christopher Haster |
3:84b61e1b050c | 166 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 167 | */ |
Christopher Haster |
3:84b61e1b050c | 168 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 169 | Binder(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 170 | attach(obj, method, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 171 | } |
Christopher Haster |
3:84b61e1b050c | 172 | |
Christopher Haster |
3:84b61e1b050c | 173 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 174 | */ |
Christopher Haster |
3:84b61e1b050c | 175 | void attach(FuncPtr<R(B0, B1, A0, A1)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 176 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 177 | _b0 = b0; _b1 = b1; |
Christopher Haster |
3:84b61e1b050c | 178 | } |
Christopher Haster |
3:84b61e1b050c | 179 | |
Christopher Haster |
3:84b61e1b050c | 180 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 181 | */ |
Christopher Haster |
3:84b61e1b050c | 182 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 183 | void attach(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 184 | attach(FuncPtr<R(B0, B1, A0, A1)>(obj, method), b0, b1); |
Christopher Haster |
3:84b61e1b050c | 185 | } |
Christopher Haster |
3:84b61e1b050c | 186 | |
Christopher Haster |
3:84b61e1b050c | 187 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 188 | */ |
Christopher Haster |
3:84b61e1b050c | 189 | R call(A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 190 | return _func(_b0, _b1, a0, a1); |
Christopher Haster |
3:84b61e1b050c | 191 | } |
Christopher Haster |
3:84b61e1b050c | 192 | |
Christopher Haster |
3:84b61e1b050c | 193 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 194 | */ |
Christopher Haster |
3:84b61e1b050c | 195 | R operator()(A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 196 | return call(a0, a1); |
Christopher Haster |
3:84b61e1b050c | 197 | } |
Christopher Haster |
3:84b61e1b050c | 198 | |
Christopher Haster |
3:84b61e1b050c | 199 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 200 | */ |
Christopher Haster |
8:71037a47492d | 201 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 202 | return _func; |
Christopher Haster |
3:84b61e1b050c | 203 | } |
Christopher Haster |
3:84b61e1b050c | 204 | |
Christopher Haster |
3:84b61e1b050c | 205 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 206 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 207 | */ |
Christopher Haster |
3:84b61e1b050c | 208 | static R thunk(void *func, A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 209 | return static_cast<Binder<R(B0, B1, A0, A1), B0, B1>*>(func) |
Christopher Haster |
3:84b61e1b050c | 210 | ->call(a0, a1); |
Christopher Haster |
3:84b61e1b050c | 211 | } |
Christopher Haster |
3:84b61e1b050c | 212 | |
Christopher Haster |
3:84b61e1b050c | 213 | private: |
Christopher Haster |
3:84b61e1b050c | 214 | FuncPtr<R(B0, B1, A0, A1)> _func; |
Christopher Haster |
3:84b61e1b050c | 215 | B0 _b0; B1 _b1; |
Christopher Haster |
3:84b61e1b050c | 216 | }; |
Christopher Haster |
3:84b61e1b050c | 217 | |
Christopher Haster |
3:84b61e1b050c | 218 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 219 | */ |
Christopher Haster |
3:84b61e1b050c | 220 | template <typename R, typename B0, typename A0, typename A1, typename A2> |
Christopher Haster |
3:84b61e1b050c | 221 | class Binder<R(B0, A0, A1, A2), B0> { |
Christopher Haster |
3:84b61e1b050c | 222 | public: |
Christopher Haster |
3:84b61e1b050c | 223 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 224 | */ |
Christopher Haster |
3:84b61e1b050c | 225 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 226 | |
Christopher Haster |
3:84b61e1b050c | 227 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 228 | */ |
Christopher Haster |
3:84b61e1b050c | 229 | Binder(FuncPtr<R(B0, A0, A1, A2)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 230 | attach(func, b0); |
Christopher Haster |
3:84b61e1b050c | 231 | } |
Christopher Haster |
3:84b61e1b050c | 232 | |
Christopher Haster |
3:84b61e1b050c | 233 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 234 | */ |
Christopher Haster |
3:84b61e1b050c | 235 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 236 | Binder(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 237 | attach(obj, method, b0); |
Christopher Haster |
3:84b61e1b050c | 238 | } |
Christopher Haster |
3:84b61e1b050c | 239 | |
Christopher Haster |
3:84b61e1b050c | 240 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 241 | */ |
Christopher Haster |
3:84b61e1b050c | 242 | void attach(FuncPtr<R(B0, A0, A1, A2)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 243 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 244 | _b0 = b0; |
Christopher Haster |
3:84b61e1b050c | 245 | } |
Christopher Haster |
3:84b61e1b050c | 246 | |
Christopher Haster |
3:84b61e1b050c | 247 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 248 | */ |
Christopher Haster |
3:84b61e1b050c | 249 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 250 | void attach(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 251 | attach(FuncPtr<R(B0, A0, A1, A2)>(obj, method), b0); |
Christopher Haster |
3:84b61e1b050c | 252 | } |
Christopher Haster |
3:84b61e1b050c | 253 | |
Christopher Haster |
3:84b61e1b050c | 254 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 255 | */ |
Christopher Haster |
3:84b61e1b050c | 256 | R call(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
3:84b61e1b050c | 257 | return _func(_b0, a0, a1, a2); |
Christopher Haster |
3:84b61e1b050c | 258 | } |
Christopher Haster |
3:84b61e1b050c | 259 | |
Christopher Haster |
3:84b61e1b050c | 260 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 261 | */ |
Christopher Haster |
3:84b61e1b050c | 262 | R operator()(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
3:84b61e1b050c | 263 | return call(a0, a1, a2); |
Christopher Haster |
3:84b61e1b050c | 264 | } |
Christopher Haster |
3:84b61e1b050c | 265 | |
Christopher Haster |
3:84b61e1b050c | 266 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 267 | */ |
Christopher Haster |
8:71037a47492d | 268 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 269 | return _func; |
Christopher Haster |
3:84b61e1b050c | 270 | } |
Christopher Haster |
3:84b61e1b050c | 271 | |
Christopher Haster |
3:84b61e1b050c | 272 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 273 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 274 | */ |
Christopher Haster |
3:84b61e1b050c | 275 | static R thunk(void *func, A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
3:84b61e1b050c | 276 | return static_cast<Binder<R(B0, A0, A1, A2), B0>*>(func) |
Christopher Haster |
3:84b61e1b050c | 277 | ->call(a0, a1, a2); |
Christopher Haster |
3:84b61e1b050c | 278 | } |
Christopher Haster |
3:84b61e1b050c | 279 | |
Christopher Haster |
3:84b61e1b050c | 280 | private: |
Christopher Haster |
3:84b61e1b050c | 281 | FuncPtr<R(B0, A0, A1, A2)> _func; |
Christopher Haster |
3:84b61e1b050c | 282 | B0 _b0; |
Christopher Haster |
3:84b61e1b050c | 283 | }; |
Christopher Haster |
3:84b61e1b050c | 284 | |
Christopher Haster |
3:84b61e1b050c | 285 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 286 | */ |
Christopher Haster |
3:84b61e1b050c | 287 | template <typename R, typename B0, typename B1, typename B2> |
Christopher Haster |
3:84b61e1b050c | 288 | class Binder<R(B0, B1, B2), B0, B1, B2> { |
Christopher Haster |
3:84b61e1b050c | 289 | public: |
Christopher Haster |
3:84b61e1b050c | 290 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 291 | */ |
Christopher Haster |
3:84b61e1b050c | 292 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 293 | |
Christopher Haster |
3:84b61e1b050c | 294 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 295 | */ |
Christopher Haster |
3:84b61e1b050c | 296 | Binder(FuncPtr<R(B0, B1, B2)> func, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 297 | attach(func, b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 298 | } |
Christopher Haster |
3:84b61e1b050c | 299 | |
Christopher Haster |
3:84b61e1b050c | 300 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 301 | */ |
Christopher Haster |
3:84b61e1b050c | 302 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 303 | Binder(T *obj, M method, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 304 | attach(obj, method, b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 305 | } |
Christopher Haster |
3:84b61e1b050c | 306 | |
Christopher Haster |
3:84b61e1b050c | 307 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 308 | */ |
Christopher Haster |
3:84b61e1b050c | 309 | void attach(FuncPtr<R(B0, B1, B2)> func, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 310 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 311 | _b0 = b0; _b1 = b1; _b2 = b2; |
Christopher Haster |
3:84b61e1b050c | 312 | } |
Christopher Haster |
3:84b61e1b050c | 313 | |
Christopher Haster |
3:84b61e1b050c | 314 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 315 | */ |
Christopher Haster |
3:84b61e1b050c | 316 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 317 | void attach(T *obj, M method, B0 b0, B1 b1, B2 b2) { |
Christopher Haster |
3:84b61e1b050c | 318 | attach(FuncPtr<R(B0, B1, B2)>(obj, method), b0, b1, b2); |
Christopher Haster |
3:84b61e1b050c | 319 | } |
Christopher Haster |
3:84b61e1b050c | 320 | |
Christopher Haster |
3:84b61e1b050c | 321 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 322 | */ |
Christopher Haster |
3:84b61e1b050c | 323 | R call() { |
Christopher Haster |
3:84b61e1b050c | 324 | return _func(_b0, _b1, _b2); |
Christopher Haster |
3:84b61e1b050c | 325 | } |
Christopher Haster |
3:84b61e1b050c | 326 | |
Christopher Haster |
3:84b61e1b050c | 327 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 328 | */ |
Christopher Haster |
3:84b61e1b050c | 329 | R operator()() { |
Christopher Haster |
3:84b61e1b050c | 330 | return call(); |
Christopher Haster |
3:84b61e1b050c | 331 | } |
Christopher Haster |
3:84b61e1b050c | 332 | |
Christopher Haster |
3:84b61e1b050c | 333 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 334 | */ |
Christopher Haster |
8:71037a47492d | 335 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 336 | return _func; |
Christopher Haster |
3:84b61e1b050c | 337 | } |
Christopher Haster |
3:84b61e1b050c | 338 | |
Christopher Haster |
3:84b61e1b050c | 339 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 340 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 341 | */ |
Christopher Haster |
3:84b61e1b050c | 342 | static R thunk(void *func) { |
Christopher Haster |
3:84b61e1b050c | 343 | return static_cast<Binder<R(B0, B1, B2), B0, B1, B2>*>(func) |
Christopher Haster |
3:84b61e1b050c | 344 | ->call(); |
Christopher Haster |
3:84b61e1b050c | 345 | } |
Christopher Haster |
3:84b61e1b050c | 346 | |
Christopher Haster |
3:84b61e1b050c | 347 | private: |
Christopher Haster |
3:84b61e1b050c | 348 | FuncPtr<R(B0, B1, B2)> _func; |
Christopher Haster |
3:84b61e1b050c | 349 | B0 _b0; B1 _b1; B2 _b2; |
Christopher Haster |
3:84b61e1b050c | 350 | }; |
Christopher Haster |
3:84b61e1b050c | 351 | |
Christopher Haster |
3:84b61e1b050c | 352 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 353 | */ |
Christopher Haster |
3:84b61e1b050c | 354 | template <typename R, typename B0, typename B1, typename A0> |
Christopher Haster |
3:84b61e1b050c | 355 | class Binder<R(B0, B1, A0), B0, B1> { |
Christopher Haster |
3:84b61e1b050c | 356 | public: |
Christopher Haster |
3:84b61e1b050c | 357 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 358 | */ |
Christopher Haster |
3:84b61e1b050c | 359 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 360 | |
Christopher Haster |
3:84b61e1b050c | 361 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 362 | */ |
Christopher Haster |
3:84b61e1b050c | 363 | Binder(FuncPtr<R(B0, B1, A0)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 364 | attach(func, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 365 | } |
Christopher Haster |
3:84b61e1b050c | 366 | |
Christopher Haster |
3:84b61e1b050c | 367 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 368 | */ |
Christopher Haster |
3:84b61e1b050c | 369 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 370 | Binder(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 371 | attach(obj, method, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 372 | } |
Christopher Haster |
3:84b61e1b050c | 373 | |
Christopher Haster |
3:84b61e1b050c | 374 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 375 | */ |
Christopher Haster |
3:84b61e1b050c | 376 | void attach(FuncPtr<R(B0, B1, A0)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 377 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 378 | _b0 = b0; _b1 = b1; |
Christopher Haster |
3:84b61e1b050c | 379 | } |
Christopher Haster |
3:84b61e1b050c | 380 | |
Christopher Haster |
3:84b61e1b050c | 381 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 382 | */ |
Christopher Haster |
3:84b61e1b050c | 383 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 384 | void attach(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 385 | attach(FuncPtr<R(B0, B1, A0)>(obj, method), b0, b1); |
Christopher Haster |
3:84b61e1b050c | 386 | } |
Christopher Haster |
3:84b61e1b050c | 387 | |
Christopher Haster |
3:84b61e1b050c | 388 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 389 | */ |
Christopher Haster |
3:84b61e1b050c | 390 | R call(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 391 | return _func(_b0, _b1, a0); |
Christopher Haster |
3:84b61e1b050c | 392 | } |
Christopher Haster |
3:84b61e1b050c | 393 | |
Christopher Haster |
3:84b61e1b050c | 394 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 395 | */ |
Christopher Haster |
3:84b61e1b050c | 396 | R operator()(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 397 | return call(a0); |
Christopher Haster |
3:84b61e1b050c | 398 | } |
Christopher Haster |
3:84b61e1b050c | 399 | |
Christopher Haster |
3:84b61e1b050c | 400 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 401 | */ |
Christopher Haster |
8:71037a47492d | 402 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 403 | return _func; |
Christopher Haster |
3:84b61e1b050c | 404 | } |
Christopher Haster |
3:84b61e1b050c | 405 | |
Christopher Haster |
3:84b61e1b050c | 406 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 407 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 408 | */ |
Christopher Haster |
3:84b61e1b050c | 409 | static R thunk(void *func, A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 410 | return static_cast<Binder<R(B0, B1, A0), B0, B1>*>(func) |
Christopher Haster |
3:84b61e1b050c | 411 | ->call(a0); |
Christopher Haster |
3:84b61e1b050c | 412 | } |
Christopher Haster |
3:84b61e1b050c | 413 | |
Christopher Haster |
3:84b61e1b050c | 414 | private: |
Christopher Haster |
3:84b61e1b050c | 415 | FuncPtr<R(B0, B1, A0)> _func; |
Christopher Haster |
3:84b61e1b050c | 416 | B0 _b0; B1 _b1; |
Christopher Haster |
3:84b61e1b050c | 417 | }; |
Christopher Haster |
3:84b61e1b050c | 418 | |
Christopher Haster |
3:84b61e1b050c | 419 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 420 | */ |
Christopher Haster |
3:84b61e1b050c | 421 | template <typename R, typename B0, typename A0, typename A1> |
Christopher Haster |
3:84b61e1b050c | 422 | class Binder<R(B0, A0, A1), B0> { |
Christopher Haster |
3:84b61e1b050c | 423 | public: |
Christopher Haster |
3:84b61e1b050c | 424 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 425 | */ |
Christopher Haster |
3:84b61e1b050c | 426 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 427 | |
Christopher Haster |
3:84b61e1b050c | 428 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 429 | */ |
Christopher Haster |
3:84b61e1b050c | 430 | Binder(FuncPtr<R(B0, A0, A1)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 431 | attach(func, b0); |
Christopher Haster |
3:84b61e1b050c | 432 | } |
Christopher Haster |
3:84b61e1b050c | 433 | |
Christopher Haster |
3:84b61e1b050c | 434 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 435 | */ |
Christopher Haster |
3:84b61e1b050c | 436 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 437 | Binder(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 438 | attach(obj, method, b0); |
Christopher Haster |
3:84b61e1b050c | 439 | } |
Christopher Haster |
3:84b61e1b050c | 440 | |
Christopher Haster |
3:84b61e1b050c | 441 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 442 | */ |
Christopher Haster |
3:84b61e1b050c | 443 | void attach(FuncPtr<R(B0, A0, A1)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 444 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 445 | _b0 = b0; |
Christopher Haster |
3:84b61e1b050c | 446 | } |
Christopher Haster |
3:84b61e1b050c | 447 | |
Christopher Haster |
3:84b61e1b050c | 448 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 449 | */ |
Christopher Haster |
3:84b61e1b050c | 450 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 451 | void attach(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 452 | attach(FuncPtr<R(B0, A0, A1)>(obj, method), b0); |
Christopher Haster |
3:84b61e1b050c | 453 | } |
Christopher Haster |
3:84b61e1b050c | 454 | |
Christopher Haster |
3:84b61e1b050c | 455 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 456 | */ |
Christopher Haster |
3:84b61e1b050c | 457 | R call(A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 458 | return _func(_b0, a0, a1); |
Christopher Haster |
3:84b61e1b050c | 459 | } |
Christopher Haster |
3:84b61e1b050c | 460 | |
Christopher Haster |
3:84b61e1b050c | 461 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 462 | */ |
Christopher Haster |
3:84b61e1b050c | 463 | R operator()(A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 464 | return call(a0, a1); |
Christopher Haster |
3:84b61e1b050c | 465 | } |
Christopher Haster |
3:84b61e1b050c | 466 | |
Christopher Haster |
3:84b61e1b050c | 467 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 468 | */ |
Christopher Haster |
8:71037a47492d | 469 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 470 | return _func; |
Christopher Haster |
3:84b61e1b050c | 471 | } |
Christopher Haster |
3:84b61e1b050c | 472 | |
Christopher Haster |
3:84b61e1b050c | 473 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 474 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 475 | */ |
Christopher Haster |
3:84b61e1b050c | 476 | static R thunk(void *func, A0 a0, A1 a1) { |
Christopher Haster |
3:84b61e1b050c | 477 | return static_cast<Binder<R(B0, A0, A1), B0>*>(func) |
Christopher Haster |
3:84b61e1b050c | 478 | ->call(a0, a1); |
Christopher Haster |
3:84b61e1b050c | 479 | } |
Christopher Haster |
3:84b61e1b050c | 480 | |
Christopher Haster |
3:84b61e1b050c | 481 | private: |
Christopher Haster |
3:84b61e1b050c | 482 | FuncPtr<R(B0, A0, A1)> _func; |
Christopher Haster |
3:84b61e1b050c | 483 | B0 _b0; |
Christopher Haster |
3:84b61e1b050c | 484 | }; |
Christopher Haster |
3:84b61e1b050c | 485 | |
Christopher Haster |
3:84b61e1b050c | 486 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 487 | */ |
Christopher Haster |
3:84b61e1b050c | 488 | template <typename R, typename B0, typename B1> |
Christopher Haster |
3:84b61e1b050c | 489 | class Binder<R(B0, B1), B0, B1> { |
Christopher Haster |
3:84b61e1b050c | 490 | public: |
Christopher Haster |
3:84b61e1b050c | 491 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 492 | */ |
Christopher Haster |
3:84b61e1b050c | 493 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 494 | |
Christopher Haster |
3:84b61e1b050c | 495 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 496 | */ |
Christopher Haster |
3:84b61e1b050c | 497 | Binder(FuncPtr<R(B0, B1)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 498 | attach(func, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 499 | } |
Christopher Haster |
3:84b61e1b050c | 500 | |
Christopher Haster |
3:84b61e1b050c | 501 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 502 | */ |
Christopher Haster |
3:84b61e1b050c | 503 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 504 | Binder(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 505 | attach(obj, method, b0, b1); |
Christopher Haster |
3:84b61e1b050c | 506 | } |
Christopher Haster |
3:84b61e1b050c | 507 | |
Christopher Haster |
3:84b61e1b050c | 508 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 509 | */ |
Christopher Haster |
3:84b61e1b050c | 510 | void attach(FuncPtr<R(B0, B1)> func, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 511 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 512 | _b0 = b0; _b1 = b1; |
Christopher Haster |
3:84b61e1b050c | 513 | } |
Christopher Haster |
3:84b61e1b050c | 514 | |
Christopher Haster |
3:84b61e1b050c | 515 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 516 | */ |
Christopher Haster |
3:84b61e1b050c | 517 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 518 | void attach(T *obj, M method, B0 b0, B1 b1) { |
Christopher Haster |
3:84b61e1b050c | 519 | attach(FuncPtr<R(B0, B1)>(obj, method), b0, b1); |
Christopher Haster |
3:84b61e1b050c | 520 | } |
Christopher Haster |
3:84b61e1b050c | 521 | |
Christopher Haster |
3:84b61e1b050c | 522 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 523 | */ |
Christopher Haster |
3:84b61e1b050c | 524 | R call() { |
Christopher Haster |
3:84b61e1b050c | 525 | return _func(_b0, _b1); |
Christopher Haster |
3:84b61e1b050c | 526 | } |
Christopher Haster |
3:84b61e1b050c | 527 | |
Christopher Haster |
3:84b61e1b050c | 528 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 529 | */ |
Christopher Haster |
3:84b61e1b050c | 530 | R operator()() { |
Christopher Haster |
3:84b61e1b050c | 531 | return call(); |
Christopher Haster |
3:84b61e1b050c | 532 | } |
Christopher Haster |
3:84b61e1b050c | 533 | |
Christopher Haster |
3:84b61e1b050c | 534 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 535 | */ |
Christopher Haster |
8:71037a47492d | 536 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 537 | return _func; |
Christopher Haster |
3:84b61e1b050c | 538 | } |
Christopher Haster |
3:84b61e1b050c | 539 | |
Christopher Haster |
3:84b61e1b050c | 540 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 541 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 542 | */ |
Christopher Haster |
3:84b61e1b050c | 543 | static R thunk(void *func) { |
Christopher Haster |
3:84b61e1b050c | 544 | return static_cast<Binder<R(B0, B1), B0, B1>*>(func) |
Christopher Haster |
3:84b61e1b050c | 545 | ->call(); |
Christopher Haster |
3:84b61e1b050c | 546 | } |
Christopher Haster |
3:84b61e1b050c | 547 | |
Christopher Haster |
3:84b61e1b050c | 548 | private: |
Christopher Haster |
3:84b61e1b050c | 549 | FuncPtr<R(B0, B1)> _func; |
Christopher Haster |
3:84b61e1b050c | 550 | B0 _b0; B1 _b1; |
Christopher Haster |
3:84b61e1b050c | 551 | }; |
Christopher Haster |
3:84b61e1b050c | 552 | |
Christopher Haster |
3:84b61e1b050c | 553 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 554 | */ |
Christopher Haster |
3:84b61e1b050c | 555 | template <typename R, typename B0, typename A0> |
Christopher Haster |
3:84b61e1b050c | 556 | class Binder<R(B0, A0), B0> { |
Christopher Haster |
3:84b61e1b050c | 557 | public: |
Christopher Haster |
3:84b61e1b050c | 558 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 559 | */ |
Christopher Haster |
3:84b61e1b050c | 560 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 561 | |
Christopher Haster |
3:84b61e1b050c | 562 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 563 | */ |
Christopher Haster |
3:84b61e1b050c | 564 | Binder(FuncPtr<R(B0, A0)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 565 | attach(func, b0); |
Christopher Haster |
3:84b61e1b050c | 566 | } |
Christopher Haster |
3:84b61e1b050c | 567 | |
Christopher Haster |
3:84b61e1b050c | 568 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 569 | */ |
Christopher Haster |
3:84b61e1b050c | 570 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 571 | Binder(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 572 | attach(obj, method, b0); |
Christopher Haster |
3:84b61e1b050c | 573 | } |
Christopher Haster |
3:84b61e1b050c | 574 | |
Christopher Haster |
3:84b61e1b050c | 575 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 576 | */ |
Christopher Haster |
3:84b61e1b050c | 577 | void attach(FuncPtr<R(B0, A0)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 578 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 579 | _b0 = b0; |
Christopher Haster |
3:84b61e1b050c | 580 | } |
Christopher Haster |
3:84b61e1b050c | 581 | |
Christopher Haster |
3:84b61e1b050c | 582 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 583 | */ |
Christopher Haster |
3:84b61e1b050c | 584 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 585 | void attach(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 586 | attach(FuncPtr<R(B0, A0)>(obj, method), b0); |
Christopher Haster |
3:84b61e1b050c | 587 | } |
Christopher Haster |
3:84b61e1b050c | 588 | |
Christopher Haster |
3:84b61e1b050c | 589 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 590 | */ |
Christopher Haster |
3:84b61e1b050c | 591 | R call(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 592 | return _func(_b0, a0); |
Christopher Haster |
3:84b61e1b050c | 593 | } |
Christopher Haster |
3:84b61e1b050c | 594 | |
Christopher Haster |
3:84b61e1b050c | 595 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 596 | */ |
Christopher Haster |
3:84b61e1b050c | 597 | R operator()(A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 598 | return call(a0); |
Christopher Haster |
3:84b61e1b050c | 599 | } |
Christopher Haster |
3:84b61e1b050c | 600 | |
Christopher Haster |
3:84b61e1b050c | 601 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 602 | */ |
Christopher Haster |
8:71037a47492d | 603 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 604 | return _func; |
Christopher Haster |
3:84b61e1b050c | 605 | } |
Christopher Haster |
3:84b61e1b050c | 606 | |
Christopher Haster |
3:84b61e1b050c | 607 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 608 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 609 | */ |
Christopher Haster |
3:84b61e1b050c | 610 | static R thunk(void *func, A0 a0) { |
Christopher Haster |
3:84b61e1b050c | 611 | return static_cast<Binder<R(B0, A0), B0>*>(func) |
Christopher Haster |
3:84b61e1b050c | 612 | ->call(a0); |
Christopher Haster |
3:84b61e1b050c | 613 | } |
Christopher Haster |
3:84b61e1b050c | 614 | |
Christopher Haster |
3:84b61e1b050c | 615 | private: |
Christopher Haster |
3:84b61e1b050c | 616 | FuncPtr<R(B0, A0)> _func; |
Christopher Haster |
3:84b61e1b050c | 617 | B0 _b0; |
Christopher Haster |
3:84b61e1b050c | 618 | }; |
Christopher Haster |
3:84b61e1b050c | 619 | |
Christopher Haster |
3:84b61e1b050c | 620 | /** Static function binding |
Christopher Haster |
3:84b61e1b050c | 621 | */ |
Christopher Haster |
3:84b61e1b050c | 622 | template <typename R, typename B0> |
Christopher Haster |
3:84b61e1b050c | 623 | class Binder<R(B0), B0> { |
Christopher Haster |
3:84b61e1b050c | 624 | public: |
Christopher Haster |
3:84b61e1b050c | 625 | /** Create an unbound Binder |
Christopher Haster |
3:84b61e1b050c | 626 | */ |
Christopher Haster |
3:84b61e1b050c | 627 | Binder() {} |
Christopher Haster |
3:84b61e1b050c | 628 | |
Christopher Haster |
3:84b61e1b050c | 629 | /** Create a Binder, binding arguments to a function |
Christopher Haster |
3:84b61e1b050c | 630 | */ |
Christopher Haster |
3:84b61e1b050c | 631 | Binder(FuncPtr<R(B0)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 632 | attach(func, b0); |
Christopher Haster |
3:84b61e1b050c | 633 | } |
Christopher Haster |
3:84b61e1b050c | 634 | |
Christopher Haster |
3:84b61e1b050c | 635 | /** Create a Binder, binding arguments to a method |
Christopher Haster |
3:84b61e1b050c | 636 | */ |
Christopher Haster |
3:84b61e1b050c | 637 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 638 | Binder(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 639 | attach(obj, method, b0); |
Christopher Haster |
3:84b61e1b050c | 640 | } |
Christopher Haster |
3:84b61e1b050c | 641 | |
Christopher Haster |
3:84b61e1b050c | 642 | /** Bind arguments to a function |
Christopher Haster |
3:84b61e1b050c | 643 | */ |
Christopher Haster |
3:84b61e1b050c | 644 | void attach(FuncPtr<R(B0)> func, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 645 | _func.attach(func); |
Christopher Haster |
3:84b61e1b050c | 646 | _b0 = b0; |
Christopher Haster |
3:84b61e1b050c | 647 | } |
Christopher Haster |
3:84b61e1b050c | 648 | |
Christopher Haster |
3:84b61e1b050c | 649 | /** Bind arguments to a method |
Christopher Haster |
3:84b61e1b050c | 650 | */ |
Christopher Haster |
3:84b61e1b050c | 651 | template <typename T, typename M> |
Christopher Haster |
3:84b61e1b050c | 652 | void attach(T *obj, M method, B0 b0) { |
Christopher Haster |
3:84b61e1b050c | 653 | attach(FuncPtr<R(B0)>(obj, method), b0); |
Christopher Haster |
3:84b61e1b050c | 654 | } |
Christopher Haster |
3:84b61e1b050c | 655 | |
Christopher Haster |
3:84b61e1b050c | 656 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 657 | */ |
Christopher Haster |
3:84b61e1b050c | 658 | R call() { |
Christopher Haster |
3:84b61e1b050c | 659 | return _func(_b0); |
Christopher Haster |
3:84b61e1b050c | 660 | } |
Christopher Haster |
3:84b61e1b050c | 661 | |
Christopher Haster |
3:84b61e1b050c | 662 | /** Call the bound function |
Christopher Haster |
3:84b61e1b050c | 663 | */ |
Christopher Haster |
3:84b61e1b050c | 664 | R operator()() { |
Christopher Haster |
3:84b61e1b050c | 665 | return call(); |
Christopher Haster |
3:84b61e1b050c | 666 | } |
Christopher Haster |
3:84b61e1b050c | 667 | |
Christopher Haster |
3:84b61e1b050c | 668 | /** Test if function has been bound |
Christopher Haster |
3:84b61e1b050c | 669 | */ |
Christopher Haster |
8:71037a47492d | 670 | operator bool() const { |
Christopher Haster |
4:627e19790dd9 | 671 | return _func; |
Christopher Haster |
3:84b61e1b050c | 672 | } |
Christopher Haster |
3:84b61e1b050c | 673 | |
Christopher Haster |
3:84b61e1b050c | 674 | /** Static thunk for passing as C-style function |
Christopher Haster |
3:84b61e1b050c | 675 | * @param func Binder to call passed as void pointer |
Christopher Haster |
3:84b61e1b050c | 676 | */ |
Christopher Haster |
3:84b61e1b050c | 677 | static R thunk(void *func) { |
Christopher Haster |
3:84b61e1b050c | 678 | return static_cast<Binder<R(B0), B0>*>(func) |
Christopher Haster |
3:84b61e1b050c | 679 | ->call(); |
Christopher Haster |
3:84b61e1b050c | 680 | } |
Christopher Haster |
3:84b61e1b050c | 681 | |
Christopher Haster |
3:84b61e1b050c | 682 | private: |
Christopher Haster |
3:84b61e1b050c | 683 | FuncPtr<R(B0)> _func; |
Christopher Haster |
3:84b61e1b050c | 684 | B0 _b0; |
Christopher Haster |
3:84b61e1b050c | 685 | }; |
Christopher Haster |
3:84b61e1b050c | 686 | |
Christopher Haster |
3:84b61e1b050c | 687 | |
Christopher Haster |
13:4d8a50d4967e | 688 | } |
Christopher Haster |
13:4d8a50d4967e | 689 | |
Christopher Haster |
3:84b61e1b050c | 690 | #endif |