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