Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /* mbed Microcontroller Library
Simon Cooksey 0:fb7af294d5d9 2 * Copyright (c) 2006-2015 ARM Limited
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Licensed under the Apache License, Version 2.0 (the "License");
Simon Cooksey 0:fb7af294d5d9 5 * you may not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 6 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 7 *
Simon Cooksey 0:fb7af294d5d9 8 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 9 *
Simon Cooksey 0:fb7af294d5d9 10 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 11 * distributed under the License is distributed on an "AS IS" BASIS,
Simon Cooksey 0:fb7af294d5d9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 13 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 14 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 15 */
Simon Cooksey 0:fb7af294d5d9 16 #ifndef MBED_FUNCTIONPOINTER_H
Simon Cooksey 0:fb7af294d5d9 17 #define MBED_FUNCTIONPOINTER_H
Simon Cooksey 0:fb7af294d5d9 18
Simon Cooksey 0:fb7af294d5d9 19 #include "platform/Callback.h"
Simon Cooksey 0:fb7af294d5d9 20 #include "platform/toolchain.h"
Simon Cooksey 0:fb7af294d5d9 21 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 22 #include <stdint.h>
Simon Cooksey 0:fb7af294d5d9 23
Simon Cooksey 0:fb7af294d5d9 24 namespace mbed {
Simon Cooksey 0:fb7af294d5d9 25 /** \addtogroup platform */
Simon Cooksey 0:fb7af294d5d9 26 /** @{*/
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28
Simon Cooksey 0:fb7af294d5d9 29 // Declarations for backwards compatibility
Simon Cooksey 0:fb7af294d5d9 30 // To be foward compatible, code should adopt the Callback class
Simon Cooksey 0:fb7af294d5d9 31 template <typename R, typename A1>
Simon Cooksey 0:fb7af294d5d9 32 class FunctionPointerArg1 : public Callback<R(A1)> {
Simon Cooksey 0:fb7af294d5d9 33 public:
Simon Cooksey 0:fb7af294d5d9 34 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Simon Cooksey 0:fb7af294d5d9 35 "FunctionPointerArg1<R, A> has been replaced by Callback<R(A)>")
Simon Cooksey 0:fb7af294d5d9 36 FunctionPointerArg1(R (*function)(A1) = 0)
Simon Cooksey 0:fb7af294d5d9 37 : Callback<R(A1)>(function) {}
Simon Cooksey 0:fb7af294d5d9 38
Simon Cooksey 0:fb7af294d5d9 39 template<typename T>
Simon Cooksey 0:fb7af294d5d9 40 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Simon Cooksey 0:fb7af294d5d9 41 "FunctionPointerArg1<R, A> has been replaced by Callback<R(A)>")
Simon Cooksey 0:fb7af294d5d9 42 FunctionPointerArg1(T *object, R (T::*member)(A1))
Simon Cooksey 0:fb7af294d5d9 43 : Callback<R(A1)>(object, member) {}
Simon Cooksey 0:fb7af294d5d9 44
Simon Cooksey 0:fb7af294d5d9 45 R (*get_function())(A1) {
Simon Cooksey 0:fb7af294d5d9 46 return *reinterpret_cast<R (**)(A1)>(this);
Simon Cooksey 0:fb7af294d5d9 47 }
Simon Cooksey 0:fb7af294d5d9 48
Simon Cooksey 0:fb7af294d5d9 49 R call(A1 a1) const {
Simon Cooksey 0:fb7af294d5d9 50 if (!Callback<R(A1)>::operator bool()) {
Simon Cooksey 0:fb7af294d5d9 51 return (R)0;
Simon Cooksey 0:fb7af294d5d9 52 }
Simon Cooksey 0:fb7af294d5d9 53
Simon Cooksey 0:fb7af294d5d9 54 return Callback<R(A1)>::call(a1);
Simon Cooksey 0:fb7af294d5d9 55 }
Simon Cooksey 0:fb7af294d5d9 56
Simon Cooksey 0:fb7af294d5d9 57 R operator()(A1 a1) const {
Simon Cooksey 0:fb7af294d5d9 58 return Callback<R(A1)>::call(a1);
Simon Cooksey 0:fb7af294d5d9 59 }
Simon Cooksey 0:fb7af294d5d9 60 };
Simon Cooksey 0:fb7af294d5d9 61
Simon Cooksey 0:fb7af294d5d9 62 template <typename R>
Simon Cooksey 0:fb7af294d5d9 63 class FunctionPointerArg1<R, void> : public Callback<R()> {
Simon Cooksey 0:fb7af294d5d9 64 public:
Simon Cooksey 0:fb7af294d5d9 65 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Simon Cooksey 0:fb7af294d5d9 66 "FunctionPointer has been replaced by Callback<void()>")
Simon Cooksey 0:fb7af294d5d9 67 FunctionPointerArg1(R (*function)() = 0)
Simon Cooksey 0:fb7af294d5d9 68 : Callback<R()>(function) {}
Simon Cooksey 0:fb7af294d5d9 69
Simon Cooksey 0:fb7af294d5d9 70 template<typename T>
Simon Cooksey 0:fb7af294d5d9 71 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Simon Cooksey 0:fb7af294d5d9 72 "FunctionPointer has been replaced by Callback<void()>")
Simon Cooksey 0:fb7af294d5d9 73 FunctionPointerArg1(T *object, R (T::*member)())
Simon Cooksey 0:fb7af294d5d9 74 : Callback<R()>(object, member) {}
Simon Cooksey 0:fb7af294d5d9 75
Simon Cooksey 0:fb7af294d5d9 76 R (*get_function())() {
Simon Cooksey 0:fb7af294d5d9 77 return *reinterpret_cast<R (**)()>(this);
Simon Cooksey 0:fb7af294d5d9 78 }
Simon Cooksey 0:fb7af294d5d9 79
Simon Cooksey 0:fb7af294d5d9 80 R call() const {
Simon Cooksey 0:fb7af294d5d9 81 if (!Callback<R()>::operator bool()) {
Simon Cooksey 0:fb7af294d5d9 82 return (R)0;
Simon Cooksey 0:fb7af294d5d9 83 }
Simon Cooksey 0:fb7af294d5d9 84
Simon Cooksey 0:fb7af294d5d9 85 return Callback<R()>::call();
Simon Cooksey 0:fb7af294d5d9 86 }
Simon Cooksey 0:fb7af294d5d9 87
Simon Cooksey 0:fb7af294d5d9 88 R operator()() const {
Simon Cooksey 0:fb7af294d5d9 89 return Callback<R()>::call();
Simon Cooksey 0:fb7af294d5d9 90 }
Simon Cooksey 0:fb7af294d5d9 91 };
Simon Cooksey 0:fb7af294d5d9 92
Simon Cooksey 0:fb7af294d5d9 93 typedef FunctionPointerArg1<void, void> FunctionPointer;
Simon Cooksey 0:fb7af294d5d9 94
Simon Cooksey 0:fb7af294d5d9 95
Simon Cooksey 0:fb7af294d5d9 96 } // namespace mbed
Simon Cooksey 0:fb7af294d5d9 97
Simon Cooksey 0:fb7af294d5d9 98 #endif
Simon Cooksey 0:fb7af294d5d9 99
Simon Cooksey 0:fb7af294d5d9 100 /** @}*/