mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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