forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_point.h
81 lines (60 loc) · 2.4 KB
/
time_point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FML_TIME_TIME_POINT_H_
#define FLUTTER_FML_TIME_TIME_POINT_H_
#include <cstdint>
#include <functional>
#include <iosfwd>
#include "flutter/fml/time/time_delta.h"
namespace fml {
// A TimePoint represents a point in time represented as an integer number of
// nanoseconds elapsed since an arbitrary point in the past.
//
// WARNING: This class should not be serialized across reboots, or across
// devices: the reference point is only stable for a given device between
// reboots.
class TimePoint {
public:
using ClockSource = TimePoint (*)();
// Default TimePoint with internal value 0 (epoch).
constexpr TimePoint() = default;
static void SetClockSource(ClockSource source);
static TimePoint Now();
static TimePoint CurrentWallTime();
static constexpr TimePoint Min() {
return TimePoint(std::numeric_limits<int64_t>::min());
}
static constexpr TimePoint Max() {
return TimePoint(std::numeric_limits<int64_t>::max());
}
static constexpr TimePoint FromEpochDelta(TimeDelta ticks) {
return TimePoint(ticks.ToNanoseconds());
}
// Expects ticks in nanos.
static constexpr TimePoint FromTicks(int64_t ticks) {
return TimePoint(ticks);
}
TimeDelta ToEpochDelta() const { return TimeDelta::FromNanoseconds(ticks_); }
// Compute the difference between two time points.
TimeDelta operator-(TimePoint other) const {
return TimeDelta::FromNanoseconds(ticks_ - other.ticks_);
}
TimePoint operator+(TimeDelta duration) const {
return TimePoint(ticks_ + duration.ToNanoseconds());
}
TimePoint operator-(TimeDelta duration) const {
return TimePoint(ticks_ - duration.ToNanoseconds());
}
bool operator==(TimePoint other) const { return ticks_ == other.ticks_; }
bool operator!=(TimePoint other) const { return ticks_ != other.ticks_; }
bool operator<(TimePoint other) const { return ticks_ < other.ticks_; }
bool operator<=(TimePoint other) const { return ticks_ <= other.ticks_; }
bool operator>(TimePoint other) const { return ticks_ > other.ticks_; }
bool operator>=(TimePoint other) const { return ticks_ >= other.ticks_; }
private:
explicit constexpr TimePoint(int64_t ticks) : ticks_(ticks) {}
int64_t ticks_ = 0;
};
} // namespace fml
#endif // FLUTTER_FML_TIME_TIME_POINT_H_