QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qprotobufselfcheckiterator.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <QByteArray>
27 #include <stdexcept>
28 
29 #include "qtprotobufglobal.h"
30 
31 #pragma once //QProtobufSelfcheckIterator
32 
33 namespace QtProtobuf {
34 
39 class Q_PROTOBUF_EXPORT QProtobufSelfcheckIterator
40 {
41 public:
42  QProtobufSelfcheckIterator(const QByteArray &container) : m_sizeLeft(container.size())
43  , m_containerSize(container.size())
44  , m_it(container.begin()) {}
45 
46  QProtobufSelfcheckIterator(const QProtobufSelfcheckIterator &other) : m_sizeLeft(other.m_sizeLeft)
47  , m_containerSize(other.m_containerSize)
48  , m_it(other.m_it) {
49  if (m_sizeLeft > m_containerSize || m_sizeLeft < 0) {
50  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
51  }
52  }
53 
54  explicit operator QByteArray::const_iterator&() { return m_it; }
55  explicit operator QByteArray::const_iterator() const { return m_it; }
56 
57  char operator *() { return *m_it; }
58 
59  QProtobufSelfcheckIterator &operator ++() {
60  --m_sizeLeft;
61  if (m_sizeLeft < 0) {
62  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
63  }
64  ++m_it;
65  return *this;
66  }
67 
68  QProtobufSelfcheckIterator &operator --() {
69  ++m_sizeLeft;
70  if (m_sizeLeft > m_containerSize) {
71  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
72  }
73  --m_it;
74  return *this;
75  }
76 
77  QProtobufSelfcheckIterator &operator +=(int count) {
78  m_sizeLeft -= count;
79  if (m_sizeLeft < 0) {
80  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
81  }
82  m_it += count;
83  return *this;
84  }
85 
86  QProtobufSelfcheckIterator &operator -=(int count) {
87  m_sizeLeft += count;
88  if (m_sizeLeft > m_containerSize) {
89  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
90  }
91  m_it -= count;
92  return *this;
93  }
94 
95  QProtobufSelfcheckIterator &operator =(const QProtobufSelfcheckIterator &other) {
96  if (this == &other) {
97  return *this;
98  }
99 
100  m_containerSize = other.m_containerSize;
101  m_sizeLeft = other.m_sizeLeft;
102  if (m_sizeLeft > m_containerSize || m_sizeLeft < 0) {
103  throw std::out_of_range("Container is less than required fields number. Deserialization failed");
104  }
105  m_it = other.m_it;
106  return *this;
107  }
108 
109  bool operator ==(const QProtobufSelfcheckIterator &other) const {
110  return other.m_it == m_it;
111  }
112 
113  bool operator !=(const QProtobufSelfcheckIterator &other) const {
114  return other.m_it != m_it;
115  }
116 
117  bool operator ==(const QByteArray::const_iterator &other) const {
118  return other == m_it;
119  }
120 
121  bool operator !=(const QByteArray::const_iterator &other) const {
122  return other != m_it;
123  }
124 
125  const char *data() const {
126  return m_it;
127  }
128 
129  int size() const {
130  return m_sizeLeft;
131  }
132 private:
133  int m_sizeLeft;
134  int m_containerSize;
135  QByteArray::const_iterator m_it;
136 };
137 
138 inline QProtobufSelfcheckIterator operator +(const QProtobufSelfcheckIterator &it, int lenght) {
139  QProtobufSelfcheckIterator itNew = it;
140  return itNew += lenght;
141 }
142 
143 }
The QProtobufSelfcheckIterator class.
Definition: qprotobufselfcheckiterator.h:40