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