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_FUNCTIONADAPTOR_H_
roywant 0:ed0152b5c495 18 #define EVENTQUEUE_DETAIL_FUNCTIONADAPTOR_H_
roywant 0:ed0152b5c495 19
roywant 0:ed0152b5c495 20 #include "MemberFunctionAdaptor.h"
roywant 0:ed0152b5c495 21
roywant 0:ed0152b5c495 22 namespace eq {
roywant 0:ed0152b5c495 23 namespace detail {
roywant 0:ed0152b5c495 24
roywant 0:ed0152b5c495 25 /**
roywant 0:ed0152b5c495 26 * In C++, several types can be used as function:
roywant 0:ed0152b5c495 27 * - function pointer
roywant 0:ed0152b5c495 28 * - member functions
roywant 0:ed0152b5c495 29 * - function like object
roywant 0:ed0152b5c495 30 * While function pointer and function like object can be used with the function
roywant 0:ed0152b5c495 31 * call syntax, the function call syntax can't be applied for function pointers.
roywant 0:ed0152b5c495 32 * This meta function yield takes a callable type F in input and as a result
roywant 0:ed0152b5c495 33 * return a type which can be constructed from F and used with the function call
roywant 0:ed0152b5c495 34 * syntax.
roywant 0:ed0152b5c495 35 *
roywant 0:ed0152b5c495 36 * \code
roywant 0:ed0152b5c495 37 * class Foo;
roywant 0:ed0152b5c495 38 *
roywant 0:ed0152b5c495 39 * Foo foo;
roywant 0:ed0152b5c495 40 * typedef void (Foo::*foo_function_t)();
roywant 0:ed0152b5c495 41 * foo_function_t foo_function = &Foo::some_function;
roywant 0:ed0152b5c495 42 *
roywant 0:ed0152b5c495 43 * //The following will fail:
roywant 0:ed0152b5c495 44 * //foo_function(foo)
roywant 0:ed0152b5c495 45 *
roywant 0:ed0152b5c495 46 * typedef FunctionAdaptor<foo_function_t>::type foo_function_adaptor_t;
roywant 0:ed0152b5c495 47 * foo_function_adaptor_t foo_function_adapted(foo_function);
roywant 0:ed0152b5c495 48 * foo_function_adapted(foo);
roywant 0:ed0152b5c495 49 *
roywant 0:ed0152b5c495 50 * \endcode
roywant 0:ed0152b5c495 51 *
roywant 0:ed0152b5c495 52 * \tparam F The type of the object to adapt.
roywant 0:ed0152b5c495 53 */
roywant 0:ed0152b5c495 54 template<typename F>
roywant 0:ed0152b5c495 55 struct FunctionAdaptor {
roywant 0:ed0152b5c495 56 /**
roywant 0:ed0152b5c495 57 * Common case (function pointer and function like object).
roywant 0:ed0152b5c495 58 * Yield itself, no addaptation needed.
roywant 0:ed0152b5c495 59 */
roywant 0:ed0152b5c495 60 typedef F type;
roywant 0:ed0152b5c495 61 };
roywant 0:ed0152b5c495 62
roywant 0:ed0152b5c495 63 /**
roywant 0:ed0152b5c495 64 * Partial specializetion for member function with no arguments
roywant 0:ed0152b5c495 65 */
roywant 0:ed0152b5c495 66 template<typename T>
roywant 0:ed0152b5c495 67 struct FunctionAdaptor<void(T::*)()> {
roywant 0:ed0152b5c495 68 /**
roywant 0:ed0152b5c495 69 * Yield a member function adaptor.
roywant 0:ed0152b5c495 70 */
roywant 0:ed0152b5c495 71 typedef MemberFunctionAdaptor0<T> type;
roywant 0:ed0152b5c495 72 };
roywant 0:ed0152b5c495 73
roywant 0:ed0152b5c495 74 /**
roywant 0:ed0152b5c495 75 * Partial specializetion for member function with one argument
roywant 0:ed0152b5c495 76 */
roywant 0:ed0152b5c495 77 template<typename T, typename Arg0>
roywant 0:ed0152b5c495 78 struct FunctionAdaptor<void(T::*)(Arg0)> {
roywant 0:ed0152b5c495 79 /**
roywant 0:ed0152b5c495 80 * Yield a member function adaptor.
roywant 0:ed0152b5c495 81 */
roywant 0:ed0152b5c495 82 typedef MemberFunctionAdaptor1<T, Arg0> type;
roywant 0:ed0152b5c495 83 };
roywant 0:ed0152b5c495 84
roywant 0:ed0152b5c495 85 /**
roywant 0:ed0152b5c495 86 * Partial specializetion for member function with two arguments
roywant 0:ed0152b5c495 87 */
roywant 0:ed0152b5c495 88 template<typename T, typename Arg0, typename Arg1>
roywant 0:ed0152b5c495 89 struct FunctionAdaptor<void(T::*)(Arg0, Arg1)> {
roywant 0:ed0152b5c495 90 /**
roywant 0:ed0152b5c495 91 * Yield a member function adaptor.
roywant 0:ed0152b5c495 92 */
roywant 0:ed0152b5c495 93 typedef MemberFunctionAdaptor2<T, Arg0, Arg1> type;
roywant 0:ed0152b5c495 94 };
roywant 0:ed0152b5c495 95
roywant 0:ed0152b5c495 96 } // namespace detail
roywant 0:ed0152b5c495 97 } // namespace eq
roywant 0:ed0152b5c495 98
roywant 0:ed0152b5c495 99 #endif /* EVENTQUEUE_DETAIL_FUNCTIONADAPTOR_H_ */