Last active
August 9, 2016 02:19
-
-
Save hunje/d40c350d42a88e9c5580abad26846e19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <chrono> | |
/** | |
* return true, during specifed seconds from latest call. | |
* @author Cho Hunje <akahun522@gmail.com> | |
*/ | |
template <int S> | |
class TimedBlock { | |
private: | |
int duration = S; | |
std::chrono::steady_clock::time_point latest; | |
public: | |
TimedBlock() { | |
} | |
void clear() { | |
latest = std::chrono::steady_clock::now() - std::chrono::seconds(duration); | |
} | |
bool taken() { | |
auto elapsed = | |
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - latest); | |
if (elapsed.count() < duration) { | |
return true; | |
} | |
latest = std::chrono::steady_clock::now(); | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
指定した時間ないに呼ばれるとtrueを返します。主に連打を防ぐために使います。