inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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