| 1 | // Copyright (C) 2024 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 QGLIST_HELPER_P_H |
| 5 | #define QGLIST_HELPER_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 <QtCore/qtconfigmacros.h> |
| 19 | |
| 20 | #include <glib.h> |
| 21 | #include <iterator> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | namespace QGstUtils { |
| 26 | |
| 27 | template <typename ListType, bool IsConst> |
| 28 | struct GListIterator |
| 29 | { |
| 30 | using GListType = std::conditional_t<IsConst, const GList *, GList *>; |
| 31 | |
| 32 | explicit GListIterator(GListType element = nullptr) : element(element) { } |
| 33 | |
| 34 | const ListType &operator*() const noexcept { return *operator->(); } |
| 35 | const ListType *operator->() const noexcept |
| 36 | { |
| 37 | return reinterpret_cast<const ListType *>(&element->data); |
| 38 | } |
| 39 | |
| 40 | GListIterator &operator++() noexcept |
| 41 | { |
| 42 | if (element) |
| 43 | element = element->next; |
| 44 | |
| 45 | return *this; |
| 46 | } |
| 47 | GListIterator operator++(int n) noexcept |
| 48 | { |
| 49 | for (int i = 0; i != n; ++i) |
| 50 | operator++(); |
| 51 | |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | bool operator==(const GListIterator &r) const noexcept { return element == r.element; } |
| 56 | bool operator!=(const GListIterator &r) const noexcept { return element != r.element; } |
| 57 | |
| 58 | using difference_type = std::ptrdiff_t; |
| 59 | using value_type = ListType; |
| 60 | using pointer = value_type *; |
| 61 | using reference = value_type &; |
| 62 | using iterator_category = std::input_iterator_tag; |
| 63 | |
| 64 | GListType element = nullptr; |
| 65 | }; |
| 66 | |
| 67 | template <typename ListType, bool IsConst> |
| 68 | struct GListRangeAdaptorImpl |
| 69 | { |
| 70 | static_assert(std::is_pointer_v<ListType>); |
| 71 | |
| 72 | using GListType = std::conditional_t<IsConst, const GList *, GList *>; |
| 73 | |
| 74 | explicit GListRangeAdaptorImpl(GListType list) : head(list) { } |
| 75 | |
| 76 | auto begin() { return GListIterator<ListType, IsConst>(head); } |
| 77 | auto end() { return GListIterator<ListType, IsConst>(nullptr); } |
| 78 | |
| 79 | GListType head; |
| 80 | }; |
| 81 | |
| 82 | template <typename ListType> |
| 83 | using GListRangeAdaptor = GListRangeAdaptorImpl<ListType, false>; |
| 84 | template <typename ListType> |
| 85 | using GListConstRangeAdaptor = GListRangeAdaptorImpl<ListType, true>; |
| 86 | |
| 87 | } // namespace QGstUtils |
| 88 | |
| 89 | QT_END_NAMESPACE |
| 90 | |
| 91 | #endif |
| 92 | |