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

« Back to documentation index

Show/hide line numbers FunctionAdaptor.h Source File

FunctionAdaptor.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_FUNCTIONADAPTOR_H_
00018 #define EVENTQUEUE_DETAIL_FUNCTIONADAPTOR_H_
00019 
00020 #include "MemberFunctionAdaptor.h"
00021 
00022 namespace eq {
00023 namespace detail {
00024 
00025 /**
00026  * In C++, several types can be used as function:
00027  *   - function pointer
00028  *   - member functions
00029  *   - function like object
00030  * While function pointer and function like object can be used with the function
00031  * call syntax, the function call syntax can't be applied for function pointers.
00032  * This meta function yield takes a callable type F in input and as a result
00033  * return a type which can be constructed from F and used with the function call
00034  * syntax.
00035  *
00036  * \code
00037  * class Foo;
00038  *
00039  * Foo foo;
00040  * typedef void (Foo::*foo_function_t)();
00041  * foo_function_t foo_function = &Foo::some_function;
00042  *
00043  * //The following will fail:
00044  * //foo_function(foo)
00045  *
00046  * typedef FunctionAdaptor<foo_function_t>::type foo_function_adaptor_t;
00047  * foo_function_adaptor_t foo_function_adapted(foo_function);
00048  * foo_function_adapted(foo);
00049  *
00050  * \endcode
00051  *
00052  * \tparam F The type of the object to adapt.
00053  */
00054 template<typename F>
00055 struct FunctionAdaptor {
00056     /**
00057      * Common case (function pointer and function like object).
00058      * Yield itself, no addaptation needed.
00059      */
00060     typedef F type;
00061 };
00062 
00063 /**
00064  * Partial specializetion for member function with no arguments
00065  */
00066 template<typename T>
00067 struct FunctionAdaptor<void(T::*)()> {
00068     /**
00069      * Yield a member function adaptor.
00070      */
00071     typedef MemberFunctionAdaptor0<T> type;
00072 };
00073 
00074 /**
00075  * Partial specializetion for member function with one argument
00076  */
00077 template<typename T, typename Arg0>
00078 struct FunctionAdaptor<void(T::*)(Arg0)> {
00079     /**
00080      * Yield a member function adaptor.
00081      */
00082     typedef MemberFunctionAdaptor1<T, Arg0> type;
00083 };
00084 
00085 /**
00086  * Partial specializetion for member function with two arguments
00087  */
00088 template<typename T, typename Arg0, typename Arg1>
00089 struct FunctionAdaptor<void(T::*)(Arg0, Arg1)> {
00090     /**
00091      * Yield a member function adaptor.
00092      */
00093     typedef MemberFunctionAdaptor2<T, Arg0, Arg1> type;
00094 };
00095 
00096 } // namespace detail
00097 } // namespace eq
00098 
00099 #endif /* EVENTQUEUE_DETAIL_FUNCTIONADAPTOR_H_ */