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 {}; +}; + +