mirror of
https://github.com/monero-project/monero.git
synced 2024-10-01 11:49:47 -04:00
add a once_a_time_milliseconds class
This commit is contained in:
parent
e282e9fa40
commit
85807dfb25
@ -230,35 +230,56 @@ namespace math_helper
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
template<int default_interval, bool start_immediate = true>
|
template<uint64_t scale, int default_interval, bool start_immediate = true>
|
||||||
class once_a_time_seconds
|
class once_a_time
|
||||||
{
|
{
|
||||||
|
uint64_t get_time() const
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
FILETIME fileTime;
|
||||||
|
GetSystemTimeAsFileTime(&fileTime);
|
||||||
|
unsigned __int64 present = 0;
|
||||||
|
present |= fileTime.dwHighDateTime;
|
||||||
|
present = present << 32;
|
||||||
|
present |= fileTime.dwLowDateTime;
|
||||||
|
present /= 10; // mic-sec
|
||||||
|
#else
|
||||||
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
return tv.tv_sec * 1000000 + tv.tv_usec;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
once_a_time_seconds():m_interval(default_interval)
|
once_a_time():m_interval(default_interval * scale)
|
||||||
{
|
{
|
||||||
m_last_worked_time = 0;
|
m_last_worked_time = 0;
|
||||||
if(!start_immediate)
|
if(!start_immediate)
|
||||||
time(&m_last_worked_time);
|
m_last_worked_time = get_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class functor_t>
|
template<class functor_t>
|
||||||
bool do_call(functor_t functr)
|
bool do_call(functor_t functr)
|
||||||
{
|
{
|
||||||
time_t current_time = 0;
|
uint64_t current_time = get_time();
|
||||||
time(¤t_time);
|
|
||||||
|
|
||||||
if(current_time - m_last_worked_time > m_interval)
|
if(current_time - m_last_worked_time > m_interval)
|
||||||
{
|
{
|
||||||
bool res = functr();
|
bool res = functr();
|
||||||
time(&m_last_worked_time);
|
m_last_worked_time = get_time();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
time_t m_last_worked_time;
|
uint64_t m_last_worked_time;
|
||||||
time_t m_interval;
|
uint64_t m_interval;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<int default_interval, bool start_immediate = true>
|
||||||
|
class once_a_time_seconds: public once_a_time<1000000, default_interval, start_immediate> {};
|
||||||
|
template<int default_interval, bool start_immediate = true>
|
||||||
|
class once_a_time_milliseconds: public once_a_time<1000, default_interval, start_immediate> {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user