Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API nRF51822
Fork of BLE_NODE_TEST by
mbed-src/common/CallChain.cpp@9:05f0b5a3a70a, 2014-10-29 (annotated)
- Committer:
- yihui
- Date:
- Wed Oct 29 06:23:47 2014 +0000
- Revision:
- 9:05f0b5a3a70a
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | #include "CallChain.h" |
yihui | 9:05f0b5a3a70a | 2 | #include "cmsis.h" |
yihui | 9:05f0b5a3a70a | 3 | |
yihui | 9:05f0b5a3a70a | 4 | namespace mbed { |
yihui | 9:05f0b5a3a70a | 5 | |
yihui | 9:05f0b5a3a70a | 6 | CallChain::CallChain(int size) : _chain(), _size(size), _elements(0) { |
yihui | 9:05f0b5a3a70a | 7 | _chain = new pFunctionPointer_t[size](); |
yihui | 9:05f0b5a3a70a | 8 | } |
yihui | 9:05f0b5a3a70a | 9 | |
yihui | 9:05f0b5a3a70a | 10 | CallChain::~CallChain() { |
yihui | 9:05f0b5a3a70a | 11 | clear(); |
yihui | 9:05f0b5a3a70a | 12 | delete _chain; |
yihui | 9:05f0b5a3a70a | 13 | } |
yihui | 9:05f0b5a3a70a | 14 | |
yihui | 9:05f0b5a3a70a | 15 | pFunctionPointer_t CallChain::add(void (*function)(void)) { |
yihui | 9:05f0b5a3a70a | 16 | return common_add(new FunctionPointer(function)); |
yihui | 9:05f0b5a3a70a | 17 | } |
yihui | 9:05f0b5a3a70a | 18 | |
yihui | 9:05f0b5a3a70a | 19 | pFunctionPointer_t CallChain::add_front(void (*function)(void)) { |
yihui | 9:05f0b5a3a70a | 20 | return common_add_front(new FunctionPointer(function)); |
yihui | 9:05f0b5a3a70a | 21 | } |
yihui | 9:05f0b5a3a70a | 22 | |
yihui | 9:05f0b5a3a70a | 23 | int CallChain::size() const { |
yihui | 9:05f0b5a3a70a | 24 | return _elements; |
yihui | 9:05f0b5a3a70a | 25 | } |
yihui | 9:05f0b5a3a70a | 26 | |
yihui | 9:05f0b5a3a70a | 27 | pFunctionPointer_t CallChain::get(int i) const { |
yihui | 9:05f0b5a3a70a | 28 | if (i < 0 || i >= _elements) |
yihui | 9:05f0b5a3a70a | 29 | return NULL; |
yihui | 9:05f0b5a3a70a | 30 | return _chain[i]; |
yihui | 9:05f0b5a3a70a | 31 | } |
yihui | 9:05f0b5a3a70a | 32 | |
yihui | 9:05f0b5a3a70a | 33 | int CallChain::find(pFunctionPointer_t f) const { |
yihui | 9:05f0b5a3a70a | 34 | for (int i = 0; i < _elements; i++) |
yihui | 9:05f0b5a3a70a | 35 | if (f == _chain[i]) |
yihui | 9:05f0b5a3a70a | 36 | return i; |
yihui | 9:05f0b5a3a70a | 37 | return -1; |
yihui | 9:05f0b5a3a70a | 38 | } |
yihui | 9:05f0b5a3a70a | 39 | |
yihui | 9:05f0b5a3a70a | 40 | void CallChain::clear() { |
yihui | 9:05f0b5a3a70a | 41 | for(int i = 0; i < _elements; i ++) { |
yihui | 9:05f0b5a3a70a | 42 | delete _chain[i]; |
yihui | 9:05f0b5a3a70a | 43 | _chain[i] = NULL; |
yihui | 9:05f0b5a3a70a | 44 | } |
yihui | 9:05f0b5a3a70a | 45 | _elements = 0; |
yihui | 9:05f0b5a3a70a | 46 | } |
yihui | 9:05f0b5a3a70a | 47 | |
yihui | 9:05f0b5a3a70a | 48 | bool CallChain::remove(pFunctionPointer_t f) { |
yihui | 9:05f0b5a3a70a | 49 | int i; |
yihui | 9:05f0b5a3a70a | 50 | |
yihui | 9:05f0b5a3a70a | 51 | if ((i = find(f)) == -1) |
yihui | 9:05f0b5a3a70a | 52 | return false; |
yihui | 9:05f0b5a3a70a | 53 | if (i != _elements - 1) |
yihui | 9:05f0b5a3a70a | 54 | memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); |
yihui | 9:05f0b5a3a70a | 55 | delete f; |
yihui | 9:05f0b5a3a70a | 56 | _elements --; |
yihui | 9:05f0b5a3a70a | 57 | return true; |
yihui | 9:05f0b5a3a70a | 58 | } |
yihui | 9:05f0b5a3a70a | 59 | |
yihui | 9:05f0b5a3a70a | 60 | void CallChain::call() { |
yihui | 9:05f0b5a3a70a | 61 | for(int i = 0; i < _elements; i++) |
yihui | 9:05f0b5a3a70a | 62 | _chain[i]->call(); |
yihui | 9:05f0b5a3a70a | 63 | } |
yihui | 9:05f0b5a3a70a | 64 | |
yihui | 9:05f0b5a3a70a | 65 | void CallChain::_check_size() { |
yihui | 9:05f0b5a3a70a | 66 | if (_elements < _size) |
yihui | 9:05f0b5a3a70a | 67 | return; |
yihui | 9:05f0b5a3a70a | 68 | _size = (_size < 4) ? 4 : _size + 4; |
yihui | 9:05f0b5a3a70a | 69 | pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); |
yihui | 9:05f0b5a3a70a | 70 | memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); |
yihui | 9:05f0b5a3a70a | 71 | delete _chain; |
yihui | 9:05f0b5a3a70a | 72 | _chain = new_chain; |
yihui | 9:05f0b5a3a70a | 73 | } |
yihui | 9:05f0b5a3a70a | 74 | |
yihui | 9:05f0b5a3a70a | 75 | pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { |
yihui | 9:05f0b5a3a70a | 76 | _check_size(); |
yihui | 9:05f0b5a3a70a | 77 | _chain[_elements] = pf; |
yihui | 9:05f0b5a3a70a | 78 | _elements ++; |
yihui | 9:05f0b5a3a70a | 79 | return pf; |
yihui | 9:05f0b5a3a70a | 80 | } |
yihui | 9:05f0b5a3a70a | 81 | |
yihui | 9:05f0b5a3a70a | 82 | pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { |
yihui | 9:05f0b5a3a70a | 83 | _check_size(); |
yihui | 9:05f0b5a3a70a | 84 | memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); |
yihui | 9:05f0b5a3a70a | 85 | _chain[0] = pf; |
yihui | 9:05f0b5a3a70a | 86 | _elements ++; |
yihui | 9:05f0b5a3a70a | 87 | return pf; |
yihui | 9:05f0b5a3a70a | 88 | } |
yihui | 9:05f0b5a3a70a | 89 | |
yihui | 9:05f0b5a3a70a | 90 | } // namespace mbed |