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 __disable_irq(); 00042 for(int i = 0; i < _elements; i ++) { 00043 delete _chain[i]; 00044 _chain[i] = NULL; 00045 } 00046 _elements = 0; 00047 __enable_irq(); 00048 } 00049 00050 bool CallChain::remove(pFunctionPointer_t f) { 00051 int i; 00052 00053 if ((i = find(f)) == -1) 00054 return false; 00055 __disable_irq(); 00056 if (i != _elements - 1) 00057 memmove(_chain + i, _chain + i + 1, (_elements - i - 1) * sizeof(pFunctionPointer_t)); 00058 delete f; 00059 _elements --; 00060 __enable_irq(); 00061 return true; 00062 } 00063 00064 void CallChain::call() { 00065 for(int i = 0; i < _elements; i++) 00066 _chain[i]->call(); 00067 } 00068 00069 void CallChain::_check_size() { 00070 if (_elements < _size) 00071 return; 00072 __disable_irq(); 00073 _size = (_size < 4) ? 4 : _size + 4; 00074 pFunctionPointer_t* new_chain = new pFunctionPointer_t[_size](); 00075 memcpy(new_chain, _chain, _elements * sizeof(pFunctionPointer_t)); 00076 delete _chain; 00077 _chain = new_chain; 00078 __enable_irq(); 00079 } 00080 00081 pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { 00082 _check_size(); 00083 _chain[_elements] = pf; 00084 _elements ++; 00085 return pf; 00086 } 00087 00088 pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { 00089 _check_size(); 00090 __disable_irq(); 00091 memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); 00092 _chain[0] = pf; 00093 _elements ++; 00094 __enable_irq(); 00095 return pf; 00096 } 00097 00098 } // namespace mbed
Generated on Tue Jul 12 2022 20:46:36 by
1.7.2
