Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Composer.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 |
5:5c91c61f50b6 | 1 | /* Composer |
Christopher Haster |
5:5c91c61f50b6 | 2 | * Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 3 | */ |
Christopher Haster |
5:5c91c61f50b6 | 4 | #ifndef COMPOSER_H |
Christopher Haster |
5:5c91c61f50b6 | 5 | #define COMPOSER_H |
Christopher Haster |
5:5c91c61f50b6 | 6 | |
Christopher Haster |
5:5c91c61f50b6 | 7 | #include "FuncPtr.h" |
Christopher Haster |
5:5c91c61f50b6 | 8 | |
Christopher Haster |
13:4d8a50d4967e | 9 | namespace funcptr { |
Christopher Haster |
13:4d8a50d4967e | 10 | |
Christopher Haster |
5:5c91c61f50b6 | 11 | |
Christopher Haster |
5:5c91c61f50b6 | 12 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 13 | */ |
Christopher Haster |
5:5c91c61f50b6 | 14 | template <typename F, typename G> |
Christopher Haster |
5:5c91c61f50b6 | 15 | class Composer; |
Christopher Haster |
5:5c91c61f50b6 | 16 | |
Christopher Haster |
5:5c91c61f50b6 | 17 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 18 | */ |
Christopher Haster |
5:5c91c61f50b6 | 19 | template <typename R, typename B0, typename A0, typename A1, typename A2, typename A3> |
Christopher Haster |
5:5c91c61f50b6 | 20 | class Composer<R(B0), B0(A0, A1, A2, A3)> { |
Christopher Haster |
5:5c91c61f50b6 | 21 | public: |
Christopher Haster |
5:5c91c61f50b6 | 22 | /** Create an uncomposed Composer |
Christopher Haster |
5:5c91c61f50b6 | 23 | */ |
Christopher Haster |
5:5c91c61f50b6 | 24 | Composer() {} |
Christopher Haster |
5:5c91c61f50b6 | 25 | |
Christopher Haster |
5:5c91c61f50b6 | 26 | /** Create a Composer, composing two functions |
Christopher Haster |
5:5c91c61f50b6 | 27 | */ |
Christopher Haster |
5:5c91c61f50b6 | 28 | Composer(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1, A2, A3)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 29 | attach(f, g); |
Christopher Haster |
5:5c91c61f50b6 | 30 | } |
Christopher Haster |
5:5c91c61f50b6 | 31 | |
Christopher Haster |
5:5c91c61f50b6 | 32 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 33 | */ |
Christopher Haster |
5:5c91c61f50b6 | 34 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 35 | Composer(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1, A2, A3)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 36 | attach(fobj, fmethod, g); |
Christopher Haster |
5:5c91c61f50b6 | 37 | } |
Christopher Haster |
5:5c91c61f50b6 | 38 | |
Christopher Haster |
5:5c91c61f50b6 | 39 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 40 | */ |
Christopher Haster |
5:5c91c61f50b6 | 41 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 42 | Composer(FuncPtr<B0(A0, A1, A2, A3)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 43 | attach(f, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 44 | } |
Christopher Haster |
5:5c91c61f50b6 | 45 | |
Christopher Haster |
5:5c91c61f50b6 | 46 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 47 | */ |
Christopher Haster |
5:5c91c61f50b6 | 48 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 49 | Composer(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 50 | attach(fobj, fmethod, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 51 | } |
Christopher Haster |
5:5c91c61f50b6 | 52 | |
Christopher Haster |
5:5c91c61f50b6 | 53 | /** Compose two functions |
Christopher Haster |
5:5c91c61f50b6 | 54 | */ |
Christopher Haster |
5:5c91c61f50b6 | 55 | void attach(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1, A2, A3)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 56 | _f.attach(f); |
Christopher Haster |
5:5c91c61f50b6 | 57 | _g.attach(g); |
Christopher Haster |
5:5c91c61f50b6 | 58 | } |
Christopher Haster |
5:5c91c61f50b6 | 59 | |
Christopher Haster |
5:5c91c61f50b6 | 60 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 61 | */ |
Christopher Haster |
5:5c91c61f50b6 | 62 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 63 | void attach(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1, A2, A3)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 64 | attach(FuncPtr<R(B0)>(fobj, fmethod), g); |
Christopher Haster |
5:5c91c61f50b6 | 65 | } |
Christopher Haster |
5:5c91c61f50b6 | 66 | |
Christopher Haster |
5:5c91c61f50b6 | 67 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 68 | */ |
Christopher Haster |
5:5c91c61f50b6 | 69 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 70 | void attach(FuncPtr<R(B0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 71 | attach(f, FuncPtr<B0(A0, A1, A2, A3)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 72 | } |
Christopher Haster |
5:5c91c61f50b6 | 73 | |
Christopher Haster |
5:5c91c61f50b6 | 74 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 75 | */ |
Christopher Haster |
5:5c91c61f50b6 | 76 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 77 | void attach(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 78 | attach(FuncPtr<R(B0)>(fobj, fmethod), |
Christopher Haster |
5:5c91c61f50b6 | 79 | FuncPtr<B0(A0, A1, A2, A3)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 80 | } |
Christopher Haster |
5:5c91c61f50b6 | 81 | |
Christopher Haster |
5:5c91c61f50b6 | 82 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 83 | */ |
Christopher Haster |
5:5c91c61f50b6 | 84 | R call(A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
5:5c91c61f50b6 | 85 | return _f(_g(a0, a1, a2, a3)); |
Christopher Haster |
5:5c91c61f50b6 | 86 | } |
Christopher Haster |
5:5c91c61f50b6 | 87 | |
Christopher Haster |
5:5c91c61f50b6 | 88 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 89 | */ |
Christopher Haster |
5:5c91c61f50b6 | 90 | R operator()(A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
5:5c91c61f50b6 | 91 | return call(a0, a1, a2, a3); |
Christopher Haster |
5:5c91c61f50b6 | 92 | } |
Christopher Haster |
5:5c91c61f50b6 | 93 | |
Christopher Haster |
5:5c91c61f50b6 | 94 | /** Test if functions have been composed |
Christopher Haster |
5:5c91c61f50b6 | 95 | */ |
Christopher Haster |
8:71037a47492d | 96 | operator bool() const { |
Christopher Haster |
5:5c91c61f50b6 | 97 | return _f && _g; |
Christopher Haster |
5:5c91c61f50b6 | 98 | } |
Christopher Haster |
5:5c91c61f50b6 | 99 | |
Christopher Haster |
5:5c91c61f50b6 | 100 | /** Static thunk for passing as C-style function |
Christopher Haster |
5:5c91c61f50b6 | 101 | * @param func Composer to call passed as void pointer |
Christopher Haster |
5:5c91c61f50b6 | 102 | */ |
Christopher Haster |
5:5c91c61f50b6 | 103 | static R thunk(void *func, A0 a0, A1 a1, A2 a2, A3 a3) { |
Christopher Haster |
5:5c91c61f50b6 | 104 | return static_cast<Composer<R(B0), B0(A0, A1, A2, A3)>*>(func) |
Christopher Haster |
5:5c91c61f50b6 | 105 | ->call(a0, a1, a2, a3); |
Christopher Haster |
5:5c91c61f50b6 | 106 | } |
Christopher Haster |
5:5c91c61f50b6 | 107 | |
Christopher Haster |
5:5c91c61f50b6 | 108 | private: |
Christopher Haster |
5:5c91c61f50b6 | 109 | FuncPtr<R(B0)> _f; |
Christopher Haster |
5:5c91c61f50b6 | 110 | FuncPtr<B0(A0, A1, A2, A3)> _g; |
Christopher Haster |
5:5c91c61f50b6 | 111 | }; |
Christopher Haster |
5:5c91c61f50b6 | 112 | |
Christopher Haster |
5:5c91c61f50b6 | 113 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 114 | */ |
Christopher Haster |
5:5c91c61f50b6 | 115 | template <typename R, typename B0, typename A0, typename A1, typename A2> |
Christopher Haster |
5:5c91c61f50b6 | 116 | class Composer<R(B0), B0(A0, A1, A2)> { |
Christopher Haster |
5:5c91c61f50b6 | 117 | public: |
Christopher Haster |
5:5c91c61f50b6 | 118 | /** Create an uncomposed Composer |
Christopher Haster |
5:5c91c61f50b6 | 119 | */ |
Christopher Haster |
5:5c91c61f50b6 | 120 | Composer() {} |
Christopher Haster |
5:5c91c61f50b6 | 121 | |
Christopher Haster |
5:5c91c61f50b6 | 122 | /** Create a Composer, composing two functions |
Christopher Haster |
5:5c91c61f50b6 | 123 | */ |
Christopher Haster |
5:5c91c61f50b6 | 124 | Composer(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1, A2)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 125 | attach(f, g); |
Christopher Haster |
5:5c91c61f50b6 | 126 | } |
Christopher Haster |
5:5c91c61f50b6 | 127 | |
Christopher Haster |
5:5c91c61f50b6 | 128 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 129 | */ |
Christopher Haster |
5:5c91c61f50b6 | 130 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 131 | Composer(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1, A2)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 132 | attach(fobj, fmethod, g); |
Christopher Haster |
5:5c91c61f50b6 | 133 | } |
Christopher Haster |
5:5c91c61f50b6 | 134 | |
Christopher Haster |
5:5c91c61f50b6 | 135 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 136 | */ |
Christopher Haster |
5:5c91c61f50b6 | 137 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 138 | Composer(FuncPtr<B0(A0, A1, A2)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 139 | attach(f, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 140 | } |
Christopher Haster |
5:5c91c61f50b6 | 141 | |
Christopher Haster |
5:5c91c61f50b6 | 142 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 143 | */ |
Christopher Haster |
5:5c91c61f50b6 | 144 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 145 | Composer(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 146 | attach(fobj, fmethod, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 147 | } |
Christopher Haster |
5:5c91c61f50b6 | 148 | |
Christopher Haster |
5:5c91c61f50b6 | 149 | /** Compose two functions |
Christopher Haster |
5:5c91c61f50b6 | 150 | */ |
Christopher Haster |
5:5c91c61f50b6 | 151 | void attach(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1, A2)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 152 | _f.attach(f); |
Christopher Haster |
5:5c91c61f50b6 | 153 | _g.attach(g); |
Christopher Haster |
5:5c91c61f50b6 | 154 | } |
Christopher Haster |
5:5c91c61f50b6 | 155 | |
Christopher Haster |
5:5c91c61f50b6 | 156 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 157 | */ |
Christopher Haster |
5:5c91c61f50b6 | 158 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 159 | void attach(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1, A2)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 160 | attach(FuncPtr<R(B0)>(fobj, fmethod), g); |
Christopher Haster |
5:5c91c61f50b6 | 161 | } |
Christopher Haster |
5:5c91c61f50b6 | 162 | |
Christopher Haster |
5:5c91c61f50b6 | 163 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 164 | */ |
Christopher Haster |
5:5c91c61f50b6 | 165 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 166 | void attach(FuncPtr<R(B0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 167 | attach(f, FuncPtr<B0(A0, A1, A2)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 168 | } |
Christopher Haster |
5:5c91c61f50b6 | 169 | |
Christopher Haster |
5:5c91c61f50b6 | 170 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 171 | */ |
Christopher Haster |
5:5c91c61f50b6 | 172 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 173 | void attach(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 174 | attach(FuncPtr<R(B0)>(fobj, fmethod), |
Christopher Haster |
5:5c91c61f50b6 | 175 | FuncPtr<B0(A0, A1, A2)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 176 | } |
Christopher Haster |
5:5c91c61f50b6 | 177 | |
Christopher Haster |
5:5c91c61f50b6 | 178 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 179 | */ |
Christopher Haster |
5:5c91c61f50b6 | 180 | R call(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
5:5c91c61f50b6 | 181 | return _f(_g(a0, a1, a2)); |
Christopher Haster |
5:5c91c61f50b6 | 182 | } |
Christopher Haster |
5:5c91c61f50b6 | 183 | |
Christopher Haster |
5:5c91c61f50b6 | 184 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 185 | */ |
Christopher Haster |
5:5c91c61f50b6 | 186 | R operator()(A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
5:5c91c61f50b6 | 187 | return call(a0, a1, a2); |
Christopher Haster |
5:5c91c61f50b6 | 188 | } |
Christopher Haster |
5:5c91c61f50b6 | 189 | |
Christopher Haster |
5:5c91c61f50b6 | 190 | /** Test if functions have been composed |
Christopher Haster |
5:5c91c61f50b6 | 191 | */ |
Christopher Haster |
8:71037a47492d | 192 | operator bool() const { |
Christopher Haster |
5:5c91c61f50b6 | 193 | return _f && _g; |
Christopher Haster |
5:5c91c61f50b6 | 194 | } |
Christopher Haster |
5:5c91c61f50b6 | 195 | |
Christopher Haster |
5:5c91c61f50b6 | 196 | /** Static thunk for passing as C-style function |
Christopher Haster |
5:5c91c61f50b6 | 197 | * @param func Composer to call passed as void pointer |
Christopher Haster |
5:5c91c61f50b6 | 198 | */ |
Christopher Haster |
5:5c91c61f50b6 | 199 | static R thunk(void *func, A0 a0, A1 a1, A2 a2) { |
Christopher Haster |
5:5c91c61f50b6 | 200 | return static_cast<Composer<R(B0), B0(A0, A1, A2)>*>(func) |
Christopher Haster |
5:5c91c61f50b6 | 201 | ->call(a0, a1, a2); |
Christopher Haster |
5:5c91c61f50b6 | 202 | } |
Christopher Haster |
5:5c91c61f50b6 | 203 | |
Christopher Haster |
5:5c91c61f50b6 | 204 | private: |
Christopher Haster |
5:5c91c61f50b6 | 205 | FuncPtr<R(B0)> _f; |
Christopher Haster |
5:5c91c61f50b6 | 206 | FuncPtr<B0(A0, A1, A2)> _g; |
Christopher Haster |
5:5c91c61f50b6 | 207 | }; |
Christopher Haster |
5:5c91c61f50b6 | 208 | |
Christopher Haster |
5:5c91c61f50b6 | 209 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 210 | */ |
Christopher Haster |
5:5c91c61f50b6 | 211 | template <typename R, typename B0, typename A0, typename A1> |
Christopher Haster |
5:5c91c61f50b6 | 212 | class Composer<R(B0), B0(A0, A1)> { |
Christopher Haster |
5:5c91c61f50b6 | 213 | public: |
Christopher Haster |
5:5c91c61f50b6 | 214 | /** Create an uncomposed Composer |
Christopher Haster |
5:5c91c61f50b6 | 215 | */ |
Christopher Haster |
5:5c91c61f50b6 | 216 | Composer() {} |
Christopher Haster |
5:5c91c61f50b6 | 217 | |
Christopher Haster |
5:5c91c61f50b6 | 218 | /** Create a Composer, composing two functions |
Christopher Haster |
5:5c91c61f50b6 | 219 | */ |
Christopher Haster |
5:5c91c61f50b6 | 220 | Composer(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 221 | attach(f, g); |
Christopher Haster |
5:5c91c61f50b6 | 222 | } |
Christopher Haster |
5:5c91c61f50b6 | 223 | |
Christopher Haster |
5:5c91c61f50b6 | 224 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 225 | */ |
Christopher Haster |
5:5c91c61f50b6 | 226 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 227 | Composer(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 228 | attach(fobj, fmethod, g); |
Christopher Haster |
5:5c91c61f50b6 | 229 | } |
Christopher Haster |
5:5c91c61f50b6 | 230 | |
Christopher Haster |
5:5c91c61f50b6 | 231 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 232 | */ |
Christopher Haster |
5:5c91c61f50b6 | 233 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 234 | Composer(FuncPtr<B0(A0, A1)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 235 | attach(f, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 236 | } |
Christopher Haster |
5:5c91c61f50b6 | 237 | |
Christopher Haster |
5:5c91c61f50b6 | 238 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 239 | */ |
Christopher Haster |
5:5c91c61f50b6 | 240 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 241 | Composer(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 242 | attach(fobj, fmethod, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 243 | } |
Christopher Haster |
5:5c91c61f50b6 | 244 | |
Christopher Haster |
5:5c91c61f50b6 | 245 | /** Compose two functions |
Christopher Haster |
5:5c91c61f50b6 | 246 | */ |
Christopher Haster |
5:5c91c61f50b6 | 247 | void attach(FuncPtr<R(B0)> f, FuncPtr<B0(A0, A1)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 248 | _f.attach(f); |
Christopher Haster |
5:5c91c61f50b6 | 249 | _g.attach(g); |
Christopher Haster |
5:5c91c61f50b6 | 250 | } |
Christopher Haster |
5:5c91c61f50b6 | 251 | |
Christopher Haster |
5:5c91c61f50b6 | 252 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 253 | */ |
Christopher Haster |
5:5c91c61f50b6 | 254 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 255 | void attach(FT *fobj, FM fmethod, FuncPtr<B0(A0, A1)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 256 | attach(FuncPtr<R(B0)>(fobj, fmethod), g); |
Christopher Haster |
5:5c91c61f50b6 | 257 | } |
Christopher Haster |
5:5c91c61f50b6 | 258 | |
Christopher Haster |
5:5c91c61f50b6 | 259 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 260 | */ |
Christopher Haster |
5:5c91c61f50b6 | 261 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 262 | void attach(FuncPtr<R(B0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 263 | attach(f, FuncPtr<B0(A0, A1)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 264 | } |
Christopher Haster |
5:5c91c61f50b6 | 265 | |
Christopher Haster |
5:5c91c61f50b6 | 266 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 267 | */ |
Christopher Haster |
5:5c91c61f50b6 | 268 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 269 | void attach(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 270 | attach(FuncPtr<R(B0)>(fobj, fmethod), |
Christopher Haster |
5:5c91c61f50b6 | 271 | FuncPtr<B0(A0, A1)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 272 | } |
Christopher Haster |
5:5c91c61f50b6 | 273 | |
Christopher Haster |
5:5c91c61f50b6 | 274 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 275 | */ |
Christopher Haster |
5:5c91c61f50b6 | 276 | R call(A0 a0, A1 a1) { |
Christopher Haster |
5:5c91c61f50b6 | 277 | return _f(_g(a0, a1)); |
Christopher Haster |
5:5c91c61f50b6 | 278 | } |
Christopher Haster |
5:5c91c61f50b6 | 279 | |
Christopher Haster |
5:5c91c61f50b6 | 280 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 281 | */ |
Christopher Haster |
5:5c91c61f50b6 | 282 | R operator()(A0 a0, A1 a1) { |
Christopher Haster |
5:5c91c61f50b6 | 283 | return call(a0, a1); |
Christopher Haster |
5:5c91c61f50b6 | 284 | } |
Christopher Haster |
5:5c91c61f50b6 | 285 | |
Christopher Haster |
5:5c91c61f50b6 | 286 | /** Test if functions have been composed |
Christopher Haster |
5:5c91c61f50b6 | 287 | */ |
Christopher Haster |
8:71037a47492d | 288 | operator bool() const { |
Christopher Haster |
5:5c91c61f50b6 | 289 | return _f && _g; |
Christopher Haster |
5:5c91c61f50b6 | 290 | } |
Christopher Haster |
5:5c91c61f50b6 | 291 | |
Christopher Haster |
5:5c91c61f50b6 | 292 | /** Static thunk for passing as C-style function |
Christopher Haster |
5:5c91c61f50b6 | 293 | * @param func Composer to call passed as void pointer |
Christopher Haster |
5:5c91c61f50b6 | 294 | */ |
Christopher Haster |
5:5c91c61f50b6 | 295 | static R thunk(void *func, A0 a0, A1 a1) { |
Christopher Haster |
5:5c91c61f50b6 | 296 | return static_cast<Composer<R(B0), B0(A0, A1)>*>(func) |
Christopher Haster |
5:5c91c61f50b6 | 297 | ->call(a0, a1); |
Christopher Haster |
5:5c91c61f50b6 | 298 | } |
Christopher Haster |
5:5c91c61f50b6 | 299 | |
Christopher Haster |
5:5c91c61f50b6 | 300 | private: |
Christopher Haster |
5:5c91c61f50b6 | 301 | FuncPtr<R(B0)> _f; |
Christopher Haster |
5:5c91c61f50b6 | 302 | FuncPtr<B0(A0, A1)> _g; |
Christopher Haster |
5:5c91c61f50b6 | 303 | }; |
Christopher Haster |
5:5c91c61f50b6 | 304 | |
Christopher Haster |
5:5c91c61f50b6 | 305 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 306 | */ |
Christopher Haster |
5:5c91c61f50b6 | 307 | template <typename R, typename B0, typename A0> |
Christopher Haster |
5:5c91c61f50b6 | 308 | class Composer<R(B0), B0(A0)> { |
Christopher Haster |
5:5c91c61f50b6 | 309 | public: |
Christopher Haster |
5:5c91c61f50b6 | 310 | /** Create an uncomposed Composer |
Christopher Haster |
5:5c91c61f50b6 | 311 | */ |
Christopher Haster |
5:5c91c61f50b6 | 312 | Composer() {} |
Christopher Haster |
5:5c91c61f50b6 | 313 | |
Christopher Haster |
5:5c91c61f50b6 | 314 | /** Create a Composer, composing two functions |
Christopher Haster |
5:5c91c61f50b6 | 315 | */ |
Christopher Haster |
5:5c91c61f50b6 | 316 | Composer(FuncPtr<R(B0)> f, FuncPtr<B0(A0)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 317 | attach(f, g); |
Christopher Haster |
5:5c91c61f50b6 | 318 | } |
Christopher Haster |
5:5c91c61f50b6 | 319 | |
Christopher Haster |
5:5c91c61f50b6 | 320 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 321 | */ |
Christopher Haster |
5:5c91c61f50b6 | 322 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 323 | Composer(FT *fobj, FM fmethod, FuncPtr<B0(A0)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 324 | attach(fobj, fmethod, g); |
Christopher Haster |
5:5c91c61f50b6 | 325 | } |
Christopher Haster |
5:5c91c61f50b6 | 326 | |
Christopher Haster |
5:5c91c61f50b6 | 327 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 328 | */ |
Christopher Haster |
5:5c91c61f50b6 | 329 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 330 | Composer(FuncPtr<B0(A0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 331 | attach(f, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 332 | } |
Christopher Haster |
5:5c91c61f50b6 | 333 | |
Christopher Haster |
5:5c91c61f50b6 | 334 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 335 | */ |
Christopher Haster |
5:5c91c61f50b6 | 336 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 337 | Composer(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 338 | attach(fobj, fmethod, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 339 | } |
Christopher Haster |
5:5c91c61f50b6 | 340 | |
Christopher Haster |
5:5c91c61f50b6 | 341 | /** Compose two functions |
Christopher Haster |
5:5c91c61f50b6 | 342 | */ |
Christopher Haster |
5:5c91c61f50b6 | 343 | void attach(FuncPtr<R(B0)> f, FuncPtr<B0(A0)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 344 | _f.attach(f); |
Christopher Haster |
5:5c91c61f50b6 | 345 | _g.attach(g); |
Christopher Haster |
5:5c91c61f50b6 | 346 | } |
Christopher Haster |
5:5c91c61f50b6 | 347 | |
Christopher Haster |
5:5c91c61f50b6 | 348 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 349 | */ |
Christopher Haster |
5:5c91c61f50b6 | 350 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 351 | void attach(FT *fobj, FM fmethod, FuncPtr<B0(A0)> g) { |
Christopher Haster |
5:5c91c61f50b6 | 352 | attach(FuncPtr<R(B0)>(fobj, fmethod), g); |
Christopher Haster |
5:5c91c61f50b6 | 353 | } |
Christopher Haster |
5:5c91c61f50b6 | 354 | |
Christopher Haster |
5:5c91c61f50b6 | 355 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 356 | */ |
Christopher Haster |
5:5c91c61f50b6 | 357 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 358 | void attach(FuncPtr<R(B0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 359 | attach(f, FuncPtr<B0(A0)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 360 | } |
Christopher Haster |
5:5c91c61f50b6 | 361 | |
Christopher Haster |
5:5c91c61f50b6 | 362 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 363 | */ |
Christopher Haster |
5:5c91c61f50b6 | 364 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 365 | void attach(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 366 | attach(FuncPtr<R(B0)>(fobj, fmethod), |
Christopher Haster |
5:5c91c61f50b6 | 367 | FuncPtr<B0(A0)>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 368 | } |
Christopher Haster |
5:5c91c61f50b6 | 369 | |
Christopher Haster |
5:5c91c61f50b6 | 370 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 371 | */ |
Christopher Haster |
5:5c91c61f50b6 | 372 | R call(A0 a0) { |
Christopher Haster |
5:5c91c61f50b6 | 373 | return _f(_g(a0)); |
Christopher Haster |
5:5c91c61f50b6 | 374 | } |
Christopher Haster |
5:5c91c61f50b6 | 375 | |
Christopher Haster |
5:5c91c61f50b6 | 376 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 377 | */ |
Christopher Haster |
5:5c91c61f50b6 | 378 | R operator()(A0 a0) { |
Christopher Haster |
5:5c91c61f50b6 | 379 | return call(a0); |
Christopher Haster |
5:5c91c61f50b6 | 380 | } |
Christopher Haster |
5:5c91c61f50b6 | 381 | |
Christopher Haster |
5:5c91c61f50b6 | 382 | /** Test if functions have been composed |
Christopher Haster |
5:5c91c61f50b6 | 383 | */ |
Christopher Haster |
8:71037a47492d | 384 | operator bool() const { |
Christopher Haster |
5:5c91c61f50b6 | 385 | return _f && _g; |
Christopher Haster |
5:5c91c61f50b6 | 386 | } |
Christopher Haster |
5:5c91c61f50b6 | 387 | |
Christopher Haster |
5:5c91c61f50b6 | 388 | /** Static thunk for passing as C-style function |
Christopher Haster |
5:5c91c61f50b6 | 389 | * @param func Composer to call passed as void pointer |
Christopher Haster |
5:5c91c61f50b6 | 390 | */ |
Christopher Haster |
5:5c91c61f50b6 | 391 | static R thunk(void *func, A0 a0) { |
Christopher Haster |
5:5c91c61f50b6 | 392 | return static_cast<Composer<R(B0), B0(A0)>*>(func) |
Christopher Haster |
5:5c91c61f50b6 | 393 | ->call(a0); |
Christopher Haster |
5:5c91c61f50b6 | 394 | } |
Christopher Haster |
5:5c91c61f50b6 | 395 | |
Christopher Haster |
5:5c91c61f50b6 | 396 | private: |
Christopher Haster |
5:5c91c61f50b6 | 397 | FuncPtr<R(B0)> _f; |
Christopher Haster |
5:5c91c61f50b6 | 398 | FuncPtr<B0(A0)> _g; |
Christopher Haster |
5:5c91c61f50b6 | 399 | }; |
Christopher Haster |
5:5c91c61f50b6 | 400 | |
Christopher Haster |
5:5c91c61f50b6 | 401 | /** Static function composition |
Christopher Haster |
5:5c91c61f50b6 | 402 | */ |
Christopher Haster |
5:5c91c61f50b6 | 403 | template <typename R, typename B0> |
Christopher Haster |
5:5c91c61f50b6 | 404 | class Composer<R(B0), B0()> { |
Christopher Haster |
5:5c91c61f50b6 | 405 | public: |
Christopher Haster |
5:5c91c61f50b6 | 406 | /** Create an uncomposed Composer |
Christopher Haster |
5:5c91c61f50b6 | 407 | */ |
Christopher Haster |
5:5c91c61f50b6 | 408 | Composer() {} |
Christopher Haster |
5:5c91c61f50b6 | 409 | |
Christopher Haster |
5:5c91c61f50b6 | 410 | /** Create a Composer, composing two functions |
Christopher Haster |
5:5c91c61f50b6 | 411 | */ |
Christopher Haster |
5:5c91c61f50b6 | 412 | Composer(FuncPtr<R(B0)> f, FuncPtr<B0()> g) { |
Christopher Haster |
5:5c91c61f50b6 | 413 | attach(f, g); |
Christopher Haster |
5:5c91c61f50b6 | 414 | } |
Christopher Haster |
5:5c91c61f50b6 | 415 | |
Christopher Haster |
5:5c91c61f50b6 | 416 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 417 | */ |
Christopher Haster |
5:5c91c61f50b6 | 418 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 419 | Composer(FT *fobj, FM fmethod, FuncPtr<B0()> g) { |
Christopher Haster |
5:5c91c61f50b6 | 420 | attach(fobj, fmethod, g); |
Christopher Haster |
5:5c91c61f50b6 | 421 | } |
Christopher Haster |
5:5c91c61f50b6 | 422 | |
Christopher Haster |
5:5c91c61f50b6 | 423 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 424 | */ |
Christopher Haster |
5:5c91c61f50b6 | 425 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 426 | Composer(FuncPtr<B0()> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 427 | attach(f, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 428 | } |
Christopher Haster |
5:5c91c61f50b6 | 429 | |
Christopher Haster |
5:5c91c61f50b6 | 430 | /** Create a Composer, composing functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 431 | */ |
Christopher Haster |
5:5c91c61f50b6 | 432 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 433 | Composer(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 434 | attach(fobj, fmethod, gobj, gmethod); |
Christopher Haster |
5:5c91c61f50b6 | 435 | } |
Christopher Haster |
5:5c91c61f50b6 | 436 | |
Christopher Haster |
5:5c91c61f50b6 | 437 | /** Compose two functions |
Christopher Haster |
5:5c91c61f50b6 | 438 | */ |
Christopher Haster |
5:5c91c61f50b6 | 439 | void attach(FuncPtr<R(B0)> f, FuncPtr<B0()> g) { |
Christopher Haster |
5:5c91c61f50b6 | 440 | _f.attach(f); |
Christopher Haster |
5:5c91c61f50b6 | 441 | _g.attach(g); |
Christopher Haster |
5:5c91c61f50b6 | 442 | } |
Christopher Haster |
5:5c91c61f50b6 | 443 | |
Christopher Haster |
5:5c91c61f50b6 | 444 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 445 | */ |
Christopher Haster |
5:5c91c61f50b6 | 446 | template <typename FT, typename FM> |
Christopher Haster |
5:5c91c61f50b6 | 447 | void attach(FT *fobj, FM fmethod, FuncPtr<B0()> g) { |
Christopher Haster |
5:5c91c61f50b6 | 448 | attach(FuncPtr<R(B0)>(fobj, fmethod), g); |
Christopher Haster |
5:5c91c61f50b6 | 449 | } |
Christopher Haster |
5:5c91c61f50b6 | 450 | |
Christopher Haster |
5:5c91c61f50b6 | 451 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 452 | */ |
Christopher Haster |
5:5c91c61f50b6 | 453 | template <typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 454 | void attach(FuncPtr<R(B0)> f, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 455 | attach(f, FuncPtr<B0()>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 456 | } |
Christopher Haster |
5:5c91c61f50b6 | 457 | |
Christopher Haster |
5:5c91c61f50b6 | 458 | /** Compose functions and methods |
Christopher Haster |
5:5c91c61f50b6 | 459 | */ |
Christopher Haster |
5:5c91c61f50b6 | 460 | template <typename FT, typename FM, typename GT, typename GM> |
Christopher Haster |
5:5c91c61f50b6 | 461 | void attach(FT *fobj, FM fmethod, GT *gobj, GM gmethod) { |
Christopher Haster |
5:5c91c61f50b6 | 462 | attach(FuncPtr<R(B0)>(fobj, fmethod), |
Christopher Haster |
5:5c91c61f50b6 | 463 | FuncPtr<B0()>(gobj, gmethod)); |
Christopher Haster |
5:5c91c61f50b6 | 464 | } |
Christopher Haster |
5:5c91c61f50b6 | 465 | |
Christopher Haster |
5:5c91c61f50b6 | 466 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 467 | */ |
Christopher Haster |
5:5c91c61f50b6 | 468 | R call() { |
Christopher Haster |
5:5c91c61f50b6 | 469 | return _f(_g()); |
Christopher Haster |
5:5c91c61f50b6 | 470 | } |
Christopher Haster |
5:5c91c61f50b6 | 471 | |
Christopher Haster |
5:5c91c61f50b6 | 472 | /** Call the composed functions |
Christopher Haster |
5:5c91c61f50b6 | 473 | */ |
Christopher Haster |
5:5c91c61f50b6 | 474 | R operator()() { |
Christopher Haster |
5:5c91c61f50b6 | 475 | return call(); |
Christopher Haster |
5:5c91c61f50b6 | 476 | } |
Christopher Haster |
5:5c91c61f50b6 | 477 | |
Christopher Haster |
5:5c91c61f50b6 | 478 | /** Test if functions have been composed |
Christopher Haster |
5:5c91c61f50b6 | 479 | */ |
Christopher Haster |
8:71037a47492d | 480 | operator bool() const { |
Christopher Haster |
5:5c91c61f50b6 | 481 | return _f && _g; |
Christopher Haster |
5:5c91c61f50b6 | 482 | } |
Christopher Haster |
5:5c91c61f50b6 | 483 | |
Christopher Haster |
5:5c91c61f50b6 | 484 | /** Static thunk for passing as C-style function |
Christopher Haster |
5:5c91c61f50b6 | 485 | * @param func Composer to call passed as void pointer |
Christopher Haster |
5:5c91c61f50b6 | 486 | */ |
Christopher Haster |
5:5c91c61f50b6 | 487 | static R thunk(void *func) { |
Christopher Haster |
5:5c91c61f50b6 | 488 | return static_cast<Composer<R(B0), B0()>*>(func) |
Christopher Haster |
5:5c91c61f50b6 | 489 | ->call(); |
Christopher Haster |
5:5c91c61f50b6 | 490 | } |
Christopher Haster |
5:5c91c61f50b6 | 491 | |
Christopher Haster |
5:5c91c61f50b6 | 492 | private: |
Christopher Haster |
5:5c91c61f50b6 | 493 | FuncPtr<R(B0)> _f; |
Christopher Haster |
5:5c91c61f50b6 | 494 | FuncPtr<B0()> _g; |
Christopher Haster |
5:5c91c61f50b6 | 495 | }; |
Christopher Haster |
5:5c91c61f50b6 | 496 | |
Christopher Haster |
5:5c91c61f50b6 | 497 | |
Christopher Haster |
13:4d8a50d4967e | 498 | } |
Christopher Haster |
13:4d8a50d4967e | 499 | |
Christopher Haster |
5:5c91c61f50b6 | 500 | #endif |