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