| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com> |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QPROTOBUFSELFCHECKITERATOR_P_H |
| 6 | #define QPROTOBUFSELFCHECKITERATOR_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtProtobuf/qtprotobufexports.h> |
| 20 | |
| 21 | #include <QtCore/qnumeric.h> |
| 22 | #include <QtCore/qbytearray.h> |
| 23 | |
| 24 | #include <iterator> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QProtobufSelfcheckIterator |
| 29 | { |
| 30 | public: |
| 31 | using difference_type = QByteArray::difference_type; |
| 32 | using iterator_category = std::random_access_iterator_tag; |
| 33 | using value_type = QByteArray::value_type; |
| 34 | using pointer = QByteArray::pointer; |
| 35 | using reference = QByteArray::reference; |
| 36 | |
| 37 | QProtobufSelfcheckIterator() = default; |
| 38 | |
| 39 | static QProtobufSelfcheckIterator fromView(QByteArrayView container) |
| 40 | { |
| 41 | QProtobufSelfcheckIterator iter(container); |
| 42 | return iter; |
| 43 | } |
| 44 | |
| 45 | QProtobufSelfcheckIterator(const QProtobufSelfcheckIterator &other) = default; |
| 46 | QProtobufSelfcheckIterator &operator=(const QProtobufSelfcheckIterator &other) = default; |
| 47 | |
| 48 | explicit operator QByteArray::const_iterator &() { return m_it; } |
| 49 | explicit operator QByteArray::const_iterator() const { return m_it; } |
| 50 | |
| 51 | char operator*() |
| 52 | { |
| 53 | Q_ASSERT(isValid()); |
| 54 | return *m_it; |
| 55 | } |
| 56 | |
| 57 | bool isValid() const noexcept { return m_containerBegin <= m_it && m_it <= m_containerEnd; } |
| 58 | |
| 59 | QProtobufSelfcheckIterator &operator++() noexcept |
| 60 | { |
| 61 | if (!isValid()) { |
| 62 | qWarning(msg: "Deserialization failed: Unexpected end of data." ); |
| 63 | return *this; |
| 64 | } |
| 65 | ++m_it; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | QProtobufSelfcheckIterator operator++(int) noexcept |
| 70 | { |
| 71 | QProtobufSelfcheckIterator old(*this); |
| 72 | operator++(); |
| 73 | return old; |
| 74 | } |
| 75 | |
| 76 | QProtobufSelfcheckIterator &operator--() noexcept |
| 77 | { |
| 78 | if (!isValid()) { |
| 79 | qWarning(msg: "Deserialization failed: Unexpected end of data." ); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | --m_it; |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | QProtobufSelfcheckIterator operator--(int) noexcept |
| 88 | { |
| 89 | QProtobufSelfcheckIterator old(*this); |
| 90 | operator--(); |
| 91 | return old; |
| 92 | } |
| 93 | |
| 94 | QProtobufSelfcheckIterator &operator+=(qsizetype count) noexcept |
| 95 | { |
| 96 | if (!isValid()) { |
| 97 | qWarning(msg: "Deserialization failed: Unexpected end of data." ); |
| 98 | return *this; |
| 99 | } |
| 100 | m_it += count; |
| 101 | return *this; |
| 102 | } |
| 103 | |
| 104 | QProtobufSelfcheckIterator &operator-=(qsizetype count) noexcept |
| 105 | { |
| 106 | if (!isValid()) { |
| 107 | qWarning(msg: "Deserialization failed: Unexpected end of data." ); |
| 108 | return *this; |
| 109 | } |
| 110 | m_it -= count; |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | const char *data() const { return m_it; } |
| 115 | qsizetype bytesLeft() const { return isValid() ? std::distance(first: m_it, last: m_containerEnd) : 0; } |
| 116 | |
| 117 | private: |
| 118 | explicit QProtobufSelfcheckIterator(QByteArrayView container) |
| 119 | : m_containerBegin(container.begin()), m_containerEnd(container.end()), |
| 120 | m_it(container.begin()) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | friend bool operator==(const QProtobufSelfcheckIterator &lhs, |
| 125 | const QProtobufSelfcheckIterator &rhs) noexcept |
| 126 | { |
| 127 | Q_ASSERT(lhs.m_containerBegin == rhs.m_containerBegin); |
| 128 | Q_ASSERT(lhs.m_containerEnd == rhs.m_containerEnd); |
| 129 | return lhs.m_it == rhs.m_it; |
| 130 | } |
| 131 | friend bool operator!=(const QProtobufSelfcheckIterator &lhs, |
| 132 | const QProtobufSelfcheckIterator &rhs) noexcept |
| 133 | { |
| 134 | Q_ASSERT(lhs.m_containerBegin == rhs.m_containerBegin); |
| 135 | Q_ASSERT(lhs.m_containerEnd == rhs.m_containerEnd); |
| 136 | return lhs.m_it != rhs.m_it; |
| 137 | } |
| 138 | friend bool operator==(const QProtobufSelfcheckIterator &lhs, |
| 139 | const QByteArray::const_iterator &other) noexcept |
| 140 | { |
| 141 | Q_ASSERT(lhs.m_containerBegin <= other && other <= lhs.m_containerEnd); |
| 142 | return lhs.m_it == other; |
| 143 | } |
| 144 | friend bool operator!=(const QProtobufSelfcheckIterator &lhs, |
| 145 | const QByteArray::const_iterator &other) noexcept |
| 146 | { |
| 147 | Q_ASSERT(lhs.m_containerBegin <= other && other <= lhs.m_containerEnd); |
| 148 | return lhs.m_it != other; |
| 149 | } |
| 150 | |
| 151 | friend qint64 operator-(const QProtobufSelfcheckIterator &lhs, |
| 152 | const QProtobufSelfcheckIterator &rhs) noexcept |
| 153 | { |
| 154 | Q_ASSERT(lhs.m_containerBegin == rhs.m_containerBegin); |
| 155 | return lhs.m_it - rhs.m_it; |
| 156 | } |
| 157 | |
| 158 | QByteArrayView::const_iterator m_containerBegin; |
| 159 | QByteArrayView::const_iterator m_containerEnd; |
| 160 | QByteArrayView::const_iterator m_it; |
| 161 | }; |
| 162 | |
| 163 | inline QProtobufSelfcheckIterator operator+(const QProtobufSelfcheckIterator &it, qsizetype length) |
| 164 | { |
| 165 | QProtobufSelfcheckIterator copy = it; |
| 166 | copy += length; |
| 167 | return copy; |
| 168 | } |
| 169 | |
| 170 | inline QProtobufSelfcheckIterator operator-(const QProtobufSelfcheckIterator &it, qsizetype length) |
| 171 | { |
| 172 | QProtobufSelfcheckIterator copy = it; |
| 173 | copy -= length; |
| 174 | return copy; |
| 175 | } |
| 176 | |
| 177 | QT_END_NAMESPACE |
| 178 | |
| 179 | #endif // QPROTOBUFSELFCHECKITERATOR_P_H |
| 180 | |