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