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