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