add uv::Clock
This commit is contained in:
parent
4532d321c3
commit
a7411c1154
@ -18,6 +18,7 @@ target_sources(nf7_core
|
||||
luajit/lambda.hh
|
||||
luajit/thread.hh
|
||||
uv/context.hh
|
||||
uv/clock.hh
|
||||
clock.hh
|
||||
logger.hh
|
||||
version.hh
|
||||
|
40
core/uv/clock.hh
Normal file
40
core/uv/clock.hh
Normal file
@ -0,0 +1,40 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "iface/subsys/clock.hh"
|
||||
#include "iface/env.hh"
|
||||
|
||||
#include "core/uv/context.hh"
|
||||
#include "core/clock.hh"
|
||||
|
||||
namespace nf7::core::uv {
|
||||
|
||||
class Clock : public subsys::Clock {
|
||||
public:
|
||||
explicit Clock(Env& env)
|
||||
: subsys::Clock("nf7::core::uv::Clock"),
|
||||
ctx_(env.Get<Context>()) { }
|
||||
|
||||
public:
|
||||
void Reset(Time now = core::Clock::GetCurrentTime()) noexcept {
|
||||
epoch_ = now - std::chrono::milliseconds(ctx_->loop()->now());
|
||||
}
|
||||
|
||||
Time now() const noexcept override {
|
||||
if (std::nullopt == epoch_) {
|
||||
const_cast<Clock&>(*this).Reset();
|
||||
}
|
||||
return *epoch_ + std::chrono::milliseconds(ctx_->loop()->now());
|
||||
}
|
||||
|
||||
private:
|
||||
const std::shared_ptr<Context> ctx_;
|
||||
|
||||
std::optional<Time> epoch_;
|
||||
};
|
||||
|
||||
} // namespace nf7::core::uv
|
Loading…
x
Reference in New Issue
Block a user