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