Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TUKS-COURSE-TIMER by
CallChain.cpp
00001 #include "platform/CallChain.h" 00002 #include "cmsis.h" 00003 #include "platform/critical.h" 00004 00005 namespace mbed { 00006 00007 class CallChainLink { 00008 public: 00009 CallChainLink(): cb(), next(NULL) { 00010 // No work to do 00011 } 00012 00013 CallChainLink(Callback<void()> &callback): cb(callback), next(NULL) { 00014 // No work to do 00015 } 00016 Callback<void()> cb; 00017 CallChainLink * next; 00018 }; 00019 00020 CallChain::CallChain(int size) : _chain(NULL) { 00021 // No work to do 00022 } 00023 00024 CallChain::~CallChain() { 00025 clear(); 00026 } 00027 00028 pFunctionPointer_t CallChain::add(Callback<void()> func) { 00029 CallChainLink *new_link = new CallChainLink(func); 00030 if (NULL == _chain) { 00031 _chain = new_link; 00032 return &new_link->cb; 00033 } 00034 00035 CallChainLink *link = _chain; 00036 while (true) { 00037 if (NULL == link->next) { 00038 link->next = new_link; 00039 return &new_link->cb; 00040 } 00041 link = link->next; 00042 } 00043 } 00044 00045 pFunctionPointer_t CallChain::add_front(Callback<void()> func) { 00046 CallChainLink *link = new CallChainLink(func); 00047 link->next = _chain; 00048 _chain = link; 00049 return &link->cb; 00050 } 00051 00052 int CallChain::size() const { 00053 CallChainLink *link = _chain; 00054 int elements = 0; 00055 while (link != NULL) { 00056 elements++; 00057 link = link->next; 00058 } 00059 return elements; 00060 } 00061 00062 pFunctionPointer_t CallChain::get(int idx) const { 00063 CallChainLink *link = _chain; 00064 for (int i = 0; i < idx; i++) { 00065 if (NULL == link) { 00066 break; 00067 } 00068 link = link->next; 00069 } 00070 return &link->cb; 00071 } 00072 00073 int CallChain::find(pFunctionPointer_t f) const { 00074 CallChainLink *link = _chain; 00075 int i = 0; 00076 while (link != NULL) { 00077 if (f == &link->cb) { 00078 return i; 00079 } 00080 i++; 00081 link = link->next; 00082 } 00083 return -1; 00084 } 00085 00086 void CallChain::clear() { 00087 CallChainLink *link = _chain; 00088 _chain = NULL; 00089 while (link != NULL) { 00090 CallChainLink *temp = link->next; 00091 delete link; 00092 link = temp; 00093 } 00094 } 00095 00096 bool CallChain::remove(pFunctionPointer_t f) { 00097 CallChainLink *link = _chain; 00098 while (link != NULL) { 00099 if (f == &link->cb) { 00100 delete link; 00101 return true; 00102 } 00103 link = link->next; 00104 } 00105 return false; 00106 } 00107 00108 void CallChain::call() { 00109 CallChainLink *link = _chain; 00110 while (link != NULL) { 00111 link->cb.call(); 00112 link = link->next; 00113 } 00114 } 00115 00116 } // namespace mbed
Generated on Tue Jul 12 2022 17:38:48 by
