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