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

40 lines
639 B
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.

/*
* 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_ */