,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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