add subsys::Clock
This commit is contained in:
parent
71046a14ef
commit
6413588df6
@ -16,6 +16,7 @@ target_sources(nf7_core
|
||||
luajit/context.hh
|
||||
luajit/lambda.hh
|
||||
luajit/thread.hh
|
||||
clock.hh
|
||||
version.hh
|
||||
)
|
||||
|
||||
@ -27,6 +28,7 @@ target_sources(nf7_core_test
|
||||
luajit/lambda_test.cc
|
||||
luajit/thread_test.cc
|
||||
luajit/thread_test.hh
|
||||
clock_test.cc
|
||||
)
|
||||
target_link_libraries(nf7_core_test
|
||||
PRIVATE
|
||||
|
27
core/clock.hh
Normal file
27
core/clock.hh
Normal file
@ -0,0 +1,27 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include "iface/subsys/clock.hh"
|
||||
|
||||
namespace nf7::core {
|
||||
|
||||
class Clock : public subsys::Clock {
|
||||
public:
|
||||
static Time GetCurrentTime() noexcept {
|
||||
return std::chrono::time_point_cast<
|
||||
Resolution>(std::chrono::system_clock::now());
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Clock(Time now = GetCurrentTime()) noexcept
|
||||
: subsys::Clock("Clock"), now_(now) { }
|
||||
|
||||
void Tick(Time now = GetCurrentTime()) noexcept { now_ = now; }
|
||||
|
||||
Time now() const noexcept { return now_; }
|
||||
|
||||
private:
|
||||
Time now_;
|
||||
};
|
||||
|
||||
} // namespace nf7::core
|
20
core/clock_test.cc
Normal file
20
core/clock_test.cc
Normal file
@ -0,0 +1,20 @@
|
||||
// No copyright
|
||||
#include "core/clock.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
TEST(Clock, now) {
|
||||
nf7::core::Clock sut {nf7::core::Clock::Time {0ms}};
|
||||
EXPECT_EQ(sut.now(), nf7::core::Clock::Time {0ms});
|
||||
}
|
||||
|
||||
TEST(Clock, Tick) {
|
||||
nf7::core::Clock sut {nf7::core::Clock::Time {0ms}};
|
||||
sut.Tick(nf7::core::Clock::Time {1ms});
|
||||
EXPECT_EQ(sut.now(), nf7::core::Clock::Time {1ms});
|
||||
}
|
21
iface/subsys/clock.hh
Normal file
21
iface/subsys/clock.hh
Normal file
@ -0,0 +1,21 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "iface/subsys/interface.hh"
|
||||
|
||||
|
||||
namespace nf7::subsys {
|
||||
|
||||
class Clock : public Interface {
|
||||
public:
|
||||
using Resolution = std::chrono::milliseconds;
|
||||
using Time = std::chrono::sys_time<Resolution>;
|
||||
|
||||
using Interface::Interface;
|
||||
|
||||
virtual Time now() const noexcept = 0;
|
||||
};
|
||||
|
||||
} // namespace nf7::subsys
|
Loading…
x
Reference in New Issue
Block a user