1// Copyright (C) 2016 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 QMEDIATIMERANGE_H
5#define QMEDIATIMERANGE_H
6
7#include <QtMultimedia/qtmultimediaglobal.h>
8#include <QtCore/qshareddata.h>
9#include <QtCore/qlist.h>
10#include <QtCore/qmetatype.h>
11
12QT_BEGIN_NAMESPACE
13
14
15class QMediaTimeRangePrivate;
16
17QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QMediaTimeRangePrivate, Q_MULTIMEDIA_EXPORT)
18
19class Q_MULTIMEDIA_EXPORT QMediaTimeRange
20{
21public:
22 struct Interval
23 {
24 constexpr Interval() noexcept = default;
25 explicit constexpr Interval(qint64 start, qint64 end) noexcept
26 : s(start), e(end)
27 {}
28
29 constexpr qint64 start() const noexcept { return s; }
30 constexpr qint64 end() const noexcept { return e; }
31
32 constexpr bool contains(qint64 time) const noexcept
33 {
34 return isNormal() ? (s <= time && time <= e)
35 : (e <= time && time <= s);
36 }
37
38 constexpr bool isNormal() const noexcept { return s <= e; }
39 constexpr Interval normalized() const
40 {
41 return s > e ? Interval(e, s) : *this;
42 }
43 constexpr Interval translated(qint64 offset) const
44 {
45 return Interval(s + offset, e + offset);
46 }
47
48 friend constexpr bool operator==(Interval lhs, Interval rhs) noexcept
49 {
50 return lhs.start() == rhs.start() && lhs.end() == rhs.end();
51 }
52 friend constexpr bool operator!=(Interval lhs, Interval rhs) noexcept
53 {
54 return lhs.start() != rhs.start() || lhs.end() != rhs.end();
55 }
56
57 private:
58 friend class QMediaTimeRangePrivate;
59 qint64 s = 0;
60 qint64 e = 0;
61 };
62
63 QMediaTimeRange();
64 explicit QMediaTimeRange(qint64 start, qint64 end);
65 QMediaTimeRange(const Interval&);
66 QMediaTimeRange(const QMediaTimeRange &range) noexcept;
67 ~QMediaTimeRange();
68
69 QMediaTimeRange &operator=(const QMediaTimeRange&) noexcept;
70
71 QMediaTimeRange(QMediaTimeRange &&other) noexcept = default;
72 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QMediaTimeRange)
73 void swap(QMediaTimeRange &other) noexcept
74 { d.swap(other&: other.d); }
75 void detach();
76
77 QMediaTimeRange &operator=(const Interval&);
78
79 qint64 earliestTime() const;
80 qint64 latestTime() const;
81
82 QList<QMediaTimeRange::Interval> intervals() const;
83 bool isEmpty() const;
84 bool isContinuous() const;
85
86 bool contains(qint64 time) const;
87
88 void addInterval(qint64 start, qint64 end);
89 void addInterval(const Interval &interval);
90 void addTimeRange(const QMediaTimeRange&);
91
92 void removeInterval(qint64 start, qint64 end);
93 void removeInterval(const Interval &interval);
94 void removeTimeRange(const QMediaTimeRange&);
95
96 QMediaTimeRange& operator+=(const QMediaTimeRange&);
97 QMediaTimeRange& operator+=(const Interval&);
98 QMediaTimeRange& operator-=(const QMediaTimeRange&);
99 QMediaTimeRange& operator-=(const Interval&);
100
101 void clear();
102
103 friend inline bool operator==(const QMediaTimeRange &lhs, const QMediaTimeRange &rhs)
104 { return lhs.intervals() == rhs.intervals(); }
105 friend inline bool operator!=(const QMediaTimeRange &lhs, const QMediaTimeRange &rhs)
106 { return lhs.intervals() != rhs.intervals(); }
107
108private:
109 QExplicitlySharedDataPointer<QMediaTimeRangePrivate> d;
110};
111
112#ifndef QT_NO_DEBUG_STREAM
113Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, const QMediaTimeRange::Interval &);
114Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, const QMediaTimeRange &);
115#endif
116
117inline QMediaTimeRange operator+(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
118{ return (QMediaTimeRange(r1) += r2); }
119inline QMediaTimeRange operator-(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
120{ return (QMediaTimeRange(r1) -= r2); }
121
122Q_DECLARE_SHARED(QMediaTimeRange)
123
124QT_END_NAMESPACE
125
126Q_DECLARE_METATYPE(QMediaTimeRange)
127Q_DECLARE_METATYPE(QMediaTimeRange::Interval)
128
129#endif // QMEDIATIMERANGE_H
130

source code of qtmultimedia/src/multimedia/qmediatimerange.h