From be237b6ab5f4e3ec875d11359b71d4cbb543314d Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Tue, 4 Jun 2024 13:14:39 -0700 Subject: [PATCH] core/elapsedtimer: add ElapsedTimer --- src/core/CMakeLists.txt | 1 + src/core/elapsedtimer.cpp | 22 +++++++++++++++++++ src/core/elapsedtimer.hpp | 45 +++++++++++++++++++++++++++++++++++++++ src/core/module.md | 1 + 4 files changed, 69 insertions(+) create mode 100644 src/core/elapsedtimer.cpp create mode 100644 src/core/elapsedtimer.hpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 88c26241..24d2e685 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -27,6 +27,7 @@ qt_add_library(quickshell-core STATIC transformwatcher.cpp boundcomponent.cpp model.cpp + elapsedtimer.cpp ) set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS GIT_REVISION="${GIT_REVISION}") diff --git a/src/core/elapsedtimer.cpp b/src/core/elapsedtimer.cpp new file mode 100644 index 00000000..91321122 --- /dev/null +++ b/src/core/elapsedtimer.cpp @@ -0,0 +1,22 @@ +#include "elapsedtimer.hpp" + +#include + +ElapsedTimer::ElapsedTimer() { this->timer.start(); } + +qreal ElapsedTimer::elapsed() { return static_cast(this->elapsedNs()) / 1000000000.0; } + +qreal ElapsedTimer::restart() { return static_cast(this->restartNs()) / 1000000000.0; } + +qint64 ElapsedTimer::elapsedMs() { return this->timer.elapsed(); } + +qint64 ElapsedTimer::restartMs() { return this->timer.restart(); } + +qint64 ElapsedTimer::elapsedNs() { return this->timer.nsecsElapsed(); } + +qint64 ElapsedTimer::restartNs() { + // see qelapsedtimer.cpp + auto old = this->timer; + this->timer.start(); + return old.durationTo(this->timer).count(); +} diff --git a/src/core/elapsedtimer.hpp b/src/core/elapsedtimer.hpp new file mode 100644 index 00000000..85850963 --- /dev/null +++ b/src/core/elapsedtimer.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include +#include + +///! Measures time between events +/// The ElapsedTimer measures time since its last restart, and is useful +/// for determining the time between events that don't supply it. +class ElapsedTimer: public QObject { + Q_OBJECT; + QML_ELEMENT; + +public: + explicit ElapsedTimer(); + + /// Return the number of seconds since the timer was last + /// started or restarted, with nanosecond precision. + Q_INVOKABLE qreal elapsed(); + + /// Restart the timer, returning the number of seconds since + /// the timer was last started or restarted, with nanosecond precision. + Q_INVOKABLE qreal restart(); + + /// Return the number of milliseconds since the timer was last + /// started or restarted. + Q_INVOKABLE qint64 elapsedMs(); + + /// Restart the timer, returning the number of milliseconds since + /// the timer was last started or restarted. + Q_INVOKABLE qint64 restartMs(); + + /// Return the number of nanoseconds since the timer was last + /// started or restarted. + Q_INVOKABLE qint64 elapsedNs(); + + /// Restart the timer, returning the number of nanoseconds since + /// the timer was last started or restarted. + Q_INVOKABLE qint64 restartNs(); + +private: + QElapsedTimer timer; +}; diff --git a/src/core/module.md b/src/core/module.md index dc1f204d..13218610 100644 --- a/src/core/module.md +++ b/src/core/module.md @@ -19,5 +19,6 @@ headers = [ "transformwatcher.hpp", "boundcomponent.hpp", "model.hpp", + "elapsedtimer.hpp", ] -----