selfcheckiterator.h 4.2 KB

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