This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apluscw 187:92cbb9eec47b 1 #include "CallChain.h"
apluscw 187:92cbb9eec47b 2 #include "cmsis.h"
apluscw 187:92cbb9eec47b 3 #include "critical.h"
apluscw 187:92cbb9eec47b 4
apluscw 187:92cbb9eec47b 5 namespace mbed {
apluscw 187:92cbb9eec47b 6
apluscw 187:92cbb9eec47b 7 class CallChainLink {
apluscw 187:92cbb9eec47b 8 public:
apluscw 187:92cbb9eec47b 9 CallChainLink(): cb(), next(NULL) {
apluscw 187:92cbb9eec47b 10 // No work to do
apluscw 187:92cbb9eec47b 11 }
apluscw 187:92cbb9eec47b 12
apluscw 187:92cbb9eec47b 13 CallChainLink(Callback<void()> &callback): cb(callback), next(NULL) {
apluscw 187:92cbb9eec47b 14 // No work to do
apluscw 187:92cbb9eec47b 15 }
apluscw 187:92cbb9eec47b 16 Callback<void()> cb;
apluscw 187:92cbb9eec47b 17 CallChainLink * next;
apluscw 187:92cbb9eec47b 18 };
apluscw 187:92cbb9eec47b 19
apluscw 187:92cbb9eec47b 20 CallChain::CallChain(int size) : _chain(NULL) {
apluscw 187:92cbb9eec47b 21 // No work to do
apluscw 187:92cbb9eec47b 22 }
apluscw 187:92cbb9eec47b 23
apluscw 187:92cbb9eec47b 24 CallChain::~CallChain() {
apluscw 187:92cbb9eec47b 25 clear();
apluscw 187:92cbb9eec47b 26 }
apluscw 187:92cbb9eec47b 27
apluscw 187:92cbb9eec47b 28 pFunctionPointer_t CallChain::add(Callback<void()> func) {
apluscw 187:92cbb9eec47b 29 CallChainLink *new_link = new CallChainLink(func);
apluscw 187:92cbb9eec47b 30 if (NULL == _chain) {
apluscw 187:92cbb9eec47b 31 _chain = new_link;
apluscw 187:92cbb9eec47b 32 return &new_link->cb;
apluscw 187:92cbb9eec47b 33 }
apluscw 187:92cbb9eec47b 34
apluscw 187:92cbb9eec47b 35 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 36 while (true) {
apluscw 187:92cbb9eec47b 37 if (NULL == link->next) {
apluscw 187:92cbb9eec47b 38 link->next = new_link;
apluscw 187:92cbb9eec47b 39 return &new_link->cb;
apluscw 187:92cbb9eec47b 40 }
apluscw 187:92cbb9eec47b 41 link = link->next;
apluscw 187:92cbb9eec47b 42 }
apluscw 187:92cbb9eec47b 43 }
apluscw 187:92cbb9eec47b 44
apluscw 187:92cbb9eec47b 45 pFunctionPointer_t CallChain::add_front(Callback<void()> func) {
apluscw 187:92cbb9eec47b 46 CallChainLink *link = new CallChainLink(func);
apluscw 187:92cbb9eec47b 47 link->next = _chain;
apluscw 187:92cbb9eec47b 48 _chain = link->next;
apluscw 187:92cbb9eec47b 49 return &link->cb;
apluscw 187:92cbb9eec47b 50 }
apluscw 187:92cbb9eec47b 51
apluscw 187:92cbb9eec47b 52 int CallChain::size() const {
apluscw 187:92cbb9eec47b 53 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 54 int elements = 0;
apluscw 187:92cbb9eec47b 55 while (link != NULL) {
apluscw 187:92cbb9eec47b 56 elements++;
apluscw 187:92cbb9eec47b 57 link = link->next;
apluscw 187:92cbb9eec47b 58 }
apluscw 187:92cbb9eec47b 59 return elements;
apluscw 187:92cbb9eec47b 60 }
apluscw 187:92cbb9eec47b 61
apluscw 187:92cbb9eec47b 62 pFunctionPointer_t CallChain::get(int idx) const {
apluscw 187:92cbb9eec47b 63 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 64 for (int i = 0; i < idx; i++) {
apluscw 187:92cbb9eec47b 65 if (NULL == link) {
apluscw 187:92cbb9eec47b 66 break;
apluscw 187:92cbb9eec47b 67 }
apluscw 187:92cbb9eec47b 68 link = link->next;
apluscw 187:92cbb9eec47b 69 }
apluscw 187:92cbb9eec47b 70 return &link->cb;
apluscw 187:92cbb9eec47b 71 }
apluscw 187:92cbb9eec47b 72
apluscw 187:92cbb9eec47b 73 int CallChain::find(pFunctionPointer_t f) const {
apluscw 187:92cbb9eec47b 74 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 75 int i = 0;
apluscw 187:92cbb9eec47b 76 while (link != NULL) {
apluscw 187:92cbb9eec47b 77 if (f == &link->cb) {
apluscw 187:92cbb9eec47b 78 return i;
apluscw 187:92cbb9eec47b 79 }
apluscw 187:92cbb9eec47b 80 i++;
apluscw 187:92cbb9eec47b 81 link = link->next;
apluscw 187:92cbb9eec47b 82 }
apluscw 187:92cbb9eec47b 83 return -1;
apluscw 187:92cbb9eec47b 84 }
apluscw 187:92cbb9eec47b 85
apluscw 187:92cbb9eec47b 86 void CallChain::clear() {
apluscw 187:92cbb9eec47b 87 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 88 _chain = NULL;
apluscw 187:92cbb9eec47b 89 while (link != NULL) {
apluscw 187:92cbb9eec47b 90 CallChainLink *temp = link->next;
apluscw 187:92cbb9eec47b 91 delete link;
apluscw 187:92cbb9eec47b 92 link = temp;
apluscw 187:92cbb9eec47b 93 }
apluscw 187:92cbb9eec47b 94 }
apluscw 187:92cbb9eec47b 95
apluscw 187:92cbb9eec47b 96 bool CallChain::remove(pFunctionPointer_t f) {
apluscw 187:92cbb9eec47b 97 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 98 while (link != NULL) {
apluscw 187:92cbb9eec47b 99 if (f == &link->cb) {
apluscw 187:92cbb9eec47b 100 delete link;
apluscw 187:92cbb9eec47b 101 return true;
apluscw 187:92cbb9eec47b 102 }
apluscw 187:92cbb9eec47b 103 link = link->next;
apluscw 187:92cbb9eec47b 104 }
apluscw 187:92cbb9eec47b 105 return false;
apluscw 187:92cbb9eec47b 106 }
apluscw 187:92cbb9eec47b 107
apluscw 187:92cbb9eec47b 108 void CallChain::call() {
apluscw 187:92cbb9eec47b 109 CallChainLink *link = _chain;
apluscw 187:92cbb9eec47b 110 while (link != NULL) {
apluscw 187:92cbb9eec47b 111 link->cb.call();
apluscw 187:92cbb9eec47b 112 link = link->next;
apluscw 187:92cbb9eec47b 113 }
apluscw 187:92cbb9eec47b 114 }
apluscw 187:92cbb9eec47b 115
apluscw 187:92cbb9eec47b 116 } // namespace mbed