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