mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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