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

66 lines
1.1 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.

/*
* Counter.hpp
*
* Created on: 22 июл. 2019 г.
* Author: titov
*/
#ifndef SOURCE_COMMON_COUNTER_HPP_
#define SOURCE_COMMON_COUNTER_HPP_
template<typename T>
struct Counter {
Counter( T max_value = T() );
explicit operator bool() const;
void operator++();
void operator=( T rh );
Counter<T> & operator=( const Counter<T> & rh );
T value() const { return current_value; }
private:
T max_value;
T current_value;
};
template<typename T>
inline Counter<T>::Counter( T max_value ) : max_value(max_value), current_value(0) {}
template<typename T>
inline Counter<T>::operator bool() const {
return current_value == max_value;
}
template<typename T>
inline void Counter<T>::operator++() {
if( current_value < max_value )
++current_value;
}
template<typename T>
inline void Counter<T>::operator=( T rh ) {
current_value = rh;
}
template<typename T>
inline Counter<T> & Counter<T>::operator=( const Counter<T> & rh ) {
max_value = rh.max_value;
current_value = rh.current_value;
return *this;
}
#endif /* SOURCE_COMMON_COUNTER_HPP_ */