Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemberFunctionAdaptor.h Source File

MemberFunctionAdaptor.h

00001 /*
00002  * Copyright (c) 2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_
00018 #define EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_
00019 
00020 namespace eq {
00021 namespace detail {
00022 
00023 /**
00024  * Adaptor for member function without argument.
00025  * It wrap member function into a function like object to make it usable like
00026  * a regular function.
00027  * \tparam T the type the class/struct holding the member function.
00028  * \code
00029  * struct Foo {
00030  *  void fn();
00031  * };
00032  *
00033  * Foo foo;
00034  * MemberFunctionAdaptor0<Foo> fn_adapted(&Foo::fn);
00035  *
00036  *  fn_adapted(foo); // work
00037  *  fn_adapted(&foo); // work
00038  * \endcode
00039  */
00040 template<typename T>
00041 struct MemberFunctionAdaptor0 {
00042     /**
00043      * Construct a member function adaptor.
00044      * \param fn The member function to addapt.
00045      */
00046     MemberFunctionAdaptor0(void (T::*fn)()) :
00047         _fn(fn) {
00048     }
00049 
00050     /**
00051      * Call operator for pointer of T
00052      */
00053     void operator()(T* self) const {
00054         (self->*_fn)();
00055     }
00056 
00057     /**
00058      * Call operator for reference of T
00059      */
00060     void operator()(T& self) const {
00061         (self.*_fn)();
00062     }
00063 
00064 private:
00065     void (T::* const _fn)();
00066 };
00067 
00068 
00069 /**
00070  * Adaptor for member function with one argument.
00071  * It wrap member function into a function like object to make it usable like
00072  * a regular function.
00073  * \tparam T the type the class/struct holding the member function.
00074  * \code
00075  * struct Foo {
00076  *  void fn(int);
00077  * };
00078  *
00079  * Foo foo;
00080  * MemberFunctionAdaptor1<Foo> fn_adapted(&Foo::fn);
00081  *
00082  *  fn_adapted(foo, 42); // work
00083  *  fn_adapted(&foo, 42); // work
00084  * \endcode
00085  */
00086 template<typename T, typename Arg0>
00087 struct MemberFunctionAdaptor1 {
00088     /**
00089      * Construct a member function adaptor.
00090      * \param fn The member function to addapt.
00091      */
00092     MemberFunctionAdaptor1(void (T::*fn)(Arg0)) :
00093         _fn(fn) {
00094     }
00095 
00096     /**
00097      * Call operator for pointer of T
00098      */
00099     void operator()(T* self, Arg0 arg0) const {
00100         (self->*_fn)(arg0);
00101     }
00102 
00103     /**
00104      * Call operator for reference of T
00105      */
00106     void operator()(T& self, Arg0 arg0) const {
00107         (self.*_fn)(arg0);
00108     }
00109 
00110 private:
00111     void (T::* const _fn)(Arg0);
00112 };
00113 
00114 
00115 /**
00116  * Adaptor for member function with two arguments.
00117  * It wrap member function into a function like object to make it usable like
00118  * a regular function.
00119  * \tparam T the type the class/struct holding the member function.
00120  * \code
00121  * struct Foo {
00122  *  void fn(int, const char*);
00123  * };
00124  *
00125  * Foo foo;
00126  * MemberFunctionAdaptor2<Foo> fn_adapted(&Foo::fn);
00127  *
00128  *  fn_adapted(foo, 42, "toto"); // work
00129  *  fn_adapted(&foo, 42, "toto"); // work
00130  * \endcode
00131  */
00132 template<typename T, typename Arg0, typename Arg1>
00133 struct MemberFunctionAdaptor2 {
00134     /**
00135      * Construct a member function adaptor.
00136      * \param fn The member function to addapt.
00137      */
00138     MemberFunctionAdaptor2(void (T::*fn)(Arg0, Arg1)) : _fn(fn) { }
00139 
00140     /**
00141      * Call operator for pointer of T
00142      */
00143     void operator()(T* self, Arg0 arg0, Arg1 arg1) const {
00144         (self->*_fn)(arg0, arg1);
00145     }
00146 
00147     /**
00148      * Call operator for reference of T
00149      */
00150     void operator()(T& self, Arg0 arg0, Arg1 arg1) const {
00151         (self.*_fn)(arg0, arg1);
00152     }
00153 
00154 private:
00155     void (T::* const _fn)(Arg0, Arg1);
00156 };
00157 
00158 } // namespace detail
00159 } // namespace eq
00160 
00161 #endif /* EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_ */