Christopher Haster / mbed-hal

Dependencies:   target-freescale

Fork of mbed-hal by Morpheus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Binder.h Source File

Binder.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_BINDER_H
00017 #define MBED_BINDER_H
00018 
00019 #include "FunctionPointer.h"
00020 
00021 #include <string.h>
00022 #include <stdint.h>
00023 
00024 namespace mbed {
00025 
00026 // Binding with memory management by user
00027 template <typename F, typename B1=void, typename B2=void, typename B3=void, typename B4=void>
00028 class Binder;
00029 
00030 /** Binding with memory ownership on the user
00031  */
00032 template <typename R, typename B1, typename B2, typename B3, typename B4>
00033 class Binder<R(), B1, B2, B3, B4> {
00034 public:
00035     /** Create an empty Binder
00036      */
00037     Binder() {}
00038 
00039     /** Create a Binder, binding arguments to function
00040      */
00041     Binder(FuncPtr<R(B1, B2, B3, B4)> func, B1 b1, B2 b2, B3 b3, B4 b4) {
00042         attach(func, b1, b2, b3, b4);
00043     }
00044 
00045     /** Create a Binder, binding arguments to a method
00046      */
00047     template <typename T, typename M>
00048     Binder(T *object, M method, B1 b1, B2 b2, B3 b3, B4 b4) {
00049         attach(object, method, b1, b2, b3, b4);
00050     }
00051 
00052     /** Bind arguments to a function
00053      */
00054     void attach(FuncPtr<R(B1, B2, B3, B4)> func, B1 b1, B2 b2, B3 b3, B4 b4) {
00055         _func = func;
00056         _b1 = b1;
00057         _b2 = b2;
00058         _b3 = b3;
00059         _b4 = b4;
00060     }
00061 
00062     /** Bind arguments to a method
00063      */
00064     template <typename T, typename M>
00065     void attach(T *object, M method, B1 b1, B2 b2, B3 b3, B4 b4) {
00066         _func = FuncPtr<R(B1, B2, B3, B4)>(object, method);
00067         _b1 = b1;
00068         _b2 = b2;
00069         _b3 = b3;
00070         _b4 = b4;
00071     }
00072 
00073     /** Call the bound function
00074      */
00075     R call() {
00076         return _func(_b1, _b2, _b3, _b4);
00077     }
00078 
00079 #ifdef MBED_OPERATORS
00080     R operator()() {
00081         return _func(_b1, _b2, _b3, _b4);
00082     }
00083 #endif
00084 private:
00085     FuncPtr<R(B1, B2, B3, B4)> _func;
00086     B1 _b1;
00087     B2 _b2;
00088     B3 _b3;
00089     B4 _b4;
00090 };
00091 
00092 
00093 /** Binding with memory ownership on the user
00094  */
00095 template <typename R, typename A1, typename B1, typename B2, typename B3>
00096 class Binder<R(A1), B1, B2, B3> {
00097 public:
00098     /** Create an empty Binder
00099      */
00100     Binder() {}
00101 
00102     /** Create a Binder, binding arguments to function
00103      */
00104     Binder(FuncPtr<R(B1, B2, B3, A1)> func, B1 b1, B2 b2, B3 b3) {
00105         attach(func, b1, b2, b3);
00106     }
00107 
00108     /** Create a Binder, binding arguments to a method
00109      */
00110     template <typename T, typename M>
00111     Binder(T *object, M method, B1 b1, B2 b2, B3 b3) {
00112         attach(object, method, b1, b2, b3);
00113     }
00114 
00115     /** Bind arguments to a function
00116      */
00117     void attach(FuncPtr<R(B1, B2, B3, A1)> func, B1 b1, B2 b2, B3 b3) {
00118         _func = func;
00119         _b1 = b1;
00120         _b2 = b2;
00121         _b3 = b3;
00122     }
00123 
00124     /** Bind arguments to a method
00125      */
00126     template <typename T, typename M>
00127     void attach(T *object, M method, B1 b1, B2 b2, B3 b3) {
00128         _func = FuncPtr<R(B1, B2, B3, A1)>(object, method);
00129         _b1 = b1;
00130         _b2 = b2;
00131         _b3 = b3;
00132     }
00133 
00134     /** Call the bound function
00135      */
00136     R call(A1 a1) {
00137         return _func(_b1, _b2, _b3, a1);
00138     }
00139 
00140 #ifdef MBED_OPERATORS
00141     R operator()(A1 a1) {
00142         return _func(_b1, _b2, _b3, a1);
00143     }
00144 #endif
00145 private:
00146     FuncPtr<R(B1, B2, B3, A1)> _func;
00147     B1 _b1;
00148     B2 _b2;
00149     B3 _b3;
00150 };
00151 
00152 /** Binding with memory ownership on the user
00153  */
00154 template <typename R, typename B1, typename B2, typename B3>
00155 class Binder<R(), B1, B2, B3> {
00156 public:
00157     /** Create an empty Binder
00158      */
00159     Binder() {}
00160 
00161     /** Create a Binder, binding arguments to function
00162      */
00163     Binder(FuncPtr<R(B1, B2, B3)> func, B1 b1, B2 b2, B3 b3) {
00164         attach(func, b1, b2, b3);
00165     }
00166 
00167     /** Create a Binder, binding arguments to a method
00168      */
00169     template <typename T, typename M>
00170     Binder(T *object, M method, B1 b1, B2 b2, B3 b3) {
00171         attach(object, method, b1, b2, b3);
00172     }
00173 
00174     /** Bind arguments to a function
00175      */
00176     void attach(FuncPtr<R(B1, B2, B3)> func, B1 b1, B2 b2, B3 b3) {
00177         _func = func;
00178         _b1 = b1;
00179         _b2 = b2;
00180         _b3 = b3;
00181     }
00182 
00183     /** Bind arguments to a method
00184      */
00185     template <typename T, typename M>
00186     void attach(T *object, M method, B1 b1, B2 b2, B3 b3) {
00187         _func = FuncPtr<R(B1, B2, B3)>(object, method);
00188         _b1 = b1;
00189         _b2 = b2;
00190         _b3 = b3;
00191     }
00192 
00193     /** Call the bound function
00194      */
00195     R call() {
00196         return _func(_b1, _b2, _b3);
00197     }
00198 
00199 #ifdef MBED_OPERATORS
00200     R operator()() {
00201         return _func(_b1, _b2, _b3);
00202     }
00203 #endif
00204 private:
00205     FuncPtr<R(B1, B2, B3)> _func;
00206     B1 _b1;
00207     B2 _b2;
00208     B3 _b3;
00209 };
00210 
00211 /** Binding with memory ownership on the user
00212  */
00213 template <typename R, typename A1, typename A2, typename B1, typename B2>
00214 class Binder<R(A1, A2), B1, B2> {
00215 public:
00216     /** Create an empty Binder
00217      */
00218     Binder() {}
00219 
00220     /** Create a Binder, binding arguments to function
00221      */
00222     Binder(FuncPtr<R(B1, B2, A1, A2)> func, B1 b1, B2 b2) {
00223         attach(func, b1, b2);
00224     }
00225 
00226     /** Create a Binder, binding arguments to a method
00227      */
00228     template <typename T, typename M>
00229     Binder(T *object, M method, B1 b1, B2 b2) {
00230         attach(object, method, b1, b2);
00231     }
00232 
00233     /** Bind arguments to a function
00234      */
00235     void attach(FuncPtr<R(B1, B2, A1, A2)> func, B1 b1, B2 b2) {
00236         _func = func;
00237         _b1 = b1;
00238         _b2 = b2;
00239     }
00240 
00241     /** Bind arguments to a method
00242      */
00243     template <typename T, typename M>
00244     void attach(T *object, M method, B1 b1, B2 b2) {
00245         _func = FuncPtr<R(B1, B2, A1, A2)>(object, method);
00246         _b1 = b1;
00247         _b2 = b2;
00248     }
00249 
00250     /** Call the bound function
00251      */
00252     R call(A1 a1, A2 a2) {
00253         return _func(_b1, _b2, a1, a2);
00254     }
00255 
00256 #ifdef MBED_OPERATORS
00257     R operator()(A1 a1, A2 a2) {
00258         return _func(_b1, _b2, a1, a2);
00259     }
00260 #endif
00261 private:
00262     FuncPtr<R(B1, B2, A1, A2)> _func;
00263     B1 _b1;
00264     B2 _b2;
00265 };
00266 
00267 /** Binding with memory ownership on the user
00268  */
00269 template <typename R, typename A1, typename B1, typename B2>
00270 class Binder<R(A1), B1, B2> {
00271 public:
00272     /** Create an empty Binder
00273      */
00274     Binder() {}
00275 
00276     /** Create a Binder, binding arguments to function
00277      */
00278     Binder(FuncPtr<R(B1, B2, A1)> func, B1 b1, B2 b2) {
00279         attach(func, b1, b2);
00280     }
00281 
00282     /** Create a Binder, binding arguments to a method
00283      */
00284     template <typename T, typename M>
00285     Binder(T *object, M method, B1 b1, B2 b2) {
00286         attach(object, method, b1, b2);
00287     }
00288 
00289     /** Bind arguments to a function
00290      */
00291     void attach(FuncPtr<R(B1, B2, A1)> func, B1 b1, B2 b2) {
00292         _func = func;
00293         _b1 = b1;
00294         _b2 = b2;
00295     }
00296 
00297     /** Bind arguments to a method
00298      */
00299     template <typename T, typename M>
00300     void attach(T *object, M method, B1 b1, B2 b2) {
00301         _func = FuncPtr<R(B1, B2, A1)>(object, method);
00302         _b1 = b1;
00303         _b2 = b2;
00304     }
00305 
00306     /** Call the bound function
00307      */
00308     R call(A1 a1) {
00309         return _func(_b1, _b2, a1);
00310     }
00311 
00312 #ifdef MBED_OPERATORS
00313     R operator()(A1 a1) {
00314         return _func(_b1, _b2, a1);
00315     }
00316 #endif
00317 private:
00318     FuncPtr<R(B1, B2, A1)> _func;
00319     B1 _b1;
00320     B2 _b2;
00321 };
00322 
00323 /** Binding with memory ownership on the user
00324  */
00325 template <typename R, typename B1, typename B2>
00326 class Binder<R(), B1, B2> {
00327 public:
00328     /** Create an empty Binder
00329      */
00330     Binder() {}
00331 
00332     /** Create a Binder, binding arguments to function
00333      */
00334     Binder(FuncPtr<R(B1, B2)> func, B1 b1, B2 b2) {
00335         attach(func, b1, b2);
00336     }
00337 
00338     /** Create a Binder, binding arguments to a method
00339      */
00340     template <typename T, typename M>
00341     Binder(T *object, M method, B1 b1, B2 b2) {
00342         attach(object, method, b1, b2);
00343     }
00344 
00345     /** Bind arguments to a function
00346      */
00347     void attach(FuncPtr<R(B1, B2)> func, B1 b1, B2 b2) {
00348         _func = func;
00349         _b1 = b1;
00350         _b2 = b2;
00351     }
00352 
00353     /** Bind arguments to a method
00354      */
00355     template <typename T, typename M>
00356     void attach(T *object, M method, B1 b1, B2 b2) {
00357         _func = FuncPtr<R(B1, B2)>(object, method);
00358         _b1 = b1;
00359         _b2 = b2;
00360     }
00361 
00362     /** Call the bound function
00363      */
00364     R call() {
00365         return _func(_b1, _b2);
00366     }
00367 
00368 #ifdef MBED_OPERATORS
00369     R operator()() {
00370         return _func(_b1, _b2);
00371     }
00372 #endif
00373 private:
00374     FuncPtr<R(B1, B2)> _func;
00375     B1 _b1;
00376     B2 _b2;
00377 };
00378 
00379 
00380 /** Binding with memory ownership on the user
00381  */
00382 template <typename R, typename A1, typename A2, typename A3, typename B1>
00383 class Binder<R(A1, A2, A3), B1> {
00384 public:
00385     /** Create an empty Binder
00386      */
00387     Binder() {}
00388 
00389     /** Create a Binder, binding arguments to function
00390      */
00391     Binder(FuncPtr<R(B1, A1, A2, A3)> func, B1 b1) {
00392         attach(func, b1);
00393     }
00394 
00395     /** Create a Binder, binding arguments to a method
00396      */
00397     template <typename T, typename M>
00398     Binder(T *object, M method, B1 b1) {
00399         attach(object, method, b1);
00400     }
00401 
00402     /** Bind arguments to a function
00403      */
00404     void attach(FuncPtr<R(B1, A1, A2, A3)> func, B1 b1) {
00405         _func = func;
00406         _b1 = b1;
00407     }
00408 
00409     /** Bind arguments to a method
00410      */
00411     template <typename T, typename M>
00412     void attach(T *object, M method, B1 b1) {
00413         _func = FuncPtr<R(B1, A1, A2, A3)>(object, method);
00414         _b1 = b1;
00415     }
00416 
00417     /** Call the bound function
00418      */
00419     R call(A1 a1, A2 a2, A3 a3) {
00420         return _func(_b1, a1, a2, a3);
00421     }
00422 
00423 #ifdef MBED_OPERATORS
00424     R operator()(A1 a1, A2 a2, A3 a3) {
00425         return _func(_b1, a1, a2, a3);
00426     }
00427 #endif
00428 private:
00429     FuncPtr<R(B1, A1, A2, A3)> _func;
00430     B1 _b1;
00431 };
00432 
00433 /** Binding with memory ownership on the user
00434  */
00435 template <typename R, typename A1, typename A2, typename B1>
00436 class Binder<R(A1, A2), B1> {
00437 public:
00438     /** Create an empty Binder
00439      */
00440     Binder() {}
00441 
00442     /** Create a Binder, binding arguments to function
00443      */
00444     Binder(FuncPtr<R(B1, A1, A2)> func, B1 b1) {
00445         attach(func, b1);
00446     }
00447 
00448     /** Create a Binder, binding arguments to a method
00449      */
00450     template <typename T, typename M>
00451     Binder(T *object, M method, B1 b1) {
00452         attach(object, method, b1);
00453     }
00454 
00455     /** Bind arguments to a function
00456      */
00457     void attach(FuncPtr<R(B1, A1, A2)> func, B1 b1) {
00458         _func = func;
00459         _b1 = b1;
00460     }
00461 
00462     /** Bind arguments to a method
00463      */
00464     template <typename T, typename M>
00465     void attach(T *object, M method, B1 b1) {
00466         _func = FuncPtr<R(B1, A1, A2)>(object, method);
00467         _b1 = b1;
00468     }
00469 
00470     /** Call the bound function
00471      */
00472     R call(A1 a1, A2 a2) {
00473         return _func(_b1, a1, a2);
00474     }
00475 
00476 #ifdef MBED_OPERATORS
00477     R operator()(A1 a1, A2 a2) {
00478         return _func(_b1, a1, a2);
00479     }
00480 #endif
00481 private:
00482     FuncPtr<R(B1, A1, A2)> _func;
00483     B1 _b1;
00484 };
00485 
00486 /** Binding with memory ownership on the user
00487  */
00488 template <typename R, typename A1, typename B1>
00489 class Binder<R(A1), B1> {
00490 public:
00491     /** Create an empty Binder
00492      */
00493     Binder() {}
00494 
00495     /** Create a Binder, binding arguments to function
00496      */
00497     Binder(FuncPtr<R(B1, A1)> func, B1 b1) {
00498         attach(func, b1);
00499     }
00500 
00501     /** Create a Binder, binding arguments to a method
00502      */
00503     template <typename T, typename M>
00504     Binder(T *object, M method, B1 b1) {
00505         attach(object, method, b1);
00506     }
00507 
00508     /** Bind arguments to a function
00509      */
00510     void attach(FuncPtr<R(B1, A1)> func, B1 b1) {
00511         _func = func;
00512         _b1 = b1;
00513     }
00514 
00515     /** Bind arguments to a method
00516      */
00517     template <typename T, typename M>
00518     void attach(T *object, M method, B1 b1) {
00519         _func = FuncPtr<R(B1, A1)>(object, method);
00520         _b1 = b1;
00521     }
00522 
00523     /** Call the bound function
00524      */
00525     R call(A1 a1) {
00526         return _func(_b1, a1);
00527     }
00528 
00529 #ifdef MBED_OPERATORS
00530     R operator()(A1 a1) {
00531         return _func(_b1, a1);
00532     }
00533 #endif
00534 private:
00535     FuncPtr<R(B1, A1)> _func;
00536     B1 _b1;
00537 };
00538 
00539 /** Binding with memory ownership on the user
00540  */
00541 template <typename R, typename B1>
00542 class Binder<R(), B1> {
00543 public:
00544     /** Create an empty Binder
00545      */
00546     Binder() {}
00547 
00548     /** Create a Binder, binding arguments to function
00549      */
00550     Binder(FuncPtr<R(B1)> func, B1 b1) {
00551         attach(func, b1);
00552     }
00553 
00554     /** Create a Binder, binding arguments to a method
00555      */
00556     template <typename T, typename M>
00557     Binder(T *object, M method, B1 b1) {
00558         attach(object, method, b1);
00559     }
00560 
00561     /** Bind arguments to a function
00562      */
00563     void attach(FuncPtr<R(B1)> func, B1 b1) {
00564         _func = func;
00565         _b1 = b1;
00566     }
00567 
00568     /** Bind arguments to a method
00569      */
00570     template <typename T, typename M>
00571     void attach(T *object, M method, B1 b1) {
00572         _func = FuncPtr<R(B1)>(object, method);
00573         _b1 = b1;
00574     }
00575 
00576     /** Call the bound function
00577      */
00578     R call() {
00579         return _func(_b1);
00580     }
00581 
00582 #ifdef MBED_OPERATORS
00583     R operator()() {
00584         return _func(_b1);
00585     }
00586 #endif
00587 private:
00588     FuncPtr<R(B1)> _func;
00589     B1 _b1;
00590 };
00591 
00592 } // namespace mbed
00593 
00594 #endif