PageRenderTime 73ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/corelib/tools/qiterator.h

https://bitbucket.org/cvp2ri/qt5-tlsauth
C Header | 191 lines | 142 code | 9 blank | 40 comment | 51 complexity | 2748bb84d08a2715264f0ca81a6fed59 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef QITERATOR_H
  42. #define QITERATOR_H
  43. #include <QtCore/qglobal.h>
  44. QT_BEGIN_NAMESPACE
  45. #define Q_DECLARE_SEQUENTIAL_ITERATOR(C) \
  46. \
  47. template <class T> \
  48. class Q##C##Iterator \
  49. { \
  50. typedef typename Q##C<T>::const_iterator const_iterator; \
  51. Q##C<T> c; \
  52. const_iterator i; \
  53. public: \
  54. inline Q##C##Iterator(const Q##C<T> &container) \
  55. : c(container), i(c.constBegin()) {} \
  56. inline Q##C##Iterator &operator=(const Q##C<T> &container) \
  57. { c = container; i = c.constBegin(); return *this; } \
  58. inline void toFront() { i = c.constBegin(); } \
  59. inline void toBack() { i = c.constEnd(); } \
  60. inline bool hasNext() const { return i != c.constEnd(); } \
  61. inline const T &next() { return *i++; } \
  62. inline const T &peekNext() const { return *i; } \
  63. inline bool hasPrevious() const { return i != c.constBegin(); } \
  64. inline const T &previous() { return *--i; } \
  65. inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \
  66. inline bool findNext(const T &t) \
  67. { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
  68. inline bool findPrevious(const T &t) \
  69. { while (i != c.constBegin()) if (*(--i) == t) return true; \
  70. return false; } \
  71. };
  72. #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) \
  73. \
  74. template <class T> \
  75. class QMutable##C##Iterator \
  76. { \
  77. typedef typename Q##C<T>::iterator iterator; \
  78. typedef typename Q##C<T>::const_iterator const_iterator; \
  79. Q##C<T> *c; \
  80. iterator i, n; \
  81. inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
  82. public: \
  83. inline QMutable##C##Iterator(Q##C<T> &container) \
  84. : c(&container) \
  85. { c->setSharable(false); i = c->begin(); n = c->end(); } \
  86. inline ~QMutable##C##Iterator() \
  87. { c->setSharable(true); } \
  88. inline QMutable##C##Iterator &operator=(Q##C<T> &container) \
  89. { c->setSharable(true); c = &container; c->setSharable(false); \
  90. i = c->begin(); n = c->end(); return *this; } \
  91. inline void toFront() { i = c->begin(); n = c->end(); } \
  92. inline void toBack() { i = c->end(); n = i; } \
  93. inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
  94. inline T &next() { n = i++; return *n; } \
  95. inline T &peekNext() const { return *i; } \
  96. inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
  97. inline T &previous() { n = --i; return *n; } \
  98. inline T &peekPrevious() const { iterator p = i; return *--p; } \
  99. inline void remove() \
  100. { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } } \
  101. inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n = t; } \
  102. inline T &value() { Q_ASSERT(item_exists()); return *n; } \
  103. inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
  104. inline void insert(const T &t) { n = i = c->insert(i, t); ++i; } \
  105. inline bool findNext(const T &t) \
  106. { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; } \
  107. inline bool findPrevious(const T &t) \
  108. { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true; \
  109. n = c->end(); return false; } \
  110. };
  111. #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) \
  112. \
  113. template <class Key, class T> \
  114. class Q##C##Iterator \
  115. { \
  116. typedef typename Q##C<Key,T>::const_iterator const_iterator; \
  117. typedef const_iterator Item; \
  118. Q##C<Key,T> c; \
  119. const_iterator i, n; \
  120. inline bool item_exists() const { return n != c.constEnd(); } \
  121. public: \
  122. inline Q##C##Iterator(const Q##C<Key,T> &container) \
  123. : c(container), i(c.constBegin()), n(c.constEnd()) {} \
  124. inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \
  125. { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \
  126. inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \
  127. inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \
  128. inline bool hasNext() const { return i != c.constEnd(); } \
  129. inline Item next() { n = i++; return n; } \
  130. inline Item peekNext() const { return i; } \
  131. inline bool hasPrevious() const { return i != c.constBegin(); } \
  132. inline Item previous() { n = --i; return n; } \
  133. inline Item peekPrevious() const { const_iterator p = i; return --p; } \
  134. inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
  135. inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
  136. inline bool findNext(const T &t) \
  137. { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \
  138. inline bool findPrevious(const T &t) \
  139. { while (i != c.constBegin()) if (*(n = --i) == t) return true; \
  140. n = c.constEnd(); return false; } \
  141. };
  142. #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) \
  143. \
  144. template <class Key, class T> \
  145. class QMutable##C##Iterator \
  146. { \
  147. typedef typename Q##C<Key,T>::iterator iterator; \
  148. typedef typename Q##C<Key,T>::const_iterator const_iterator; \
  149. typedef iterator Item; \
  150. Q##C<Key,T> *c; \
  151. iterator i, n; \
  152. inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
  153. public: \
  154. inline QMutable##C##Iterator(Q##C<Key,T> &container) \
  155. : c(&container) \
  156. { c->setSharable(false); i = c->begin(); n = c->end(); } \
  157. inline ~QMutable##C##Iterator() \
  158. { c->setSharable(true); } \
  159. inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \
  160. { c->setSharable(true); c = &container; c->setSharable(false); i = c->begin(); n = c->end(); return *this; } \
  161. inline void toFront() { i = c->begin(); n = c->end(); } \
  162. inline void toBack() { i = c->end(); n = c->end(); } \
  163. inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \
  164. inline Item next() { n = i++; return n; } \
  165. inline Item peekNext() const { return i; } \
  166. inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); } \
  167. inline Item previous() { n = --i; return n; } \
  168. inline Item peekPrevious() const { iterator p = i; return --p; } \
  169. inline void remove() \
  170. { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \
  171. inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \
  172. inline T &value() { Q_ASSERT(item_exists()); return *n; } \
  173. inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
  174. inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
  175. inline bool findNext(const T &t) \
  176. { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \
  177. inline bool findPrevious(const T &t) \
  178. { while (const_iterator(i) != c->constBegin()) if (*(n = --i) == t) return true; \
  179. n = c->end(); return false; } \
  180. };
  181. QT_END_NAMESPACE
  182. #endif // QITERATOR_H