Skip to content

Instantly share code, notes, and snippets.

@hunje
Last active August 9, 2016 02:19
Show Gist options
  • Save hunje/d40c350d42a88e9c5580abad26846e19 to your computer and use it in GitHub Desktop.
Save hunje/d40c350d42a88e9c5580abad26846e19 to your computer and use it in GitHub Desktop.
#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;
}
};
@hunje
Copy link
Author

hunje commented Aug 9, 2016

指定した時間ないに呼ばれるとtrueを返します。主に連打を防ぐために使います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment