Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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