MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/common/DoubleBuffer.hpp

40 lines
639 B
C++
Raw Normal View History

2024-06-07 11:12:56 +03:00
/*
* DoubleBuffer.hpp
*
* Created on: 15 мар. 2021 г.
* Author: titov
*/
#ifndef UMLIBRARY_COMMON_DOUBLEBUFFER_HPP_
#define UMLIBRARY_COMMON_DOUBLEBUFFER_HPP_
#include <utility>
template<typename V>
class DoubleBuffer {
bool flag = false;
std::pair<V, V> data;
public:
void write( V v );
V read() const;
};
template<typename V>
inline void DoubleBuffer<V>::write( V v ) {
( flag ? data.second : data.first ) = v;
flag = not flag;
}
template<typename V>
inline V DoubleBuffer<V>::read() const {
return flag ? data.first : data.second;
}
#endif /* UMLIBRARY_COMMON_DOUBLEBUFFER_HPP_ */