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