mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
platform/source/CallChain.cpp@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2018-2019 ARM Limited |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | |
kenjiArai | 1:9db0e321a9f4 | 18 | // Suppress deprecation warnings since this whole |
kenjiArai | 1:9db0e321a9f4 | 19 | // class is deprecated already |
kenjiArai | 1:9db0e321a9f4 | 20 | #include "mbed_toolchain.h" |
kenjiArai | 1:9db0e321a9f4 | 21 | #undef MBED_DEPRECATED_SINCE |
kenjiArai | 1:9db0e321a9f4 | 22 | #define MBED_DEPRECATED_SINCE(...) |
kenjiArai | 1:9db0e321a9f4 | 23 | |
kenjiArai | 1:9db0e321a9f4 | 24 | #include "platform/CallChain.h" |
kenjiArai | 1:9db0e321a9f4 | 25 | #include "cmsis.h" |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "platform/mbed_critical.h" |
kenjiArai | 1:9db0e321a9f4 | 27 | |
kenjiArai | 1:9db0e321a9f4 | 28 | namespace mbed { |
kenjiArai | 1:9db0e321a9f4 | 29 | |
kenjiArai | 1:9db0e321a9f4 | 30 | class CallChainLink { |
kenjiArai | 1:9db0e321a9f4 | 31 | public: |
kenjiArai | 1:9db0e321a9f4 | 32 | CallChainLink(): cb(), next(NULL) |
kenjiArai | 1:9db0e321a9f4 | 33 | { |
kenjiArai | 1:9db0e321a9f4 | 34 | // No work to do |
kenjiArai | 1:9db0e321a9f4 | 35 | } |
kenjiArai | 1:9db0e321a9f4 | 36 | |
kenjiArai | 1:9db0e321a9f4 | 37 | CallChainLink(Callback<void()> &callback): cb(callback), next(NULL) |
kenjiArai | 1:9db0e321a9f4 | 38 | { |
kenjiArai | 1:9db0e321a9f4 | 39 | // No work to do |
kenjiArai | 1:9db0e321a9f4 | 40 | } |
kenjiArai | 1:9db0e321a9f4 | 41 | Callback<void()> cb; |
kenjiArai | 1:9db0e321a9f4 | 42 | CallChainLink *next; |
kenjiArai | 1:9db0e321a9f4 | 43 | }; |
kenjiArai | 1:9db0e321a9f4 | 44 | |
kenjiArai | 1:9db0e321a9f4 | 45 | CallChain::CallChain(int size) : _chain(NULL) |
kenjiArai | 1:9db0e321a9f4 | 46 | { |
kenjiArai | 1:9db0e321a9f4 | 47 | // No work to do |
kenjiArai | 1:9db0e321a9f4 | 48 | } |
kenjiArai | 1:9db0e321a9f4 | 49 | |
kenjiArai | 1:9db0e321a9f4 | 50 | CallChain::~CallChain() |
kenjiArai | 1:9db0e321a9f4 | 51 | { |
kenjiArai | 1:9db0e321a9f4 | 52 | clear(); |
kenjiArai | 1:9db0e321a9f4 | 53 | } |
kenjiArai | 1:9db0e321a9f4 | 54 | |
kenjiArai | 1:9db0e321a9f4 | 55 | pFunctionPointer_t CallChain::add(Callback<void()> func) |
kenjiArai | 1:9db0e321a9f4 | 56 | { |
kenjiArai | 1:9db0e321a9f4 | 57 | CallChainLink *new_link = new CallChainLink(func); |
kenjiArai | 1:9db0e321a9f4 | 58 | if (NULL == _chain) { |
kenjiArai | 1:9db0e321a9f4 | 59 | _chain = new_link; |
kenjiArai | 1:9db0e321a9f4 | 60 | return &new_link->cb; |
kenjiArai | 1:9db0e321a9f4 | 61 | } |
kenjiArai | 1:9db0e321a9f4 | 62 | |
kenjiArai | 1:9db0e321a9f4 | 63 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 64 | while (true) { |
kenjiArai | 1:9db0e321a9f4 | 65 | if (NULL == link->next) { |
kenjiArai | 1:9db0e321a9f4 | 66 | link->next = new_link; |
kenjiArai | 1:9db0e321a9f4 | 67 | return &new_link->cb; |
kenjiArai | 1:9db0e321a9f4 | 68 | } |
kenjiArai | 1:9db0e321a9f4 | 69 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 70 | } |
kenjiArai | 1:9db0e321a9f4 | 71 | } |
kenjiArai | 1:9db0e321a9f4 | 72 | |
kenjiArai | 1:9db0e321a9f4 | 73 | pFunctionPointer_t CallChain::add_front(Callback<void()> func) |
kenjiArai | 1:9db0e321a9f4 | 74 | { |
kenjiArai | 1:9db0e321a9f4 | 75 | CallChainLink *link = new CallChainLink(func); |
kenjiArai | 1:9db0e321a9f4 | 76 | link->next = _chain; |
kenjiArai | 1:9db0e321a9f4 | 77 | _chain = link; |
kenjiArai | 1:9db0e321a9f4 | 78 | return &link->cb; |
kenjiArai | 1:9db0e321a9f4 | 79 | } |
kenjiArai | 1:9db0e321a9f4 | 80 | |
kenjiArai | 1:9db0e321a9f4 | 81 | int CallChain::size() const |
kenjiArai | 1:9db0e321a9f4 | 82 | { |
kenjiArai | 1:9db0e321a9f4 | 83 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 84 | int elements = 0; |
kenjiArai | 1:9db0e321a9f4 | 85 | while (link != NULL) { |
kenjiArai | 1:9db0e321a9f4 | 86 | elements++; |
kenjiArai | 1:9db0e321a9f4 | 87 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 88 | } |
kenjiArai | 1:9db0e321a9f4 | 89 | return elements; |
kenjiArai | 1:9db0e321a9f4 | 90 | } |
kenjiArai | 1:9db0e321a9f4 | 91 | |
kenjiArai | 1:9db0e321a9f4 | 92 | pFunctionPointer_t CallChain::get(int idx) const |
kenjiArai | 1:9db0e321a9f4 | 93 | { |
kenjiArai | 1:9db0e321a9f4 | 94 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 95 | for (int i = 0; i < idx; i++) { |
kenjiArai | 1:9db0e321a9f4 | 96 | if (NULL == link) { |
kenjiArai | 1:9db0e321a9f4 | 97 | break; |
kenjiArai | 1:9db0e321a9f4 | 98 | } |
kenjiArai | 1:9db0e321a9f4 | 99 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 100 | } |
kenjiArai | 1:9db0e321a9f4 | 101 | return &link->cb; |
kenjiArai | 1:9db0e321a9f4 | 102 | } |
kenjiArai | 1:9db0e321a9f4 | 103 | |
kenjiArai | 1:9db0e321a9f4 | 104 | int CallChain::find(pFunctionPointer_t f) const |
kenjiArai | 1:9db0e321a9f4 | 105 | { |
kenjiArai | 1:9db0e321a9f4 | 106 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 107 | int i = 0; |
kenjiArai | 1:9db0e321a9f4 | 108 | while (link != NULL) { |
kenjiArai | 1:9db0e321a9f4 | 109 | if (f == &link->cb) { |
kenjiArai | 1:9db0e321a9f4 | 110 | return i; |
kenjiArai | 1:9db0e321a9f4 | 111 | } |
kenjiArai | 1:9db0e321a9f4 | 112 | i++; |
kenjiArai | 1:9db0e321a9f4 | 113 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 114 | } |
kenjiArai | 1:9db0e321a9f4 | 115 | return -1; |
kenjiArai | 1:9db0e321a9f4 | 116 | } |
kenjiArai | 1:9db0e321a9f4 | 117 | |
kenjiArai | 1:9db0e321a9f4 | 118 | void CallChain::clear() |
kenjiArai | 1:9db0e321a9f4 | 119 | { |
kenjiArai | 1:9db0e321a9f4 | 120 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 121 | _chain = NULL; |
kenjiArai | 1:9db0e321a9f4 | 122 | while (link != NULL) { |
kenjiArai | 1:9db0e321a9f4 | 123 | CallChainLink *temp = link->next; |
kenjiArai | 1:9db0e321a9f4 | 124 | delete link; |
kenjiArai | 1:9db0e321a9f4 | 125 | link = temp; |
kenjiArai | 1:9db0e321a9f4 | 126 | } |
kenjiArai | 1:9db0e321a9f4 | 127 | } |
kenjiArai | 1:9db0e321a9f4 | 128 | |
kenjiArai | 1:9db0e321a9f4 | 129 | bool CallChain::remove(pFunctionPointer_t f) |
kenjiArai | 1:9db0e321a9f4 | 130 | { |
kenjiArai | 1:9db0e321a9f4 | 131 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 132 | while (link != NULL) { |
kenjiArai | 1:9db0e321a9f4 | 133 | if (f == &link->cb) { |
kenjiArai | 1:9db0e321a9f4 | 134 | delete link; |
kenjiArai | 1:9db0e321a9f4 | 135 | return true; |
kenjiArai | 1:9db0e321a9f4 | 136 | } |
kenjiArai | 1:9db0e321a9f4 | 137 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 138 | } |
kenjiArai | 1:9db0e321a9f4 | 139 | return false; |
kenjiArai | 1:9db0e321a9f4 | 140 | } |
kenjiArai | 1:9db0e321a9f4 | 141 | |
kenjiArai | 1:9db0e321a9f4 | 142 | void CallChain::call() |
kenjiArai | 1:9db0e321a9f4 | 143 | { |
kenjiArai | 1:9db0e321a9f4 | 144 | CallChainLink *link = _chain; |
kenjiArai | 1:9db0e321a9f4 | 145 | while (link != NULL) { |
kenjiArai | 1:9db0e321a9f4 | 146 | link->cb.call(); |
kenjiArai | 1:9db0e321a9f4 | 147 | link = link->next; |
kenjiArai | 1:9db0e321a9f4 | 148 | } |
kenjiArai | 1:9db0e321a9f4 | 149 | } |
kenjiArai | 1:9db0e321a9f4 | 150 | |
kenjiArai | 1:9db0e321a9f4 | 151 | } // namespace mbed |