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.
Fork of mbed-src by
CallChain.cpp
00001 #include "CallChain.h" 00002 #include "cmsis.h" 00003 00004 namespace mbed { 00005 00006 CallChain::CallChain(int size) : _size(size), _elements(0) { 00007 _chain = new pFunctionPointer_t[size](); 00008 } 00009 00010 CallChain::~CallChain() { 00011 clear(); 00012 delete _chain; 00013 } 00014 00015 pFunctionPointer_t CallChain::add(void (*function)(void)) { 00016 return common_add(new FunctionPointer(function)); 00017 } 00018 00019 pFunctionPointer_t CallChain::add_front(void (*function)(void)) { 00020 return common_add_front(new FunctionPointer(function)); 00021 } 00022 00023 int CallChain::size() const { 00024 return _elements; 00025 } 00026 00027 pFunctionPointer_t CallChain::get(int i) const { 00028 if (i < 0 || i >= _elements) 00029 return NULL; 00030 return _chain[i]; 00031 } 00032 00033 int CallChain::find(pFunctionPointer_t f) const { 00034 for (int i = 0; i < _elements; i++) 00035 if (f == _chain[i]) 00036 return i; 00037 return -1; 00038 } 00039 00040 void CallChain::clear() { 00041 for(int i = 0; i < _elements; i ++) { 00042 delete _chain[i]; 00043 _chain[i] = NULL; 00044 } 00045 _elements = 0; 00046 } 00047 00048 bool CallChain::remove(pFunctionPointer_t f) { 00049 int i; 00050 00051 if ((i = find(f)) == -1) 00052 return false; 00053 if (i != _elements - 1) 00054 memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); 00055 delete f; 00056 _elements --; 00057 return true; 00058 } 00059 00060 void CallChain::call() { 00061 for(int i = 0; i < _elements; i++) 00062 _chain[i]->call(); 00063 } 00064 00065 void CallChain::_check_size() { 00066 if (_elements < _size) 00067 return; 00068 _size = (_size < 4) ? 4 : _size + 4; 00069 pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); 00070 memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); 00071 delete _chain; 00072 _chain = new_chain; 00073 } 00074 00075 pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { 00076 _check_size(); 00077 _chain[_elements] = pf; 00078 _elements ++; 00079 return pf; 00080 } 00081 00082 pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { 00083 _check_size(); 00084 memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); 00085 _chain[0] = pf; 00086 _elements ++; 00087 return pf; 00088 } 00089 00090 } // namespace mbed
Generated on Tue Jul 12 2022 21:21:33 by
