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