1 | // Copyright (C) 2025 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTAGGEDTIME_P_H |
5 | #define QTAGGEDTIME_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qcompare.h" |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | template <typename ValueType, typename ThisType, typename AmendedType = ThisType> |
23 | class BaseTime |
24 | { |
25 | public: |
26 | constexpr ValueType get() const noexcept { return m_value; } |
27 | |
28 | constexpr BaseTime(ValueType value) noexcept : m_value(value) { } |
29 | constexpr BaseTime(const ThisType &other) noexcept : m_value(other.m_value) { } |
30 | |
31 | constexpr ThisType &operator=(const ThisType &other) noexcept |
32 | { |
33 | m_value = other.m_value; |
34 | return static_cast<ThisType &>(*this); |
35 | } |
36 | |
37 | friend bool comparesEqual(const ThisType &lhs, const ThisType &rhs) noexcept |
38 | { |
39 | return lhs.m_value == rhs.m_value; |
40 | } |
41 | |
42 | friend constexpr Qt::strong_ordering compareThreeWay(const ThisType &lhs, |
43 | const ThisType &rhs) noexcept |
44 | { |
45 | return qCompareThreeWay(lhs.m_value, rhs.m_value); |
46 | } |
47 | |
48 | Q_DECLARE_STRONGLY_ORDERED(ThisType); |
49 | |
50 | constexpr ThisType operator-() const { return ThisType(-m_value); } |
51 | |
52 | friend constexpr ThisType operator+(const ThisType &lhs, const AmendedType &rhs) noexcept |
53 | { |
54 | return ThisType(lhs.m_value + rhs.get()); |
55 | } |
56 | |
57 | template <typename T = ThisType, std::enable_if_t<!std::is_same_v<AmendedType, T>> * = nullptr> |
58 | friend constexpr ThisType operator+(const AmendedType &lhs, const ThisType &rhs) noexcept |
59 | { |
60 | return ThisType(lhs.get() + rhs.m_value); |
61 | } |
62 | |
63 | friend constexpr ThisType operator-(const ThisType &lhs, const AmendedType &rhs) noexcept |
64 | { |
65 | return ThisType(lhs.m_value - rhs.get()); |
66 | } |
67 | |
68 | friend constexpr ThisType &operator+=(ThisType &lhs, const AmendedType &rhs) noexcept |
69 | { |
70 | lhs.m_value += rhs.get(); |
71 | return lhs; |
72 | } |
73 | |
74 | friend constexpr ThisType &operator-=(ThisType &lhs, const AmendedType &rhs) noexcept |
75 | { |
76 | lhs.m_value -= rhs.get(); |
77 | return lhs; |
78 | } |
79 | |
80 | private: |
81 | ValueType m_value; |
82 | }; |
83 | |
84 | template <typename ValueType, typename Tag> |
85 | class QTaggedTimePoint; |
86 | |
87 | template <typename ValueType, typename Tag> |
88 | class QTaggedDuration : public BaseTime<ValueType, QTaggedDuration<ValueType, Tag>> |
89 | { |
90 | public: |
91 | using TimePoint = QTaggedTimePoint<ValueType, Tag>; |
92 | using BaseTime<ValueType, QTaggedDuration>::BaseTime; |
93 | |
94 | constexpr TimePoint asTimePoint() const noexcept { return TimePoint(this->get()); } |
95 | }; |
96 | |
97 | template <typename ValueType, typename Tag> |
98 | class QTaggedTimePoint |
99 | : public BaseTime<ValueType, QTaggedTimePoint<ValueType, Tag>, QTaggedDuration<ValueType, Tag>> |
100 | { |
101 | public: |
102 | using Duration = QTaggedDuration<ValueType, Tag>; |
103 | using Base = BaseTime<ValueType, QTaggedTimePoint, Duration>; |
104 | using Base::Base; |
105 | |
106 | friend constexpr Duration operator-(const QTaggedTimePoint &lhs, |
107 | const QTaggedTimePoint &rhs) noexcept |
108 | { |
109 | return Duration(lhs.get() - rhs.get()); |
110 | } |
111 | |
112 | constexpr Duration asDuration() const noexcept { return Duration(this->get()); } |
113 | }; |
114 | |
115 | QT_END_NAMESPACE |
116 | |
117 | #endif // QTAGGEDTIME_P_H |
118 | |