summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/gstreamer/common/qglist_helper_p.h
blob: 54108e1c3d521b98fe32ad031a1e961190a2eab6 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QGLIST_HELPER_P_H
#define QGLIST_HELPER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qtconfigmacros.h>

#include <glib.h>
#include <iterator>

QT_BEGIN_NAMESPACE

namespace QGstUtils {

template <typename ListType>
struct GListIterator
{
    explicit GListIterator(const GList *element = nullptr) : element(element) { }

    const ListType &operator*() const noexcept { return *operator->(); }
    const ListType *operator->() const noexcept
    {
        return reinterpret_cast<const ListType *>(&element->data);
    }

    GListIterator &operator++() noexcept
    {
        if (element)
            element = element->next;

        return *this;
    }
    GListIterator operator++(int n) noexcept
    {
        for (int i = 0; i != n; ++i)
            operator++();

        return *this;
    }

    bool operator==(const GListIterator &r) const noexcept { return element == r.element; }
    bool operator!=(const GListIterator &r) const noexcept { return element != r.element; }

    using difference_type = std::ptrdiff_t;
    using value_type = ListType;
    using pointer = value_type *;
    using reference = value_type &;
    using iterator_category = std::input_iterator_tag;

    const GList *element = nullptr;
};

template <typename ListType>
struct GListRangeAdaptor
{
    static_assert(std::is_pointer_v<ListType>);

    explicit GListRangeAdaptor(const GList *list) : head(list) { }

    auto begin() { return GListIterator<ListType>(head); }
    auto end() { return GListIterator<ListType>(nullptr); }

    const GList *head;
};

} // namespace QGstUtils

QT_END_NAMESPACE

#endif