From ef8d0bc5277be6b55d99394133c6ca3361e4361e Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Sat, 29 Jul 2017 19:48:40 +0300 Subject: [PATCH] add timer --- src/common.h | 1 + src/timer.hpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/timer.hpp diff --git a/src/common.h b/src/common.h index 53c166de..e739c8d9 100644 --- a/src/common.h +++ b/src/common.h @@ -34,6 +34,7 @@ #include #include +#include "timer.hpp" #include "averager.hpp" #include "aftercheaders.h" diff --git a/src/timer.hpp b/src/timer.hpp new file mode 100644 index 00000000..e995da60 --- /dev/null +++ b/src/timer.hpp @@ -0,0 +1,36 @@ +/* + * timer.hpp + * + * Created on: Jul 29, 2017 + * Author: nullifiedcat + */ + +#pragma once + +#include + +class Timer { +public: + typedef std::chrono::system_clock clock; + + inline Timer() {}; + + inline bool check(unsigned ms) const { + return std::chrono::duration_cast(clock::now() - last).count() >= ms; + } + inline bool test_and_set(unsigned ms) { + if (check(ms)) { + update(); + return true; + } + return false; + } + inline void update() { + last = clock::now(); + } + +public: + std::chrono::time_point last {}; +}; + +