test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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