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