esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
chrono.hpp
Go to the documentation of this file.
1#ifndef NDNPH_PORT_CLOCK_CHRONO_HPP
2#define NDNPH_PORT_CLOCK_CHRONO_HPP
3
4#include "../../core/common.hpp"
5
6#include <chrono>
7#include <thread>
8
9namespace ndnph {
10namespace port_clock_chrono {
11
13class Clock {
14public:
15 Clock() = delete;
16
17 using Time = std::chrono::steady_clock::time_point;
18
19 static Time now() {
20 return std::chrono::steady_clock::now();
21 }
22
23 static Time add(Time t, int ms) {
24 return Time(t.time_since_epoch() +
25 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
26 std::chrono::milliseconds(ms)));
27 }
28
29 static int sub(Time a, Time b) {
30 return std::chrono::duration_cast<std::chrono::milliseconds>(a - b).count();
31 }
32
33 static bool isBefore(Time a, Time b) {
34 return a < b;
35 }
36
37 static void sleep(int ms) {
38#ifdef NDNPH_PORT_CHRONO_BUSY_SLEEP
39 Time end = add(now(), ms);
40 while (isBefore(now(), end)) {
41 }
42#else
43 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
44#endif
45 }
46};
47
48} // namespace port_clock_chrono
49
50#ifdef NDNPH_PORT_CLOCK_CHRONO
51namespace port {
52using Clock = port_clock_chrono::Clock;
53} // namespace port
54#endif
55
56} // namespace ndnph
57
58#endif // NDNPH_PORT_CLOCK_CHRONO_HPP
Clock implemented with std::chrono.
Definition chrono.hpp:13
std::chrono::steady_clock::time_point Time
Definition chrono.hpp:17
static Time now()
Definition chrono.hpp:19
static Time add(Time t, int ms)
Definition chrono.hpp:23
static void sleep(int ms)
Definition chrono.hpp:37
static int sub(Time a, Time b)
Definition chrono.hpp:29
static bool isBefore(Time a, Time b)
Definition chrono.hpp:33
port_clock_ino::Clock Clock
Definition ino.hpp:59
Definition fs.hpp:33