summaryrefslogtreecommitdiffstats
path: root/src/multimedia/qmediatimerange.h
blob: b61ed9c59437c39a42a11c10ed32936f6bdddde1 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QMEDIATIMERANGE_H
#define QMEDIATIMERANGE_H

#include <QtMultimedia/qtmultimediaglobal.h>
#include <QtCore/qshareddata.h>

QT_BEGIN_NAMESPACE


class QMediaTimeRangePrivate;

QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QMediaTimeRangePrivate, Q_MULTIMEDIA_EXPORT)

class Q_MULTIMEDIA_EXPORT QMediaTimeRange
{
public:
    struct Interval
    {
        constexpr Interval() noexcept = default;
        explicit constexpr Interval(qint64 start, qint64 end) noexcept
        : s(start), e(end)
        {}

        constexpr qint64 start() const noexcept { return s; }
        constexpr qint64 end() const noexcept { return e; }

        constexpr bool contains(qint64 time) const noexcept
        {
            return isNormal() ? (s <= time && time <= e)
                : (e <= time && time <= s);
        }

        constexpr bool isNormal() const noexcept { return s <= e; }
        constexpr Interval normalized() const
        {
            return s > e ? Interval(e, s) : *this;
        }
        constexpr Interval translated(qint64 offset) const
        {
            return Interval(s + offset, e + offset);
        }

        friend constexpr bool operator==(Interval lhs, Interval rhs) noexcept
        {
            return lhs.start() == rhs.start() && lhs.end() == rhs.end();
        }
        friend constexpr bool operator!=(Interval lhs, Interval rhs) noexcept
        {
            return lhs.start() != rhs.start() || lhs.end() != rhs.end();
        }

    private:
        friend class QMediaTimeRangePrivate;
        qint64 s = 0;
        qint64 e = 0;
    };

    QMediaTimeRange();
    explicit QMediaTimeRange(qint64 start, qint64 end);
    QMediaTimeRange(const Interval&);
    QMediaTimeRange(const QMediaTimeRange &range) noexcept;
    ~QMediaTimeRange();

    QMediaTimeRange &operator=(const QMediaTimeRange&) noexcept;

    QMediaTimeRange(QMediaTimeRange &&other) noexcept = default;
    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QMediaTimeRange)
    void swap(QMediaTimeRange &other) noexcept
    { qSwap(d, other.d); }
    void detach();

    QMediaTimeRange &operator=(const Interval&);

    qint64 earliestTime() const;
    qint64 latestTime() const;

    QList<QMediaTimeRange::Interval> intervals() const;
    bool isEmpty() const;
    bool isContinuous() const;

    bool contains(qint64 time) const;

    void addInterval(qint64 start, qint64 end);
    void addInterval(const Interval &interval);
    void addTimeRange(const QMediaTimeRange&);

    void removeInterval(qint64 start, qint64 end);
    void removeInterval(const Interval &interval);
    void removeTimeRange(const QMediaTimeRange&);

    QMediaTimeRange& operator+=(const QMediaTimeRange&);
    QMediaTimeRange& operator+=(const Interval&);
    QMediaTimeRange& operator-=(const QMediaTimeRange&);
    QMediaTimeRange& operator-=(const Interval&);

    void clear();

    friend inline bool operator==(const QMediaTimeRange &lhs, const QMediaTimeRange &rhs)
    { return lhs.intervals() == rhs.intervals(); }
    friend inline bool operator!=(const QMediaTimeRange &lhs, const QMediaTimeRange &rhs)
    { return lhs.intervals() != rhs.intervals(); }

private:
    QExplicitlySharedDataPointer<QMediaTimeRangePrivate> d;
};

#ifndef QT_NO_DEBUG_STREAM
Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, const QMediaTimeRange::Interval &);
Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, const QMediaTimeRange &);
#endif

inline QMediaTimeRange operator+(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
{ return (QMediaTimeRange(r1) += r2); }
inline QMediaTimeRange operator-(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
{ return (QMediaTimeRange(r1) -= r2); }

Q_DECLARE_SHARED(QMediaTimeRange)

QT_END_NAMESPACE

Q_DECLARE_METATYPE(QMediaTimeRange)
Q_DECLARE_METATYPE(QMediaTimeRange::Interval)

#endif  // QMEDIATIMERANGE_H