mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
common/CallChain.cpp@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | #include "CallChain.h" |
bogdanm | 0:9b334a45a8ff | 2 | #include "cmsis.h" |
bogdanm | 0:9b334a45a8ff | 3 | |
bogdanm | 0:9b334a45a8ff | 4 | namespace mbed { |
bogdanm | 0:9b334a45a8ff | 5 | |
bogdanm | 0:9b334a45a8ff | 6 | CallChain::CallChain(int size) : _chain(), _size(size), _elements(0) { |
bogdanm | 0:9b334a45a8ff | 7 | _chain = new pFunctionPointer_t[size](); |
bogdanm | 0:9b334a45a8ff | 8 | } |
bogdanm | 0:9b334a45a8ff | 9 | |
bogdanm | 0:9b334a45a8ff | 10 | CallChain::~CallChain() { |
bogdanm | 0:9b334a45a8ff | 11 | clear(); |
bogdanm | 0:9b334a45a8ff | 12 | delete _chain; |
bogdanm | 0:9b334a45a8ff | 13 | } |
bogdanm | 0:9b334a45a8ff | 14 | |
bogdanm | 0:9b334a45a8ff | 15 | pFunctionPointer_t CallChain::add(void (*function)(void)) { |
bogdanm | 0:9b334a45a8ff | 16 | return common_add(new FunctionPointer(function)); |
bogdanm | 0:9b334a45a8ff | 17 | } |
bogdanm | 0:9b334a45a8ff | 18 | |
bogdanm | 0:9b334a45a8ff | 19 | pFunctionPointer_t CallChain::add_front(void (*function)(void)) { |
bogdanm | 0:9b334a45a8ff | 20 | return common_add_front(new FunctionPointer(function)); |
bogdanm | 0:9b334a45a8ff | 21 | } |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | int CallChain::size() const { |
bogdanm | 0:9b334a45a8ff | 24 | return _elements; |
bogdanm | 0:9b334a45a8ff | 25 | } |
bogdanm | 0:9b334a45a8ff | 26 | |
bogdanm | 0:9b334a45a8ff | 27 | pFunctionPointer_t CallChain::get(int i) const { |
bogdanm | 0:9b334a45a8ff | 28 | if (i < 0 || i >= _elements) |
bogdanm | 0:9b334a45a8ff | 29 | return NULL; |
bogdanm | 0:9b334a45a8ff | 30 | return _chain[i]; |
bogdanm | 0:9b334a45a8ff | 31 | } |
bogdanm | 0:9b334a45a8ff | 32 | |
bogdanm | 0:9b334a45a8ff | 33 | int CallChain::find(pFunctionPointer_t f) const { |
bogdanm | 0:9b334a45a8ff | 34 | for (int i = 0; i < _elements; i++) |
bogdanm | 0:9b334a45a8ff | 35 | if (f == _chain[i]) |
bogdanm | 0:9b334a45a8ff | 36 | return i; |
bogdanm | 0:9b334a45a8ff | 37 | return -1; |
bogdanm | 0:9b334a45a8ff | 38 | } |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | void CallChain::clear() { |
bogdanm | 0:9b334a45a8ff | 41 | for(int i = 0; i < _elements; i ++) { |
bogdanm | 0:9b334a45a8ff | 42 | delete _chain[i]; |
bogdanm | 0:9b334a45a8ff | 43 | _chain[i] = NULL; |
bogdanm | 0:9b334a45a8ff | 44 | } |
bogdanm | 0:9b334a45a8ff | 45 | _elements = 0; |
bogdanm | 0:9b334a45a8ff | 46 | } |
bogdanm | 0:9b334a45a8ff | 47 | |
bogdanm | 0:9b334a45a8ff | 48 | bool CallChain::remove(pFunctionPointer_t f) { |
bogdanm | 0:9b334a45a8ff | 49 | int i; |
bogdanm | 0:9b334a45a8ff | 50 | |
bogdanm | 0:9b334a45a8ff | 51 | if ((i = find(f)) == -1) |
bogdanm | 0:9b334a45a8ff | 52 | return false; |
bogdanm | 0:9b334a45a8ff | 53 | if (i != _elements - 1) |
bogdanm | 0:9b334a45a8ff | 54 | memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); |
bogdanm | 0:9b334a45a8ff | 55 | delete f; |
bogdanm | 0:9b334a45a8ff | 56 | _elements --; |
bogdanm | 0:9b334a45a8ff | 57 | return true; |
bogdanm | 0:9b334a45a8ff | 58 | } |
bogdanm | 0:9b334a45a8ff | 59 | |
bogdanm | 0:9b334a45a8ff | 60 | void CallChain::call() { |
bogdanm | 0:9b334a45a8ff | 61 | for(int i = 0; i < _elements; i++) |
bogdanm | 0:9b334a45a8ff | 62 | _chain[i]->call(); |
bogdanm | 0:9b334a45a8ff | 63 | } |
bogdanm | 0:9b334a45a8ff | 64 | |
bogdanm | 0:9b334a45a8ff | 65 | void CallChain::_check_size() { |
bogdanm | 0:9b334a45a8ff | 66 | if (_elements < _size) |
bogdanm | 0:9b334a45a8ff | 67 | return; |
bogdanm | 0:9b334a45a8ff | 68 | _size = (_size < 4) ? 4 : _size + 4; |
bogdanm | 0:9b334a45a8ff | 69 | pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); |
bogdanm | 0:9b334a45a8ff | 70 | memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); |
bogdanm | 0:9b334a45a8ff | 71 | delete _chain; |
bogdanm | 0:9b334a45a8ff | 72 | _chain = new_chain; |
bogdanm | 0:9b334a45a8ff | 73 | } |
bogdanm | 0:9b334a45a8ff | 74 | |
bogdanm | 0:9b334a45a8ff | 75 | pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { |
bogdanm | 0:9b334a45a8ff | 76 | _check_size(); |
bogdanm | 0:9b334a45a8ff | 77 | _chain[_elements] = pf; |
bogdanm | 0:9b334a45a8ff | 78 | _elements ++; |
bogdanm | 0:9b334a45a8ff | 79 | return pf; |
bogdanm | 0:9b334a45a8ff | 80 | } |
bogdanm | 0:9b334a45a8ff | 81 | |
bogdanm | 0:9b334a45a8ff | 82 | pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { |
bogdanm | 0:9b334a45a8ff | 83 | _check_size(); |
bogdanm | 0:9b334a45a8ff | 84 | memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); |
bogdanm | 0:9b334a45a8ff | 85 | _chain[0] = pf; |
bogdanm | 0:9b334a45a8ff | 86 | _elements ++; |
bogdanm | 0:9b334a45a8ff | 87 | return pf; |
bogdanm | 0:9b334a45a8ff | 88 | } |
bogdanm | 0:9b334a45a8ff | 89 | |
bogdanm | 0:9b334a45a8ff | 90 | } // namespace mbed |