66 lines
1.1 KiB
C++
66 lines
1.1 KiB
C++
/*
|
||
* 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_ */
|