danicoin/src/CryptoNoteCore/OnceInInterval.h

37 lines
712 B
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-09-15 10:48:45 +00:00
#pragma once
2015-05-27 12:08:46 +00:00
#include <time.h>
2015-05-27 12:08:46 +00:00
namespace CryptoNote
{
2014-09-15 10:48:45 +00:00
2015-05-27 12:08:46 +00:00
class OnceInInterval {
2014-09-15 10:48:45 +00:00
public:
2015-05-27 12:08:46 +00:00
OnceInInterval(unsigned interval, bool startNow = true)
: m_interval(interval), m_lastCalled(startNow ? 0 : time(nullptr)) {}
template<class F>
bool call(F func) {
time_t currentTime = time(nullptr);
if (currentTime - m_lastCalled > m_interval) {
bool res = func();
time(&m_lastCalled);
return res;
}
return true;
}
2014-09-15 10:48:45 +00:00
private:
2015-05-27 12:08:46 +00:00
time_t m_lastCalled;
time_t m_interval;
2014-09-15 10:48:45 +00:00
};
}