Roy Want / Mbed OS beaconCompileReadyFork
Committer:
roywant
Date:
Mon Sep 19 00:59:11 2016 +0000
Revision:
0:ed0152b5c495
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roywant 0:ed0152b5c495 1 /*
roywant 0:ed0152b5c495 2 * Copyright (c) 2016, ARM Limited, All Rights Reserved
roywant 0:ed0152b5c495 3 * SPDX-License-Identifier: Apache-2.0
roywant 0:ed0152b5c495 4 *
roywant 0:ed0152b5c495 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
roywant 0:ed0152b5c495 6 * not use this file except in compliance with the License.
roywant 0:ed0152b5c495 7 * You may obtain a copy of the License at
roywant 0:ed0152b5c495 8 *
roywant 0:ed0152b5c495 9 * http://www.apache.org/licenses/LICENSE-2.0
roywant 0:ed0152b5c495 10 *
roywant 0:ed0152b5c495 11 * Unless required by applicable law or agreed to in writing, software
roywant 0:ed0152b5c495 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
roywant 0:ed0152b5c495 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roywant 0:ed0152b5c495 14 * See the License for the specific language governing permissions and
roywant 0:ed0152b5c495 15 * limitations under the License.
roywant 0:ed0152b5c495 16 */
roywant 0:ed0152b5c495 17 #ifndef EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_
roywant 0:ed0152b5c495 18 #define EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_
roywant 0:ed0152b5c495 19
roywant 0:ed0152b5c495 20 namespace eq {
roywant 0:ed0152b5c495 21 namespace detail {
roywant 0:ed0152b5c495 22
roywant 0:ed0152b5c495 23 /**
roywant 0:ed0152b5c495 24 * Adaptor for member function without argument.
roywant 0:ed0152b5c495 25 * It wrap member function into a function like object to make it usable like
roywant 0:ed0152b5c495 26 * a regular function.
roywant 0:ed0152b5c495 27 * \tparam T the type the class/struct holding the member function.
roywant 0:ed0152b5c495 28 * \code
roywant 0:ed0152b5c495 29 * struct Foo {
roywant 0:ed0152b5c495 30 * void fn();
roywant 0:ed0152b5c495 31 * };
roywant 0:ed0152b5c495 32 *
roywant 0:ed0152b5c495 33 * Foo foo;
roywant 0:ed0152b5c495 34 * MemberFunctionAdaptor0<Foo> fn_adapted(&Foo::fn);
roywant 0:ed0152b5c495 35 *
roywant 0:ed0152b5c495 36 * fn_adapted(foo); // work
roywant 0:ed0152b5c495 37 * fn_adapted(&foo); // work
roywant 0:ed0152b5c495 38 * \endcode
roywant 0:ed0152b5c495 39 */
roywant 0:ed0152b5c495 40 template<typename T>
roywant 0:ed0152b5c495 41 struct MemberFunctionAdaptor0 {
roywant 0:ed0152b5c495 42 /**
roywant 0:ed0152b5c495 43 * Construct a member function adaptor.
roywant 0:ed0152b5c495 44 * \param fn The member function to addapt.
roywant 0:ed0152b5c495 45 */
roywant 0:ed0152b5c495 46 MemberFunctionAdaptor0(void (T::*fn)()) :
roywant 0:ed0152b5c495 47 _fn(fn) {
roywant 0:ed0152b5c495 48 }
roywant 0:ed0152b5c495 49
roywant 0:ed0152b5c495 50 /**
roywant 0:ed0152b5c495 51 * Call operator for pointer of T
roywant 0:ed0152b5c495 52 */
roywant 0:ed0152b5c495 53 void operator()(T* self) const {
roywant 0:ed0152b5c495 54 (self->*_fn)();
roywant 0:ed0152b5c495 55 }
roywant 0:ed0152b5c495 56
roywant 0:ed0152b5c495 57 /**
roywant 0:ed0152b5c495 58 * Call operator for reference of T
roywant 0:ed0152b5c495 59 */
roywant 0:ed0152b5c495 60 void operator()(T& self) const {
roywant 0:ed0152b5c495 61 (self.*_fn)();
roywant 0:ed0152b5c495 62 }
roywant 0:ed0152b5c495 63
roywant 0:ed0152b5c495 64 private:
roywant 0:ed0152b5c495 65 void (T::* const _fn)();
roywant 0:ed0152b5c495 66 };
roywant 0:ed0152b5c495 67
roywant 0:ed0152b5c495 68
roywant 0:ed0152b5c495 69 /**
roywant 0:ed0152b5c495 70 * Adaptor for member function with one argument.
roywant 0:ed0152b5c495 71 * It wrap member function into a function like object to make it usable like
roywant 0:ed0152b5c495 72 * a regular function.
roywant 0:ed0152b5c495 73 * \tparam T the type the class/struct holding the member function.
roywant 0:ed0152b5c495 74 * \code
roywant 0:ed0152b5c495 75 * struct Foo {
roywant 0:ed0152b5c495 76 * void fn(int);
roywant 0:ed0152b5c495 77 * };
roywant 0:ed0152b5c495 78 *
roywant 0:ed0152b5c495 79 * Foo foo;
roywant 0:ed0152b5c495 80 * MemberFunctionAdaptor1<Foo> fn_adapted(&Foo::fn);
roywant 0:ed0152b5c495 81 *
roywant 0:ed0152b5c495 82 * fn_adapted(foo, 42); // work
roywant 0:ed0152b5c495 83 * fn_adapted(&foo, 42); // work
roywant 0:ed0152b5c495 84 * \endcode
roywant 0:ed0152b5c495 85 */
roywant 0:ed0152b5c495 86 template<typename T, typename Arg0>
roywant 0:ed0152b5c495 87 struct MemberFunctionAdaptor1 {
roywant 0:ed0152b5c495 88 /**
roywant 0:ed0152b5c495 89 * Construct a member function adaptor.
roywant 0:ed0152b5c495 90 * \param fn The member function to addapt.
roywant 0:ed0152b5c495 91 */
roywant 0:ed0152b5c495 92 MemberFunctionAdaptor1(void (T::*fn)(Arg0)) :
roywant 0:ed0152b5c495 93 _fn(fn) {
roywant 0:ed0152b5c495 94 }
roywant 0:ed0152b5c495 95
roywant 0:ed0152b5c495 96 /**
roywant 0:ed0152b5c495 97 * Call operator for pointer of T
roywant 0:ed0152b5c495 98 */
roywant 0:ed0152b5c495 99 void operator()(T* self, Arg0 arg0) const {
roywant 0:ed0152b5c495 100 (self->*_fn)(arg0);
roywant 0:ed0152b5c495 101 }
roywant 0:ed0152b5c495 102
roywant 0:ed0152b5c495 103 /**
roywant 0:ed0152b5c495 104 * Call operator for reference of T
roywant 0:ed0152b5c495 105 */
roywant 0:ed0152b5c495 106 void operator()(T& self, Arg0 arg0) const {
roywant 0:ed0152b5c495 107 (self.*_fn)(arg0);
roywant 0:ed0152b5c495 108 }
roywant 0:ed0152b5c495 109
roywant 0:ed0152b5c495 110 private:
roywant 0:ed0152b5c495 111 void (T::* const _fn)(Arg0);
roywant 0:ed0152b5c495 112 };
roywant 0:ed0152b5c495 113
roywant 0:ed0152b5c495 114
roywant 0:ed0152b5c495 115 /**
roywant 0:ed0152b5c495 116 * Adaptor for member function with two arguments.
roywant 0:ed0152b5c495 117 * It wrap member function into a function like object to make it usable like
roywant 0:ed0152b5c495 118 * a regular function.
roywant 0:ed0152b5c495 119 * \tparam T the type the class/struct holding the member function.
roywant 0:ed0152b5c495 120 * \code
roywant 0:ed0152b5c495 121 * struct Foo {
roywant 0:ed0152b5c495 122 * void fn(int, const char*);
roywant 0:ed0152b5c495 123 * };
roywant 0:ed0152b5c495 124 *
roywant 0:ed0152b5c495 125 * Foo foo;
roywant 0:ed0152b5c495 126 * MemberFunctionAdaptor2<Foo> fn_adapted(&Foo::fn);
roywant 0:ed0152b5c495 127 *
roywant 0:ed0152b5c495 128 * fn_adapted(foo, 42, "toto"); // work
roywant 0:ed0152b5c495 129 * fn_adapted(&foo, 42, "toto"); // work
roywant 0:ed0152b5c495 130 * \endcode
roywant 0:ed0152b5c495 131 */
roywant 0:ed0152b5c495 132 template<typename T, typename Arg0, typename Arg1>
roywant 0:ed0152b5c495 133 struct MemberFunctionAdaptor2 {
roywant 0:ed0152b5c495 134 /**
roywant 0:ed0152b5c495 135 * Construct a member function adaptor.
roywant 0:ed0152b5c495 136 * \param fn The member function to addapt.
roywant 0:ed0152b5c495 137 */
roywant 0:ed0152b5c495 138 MemberFunctionAdaptor2(void (T::*fn)(Arg0, Arg1)) : _fn(fn) { }
roywant 0:ed0152b5c495 139
roywant 0:ed0152b5c495 140 /**
roywant 0:ed0152b5c495 141 * Call operator for pointer of T
roywant 0:ed0152b5c495 142 */
roywant 0:ed0152b5c495 143 void operator()(T* self, Arg0 arg0, Arg1 arg1) const {
roywant 0:ed0152b5c495 144 (self->*_fn)(arg0, arg1);
roywant 0:ed0152b5c495 145 }
roywant 0:ed0152b5c495 146
roywant 0:ed0152b5c495 147 /**
roywant 0:ed0152b5c495 148 * Call operator for reference of T
roywant 0:ed0152b5c495 149 */
roywant 0:ed0152b5c495 150 void operator()(T& self, Arg0 arg0, Arg1 arg1) const {
roywant 0:ed0152b5c495 151 (self.*_fn)(arg0, arg1);
roywant 0:ed0152b5c495 152 }
roywant 0:ed0152b5c495 153
roywant 0:ed0152b5c495 154 private:
roywant 0:ed0152b5c495 155 void (T::* const _fn)(Arg0, Arg1);
roywant 0:ed0152b5c495 156 };
roywant 0:ed0152b5c495 157
roywant 0:ed0152b5c495 158 } // namespace detail
roywant 0:ed0152b5c495 159 } // namespace eq
roywant 0:ed0152b5c495 160
roywant 0:ed0152b5c495 161 #endif /* EVENTQUEUE_DETAIL_MEMBERFUNCTIONADAPTOR_H_ */