qprotobufselfcheckiterator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <QByteArray>
  26. #include <stdexcept>
  27. #include "qtprotobufglobal.h"
  28. #pragma once //QProtobufSelfcheckIterator
  29. namespace QtProtobuf {
  30. /*!
  31. * \ingroup QtProtobuf
  32. * \brief The QProtobufSelfcheckIterator class
  33. */
  34. class Q_PROTOBUF_EXPORT QProtobufSelfcheckIterator
  35. {
  36. public:
  37. QProtobufSelfcheckIterator(const QByteArray &container) : m_sizeLeft(container.size())
  38. , m_containerSize(container.size())
  39. , m_it(container.begin()) {}
  40. QProtobufSelfcheckIterator(const QProtobufSelfcheckIterator &other) : m_sizeLeft(other.m_sizeLeft)
  41. , m_containerSize(other.m_containerSize)
  42. , m_it(other.m_it) {
  43. if (m_sizeLeft >= m_containerSize) {
  44. throw std::out_of_range("Container is less than required fields number. Deserialization failed");
  45. }
  46. }
  47. explicit operator QByteArray::const_iterator&() { return m_it; }
  48. explicit operator QByteArray::const_iterator() const { return m_it; }
  49. char operator *() { return *m_it; }
  50. QProtobufSelfcheckIterator &operator ++() {
  51. --m_sizeLeft;
  52. if (m_sizeLeft < 0) {
  53. throw std::out_of_range("Container is less than required fields number. Deserialization failed");
  54. }
  55. ++m_it;
  56. return *this;
  57. }
  58. QProtobufSelfcheckIterator &operator --() {
  59. ++m_sizeLeft;
  60. if (m_sizeLeft >= m_containerSize) {
  61. throw std::out_of_range("Container is less than required fields number. Deserialization failed");
  62. }
  63. --m_it;
  64. return *this;
  65. }
  66. QProtobufSelfcheckIterator &operator +=(int count) {
  67. m_sizeLeft -= count;
  68. if (m_sizeLeft < 0) {
  69. throw std::out_of_range("Container is less than required fields number. Deserialization failed");
  70. }
  71. m_it += count;
  72. return *this;
  73. }
  74. QProtobufSelfcheckIterator &operator -=(int count) {
  75. m_sizeLeft += count;
  76. if (m_sizeLeft >= m_containerSize) {
  77. throw std::out_of_range("Container is less than required fields number. Deserialization failed");
  78. }
  79. m_it -= count;
  80. return *this;
  81. }
  82. QProtobufSelfcheckIterator &operator =(const QProtobufSelfcheckIterator &other) {
  83. if (this == &other) {
  84. return *this;
  85. }
  86. m_containerSize = other.m_containerSize;
  87. m_sizeLeft = other.m_sizeLeft;
  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 = other.m_it;
  92. return *this;
  93. }
  94. bool operator ==(const QProtobufSelfcheckIterator &other) const {
  95. return other.m_it == m_it;
  96. }
  97. bool operator !=(const QProtobufSelfcheckIterator &other) const {
  98. return other.m_it != m_it;
  99. }
  100. bool operator ==(const QByteArray::const_iterator &other) const {
  101. return other == m_it;
  102. }
  103. bool operator !=(const QByteArray::const_iterator &other) const {
  104. return other != m_it;
  105. }
  106. private:
  107. int m_sizeLeft;
  108. int m_containerSize;
  109. QByteArray::const_iterator m_it;
  110. };
  111. inline QProtobufSelfcheckIterator operator +(const QProtobufSelfcheckIterator &it, int lenght) {
  112. QProtobufSelfcheckIterator itNew = it;
  113. return itNew += lenght;
  114. }
  115. }