#ifndef __AVERAGEVALUE_HPP__ #define __AVERAGEVALUE_HPP__ /* ================================================================================ averageValue.hpp v. 0.5.1.a © sam0delk1n, 2015 ================================================================================ */ #include #include #define PURE_FUNCTION namespace boolean { /* ================================================================================ averageValue ================================================================================ */ template< typename T > class averageValue final { public: uint32_t msecDeltaTime { 1000 }; auto update( const T& addValue ) ->void; auto isReadyToPrint( void ) const noexcept ->bool; auto getValuePerTime( const bool rogerThat ) const ->const T&; auto getAverageValue( const bool rogerThat ) const ->const T&; auto operator=( const averageValue& ) ->averageValue& = delete; averageValue( void ) = default; averageValue( const averageValue& ) = delete; ~averageValue( void ) = default; private: uint32_t msecPrintLastTime { 0 }; mutable bool readyToPrint { false }; uint32_t iterations { 0 }; T valuePerTime { static_cast< T >( 0 ) }; T lastValuePerTime { static_cast< T >( 0 ) }; T lastAverageValue { static_cast< T >( 0 ) }; static auto PURE_FUNCTION timespecToMilliseconds( const timespec& tspec ) noexcept ->uint32_t; }; /* ================================================================================ averageValue::update ================================================================================ */ template< typename T > auto averageValue< T >::update( const T& addValue ) ->void { valuePerTime += addValue; ++iterations; timespec tspecCurrentTime; clock_gettime( CLOCK_MONOTONIC, &tspecCurrentTime ); const uint32_t msecCurrentTime { timespecToMilliseconds( tspecCurrentTime ) }; if ( msecCurrentTime - msecPrintLastTime >= msecDeltaTime ) { msecPrintLastTime = msecCurrentTime; readyToPrint = true; lastValuePerTime = valuePerTime; lastAverageValue = valuePerTime / iterations; iterations = 0; valuePerTime = 0; } } /* ================================================================================ averageValue::isReadyToPrint ================================================================================ */ template< typename T > inline auto averageValue< T >::isReadyToPrint( void ) const noexcept ->bool { return readyToPrint; } /* ================================================================================ averageValue::getValuePerTime ================================================================================ */ template< typename T > auto averageValue< T >::getValuePerTime( const bool rogerThat ) const ->const T& { readyToPrint = !rogerThat; return lastValuePerTime; } /* ================================================================================ averageValue::getAverageValue ================================================================================ */ template< typename T > auto averageValue< T >::getAverageValue( const bool rogerThat ) const ->const T& { readyToPrint = !rogerThat; return lastAverageValue; } /* ================================================================================ averageValue::timespecToMilliseconds ================================================================================ */ template< typename T > auto PURE_FUNCTION averageValue< T >::timespecToMilliseconds( const timespec& tspec ) noexcept ->uint32_t { uint32_t msec { 0 }; if ( tspec.tv_sec > 0 ) msec += 1000 * tspec.tv_sec; if ( tspec.tv_nsec > 0 ) msec += tspec.tv_nsec / 1000000; return msec; } }/*namespace boolean*/ #endif/*__AVERAGEVALUE_HPP__*/