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