mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
We currently use a mix of C standard includes (e.g. limits.h) and their C++ variants (e.g. climits). This migrates to a consistent style for all cases where the C++ variants are acceptable, but leaves the C equivalents in place where they are required, such as in the embedder API and other headers that may be used from C.
70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
// 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 <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:
|
|
// Default TimePoint with internal value 0 (epoch).
|
|
constexpr TimePoint() = default;
|
|
|
|
static TimePoint Now();
|
|
|
|
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());
|
|
}
|
|
|
|
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_
|