mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CallChain.cpp Source File

CallChain.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 // Suppress deprecation warnings since this whole
00019 // class is deprecated already
00020 #include "mbed_toolchain.h"
00021 #undef MBED_DEPRECATED_SINCE
00022 #define MBED_DEPRECATED_SINCE(...)
00023 
00024 #include "platform/CallChain.h"
00025 #include "cmsis.h"
00026 #include "platform/mbed_critical.h"
00027 
00028 namespace mbed {
00029 
00030 class CallChainLink {
00031 public:
00032     CallChainLink(): cb(), next(NULL)
00033     {
00034         // No work to do
00035     }
00036 
00037     CallChainLink(Callback<void()> &callback): cb(callback), next(NULL)
00038     {
00039         // No work to do
00040     }
00041     Callback<void()> cb;
00042     CallChainLink *next;
00043 };
00044 
00045 CallChain::CallChain(int size) : _chain(NULL)
00046 {
00047     // No work to do
00048 }
00049 
00050 CallChain::~CallChain()
00051 {
00052     clear();
00053 }
00054 
00055 pFunctionPointer_t CallChain::add(Callback<void()> func)
00056 {
00057     CallChainLink *new_link = new CallChainLink(func);
00058     if (NULL == _chain) {
00059         _chain = new_link;
00060         return &new_link->cb;
00061     }
00062 
00063     CallChainLink *link = _chain;
00064     while (true) {
00065         if (NULL == link->next) {
00066             link->next = new_link;
00067             return &new_link->cb;
00068         }
00069         link = link->next;
00070     }
00071 }
00072 
00073 pFunctionPointer_t CallChain::add_front(Callback<void()> func)
00074 {
00075     CallChainLink *link = new CallChainLink(func);
00076     link->next = _chain;
00077     _chain = link;
00078     return &link->cb;
00079 }
00080 
00081 int CallChain::size() const
00082 {
00083     CallChainLink *link = _chain;
00084     int elements = 0;
00085     while (link != NULL) {
00086         elements++;
00087         link = link->next;
00088     }
00089     return elements;
00090 }
00091 
00092 pFunctionPointer_t CallChain::get(int idx) const
00093 {
00094     CallChainLink *link = _chain;
00095     for (int i = 0; i < idx; i++) {
00096         if (NULL == link) {
00097             break;
00098         }
00099         link = link->next;
00100     }
00101     return &link->cb;
00102 }
00103 
00104 int CallChain::find(pFunctionPointer_t f) const
00105 {
00106     CallChainLink *link = _chain;
00107     int i = 0;
00108     while (link != NULL) {
00109         if (f == &link->cb) {
00110             return i;
00111         }
00112         i++;
00113         link = link->next;
00114     }
00115     return -1;
00116 }
00117 
00118 void CallChain::clear()
00119 {
00120     CallChainLink *link = _chain;
00121     _chain = NULL;
00122     while (link != NULL) {
00123         CallChainLink *temp = link->next;
00124         delete link;
00125         link = temp;
00126     }
00127 }
00128 
00129 bool CallChain::remove(pFunctionPointer_t f)
00130 {
00131     CallChainLink *link = _chain;
00132     while (link != NULL) {
00133         if (f == &link->cb) {
00134             delete link;
00135             return true;
00136         }
00137         link = link->next;
00138     }
00139     return false;
00140 }
00141 
00142 void CallChain::call()
00143 {
00144     CallChainLink *link = _chain;
00145     while (link != NULL) {
00146         link->cb.call();
00147         link = link->next;
00148     }
00149 }
00150 
00151 } // namespace mbed