MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/common/Delay.hh
2024-06-07 11:12:56 +03:00

39 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Delay.h
*
* Created on: 2 мая 2017 г.
* Author: titov
*/
#ifndef SOURCES_COMMON_DELAY_H_
#define SOURCES_COMMON_DELAY_H_
//! Звено задержки на кольцевом буфере.
//! T - Тип данных.
//! size - Размер буфера, должен быть кратен 2.
template<typename T, unsigned int size>
struct Delay {
private:
static const unsigned int mask = size - 1; //!< Маска защиты от переполнения.
T buff[size]; //!< Буфер задержиываемых элементов.
unsigned int index; //!< Текущий индекс в массиве.
public:
//! Оператор задержки.
T operator()(T item) {
const unsigned int prev_index(index);
buff[ ++index & mask ] = item;
return buff[ ( prev_index ) & mask ];
}
//! Оператор добавления данных.
Delay & operator<<(T item) {
buff[ ++index & mask ] = item;
return *this;
}
//! Оператор получения данных запаздывающих на произвольное количество тактов.
T operator[](int i) const {
return buff[ ( index - i ) & mask ];
}
};
#endif /* SOURCES_COMMON_DELAY_H_ */