| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtScript 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qscriptclasspropertyiterator.h" |
| 41 | |
| 42 | #include "qscriptstring.h" |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | /*! |
| 47 | \since 4.4 |
| 48 | \class QScriptClassPropertyIterator |
| 49 | \inmodule QtScript |
| 50 | \brief The QScriptClassPropertyIterator class provides an iterator interface for custom Qt Script objects. |
| 51 | |
| 52 | \ingroup script |
| 53 | |
| 54 | This class is only relevant if you have subclassed QScriptClass and |
| 55 | want to provide enumeration of your custom properties (e.g. when |
| 56 | objects of your class are used with QScriptValueIterator, or with |
| 57 | the for-in statement in scripts). |
| 58 | |
| 59 | The object() function returns the Qt Script object the iterator is |
| 60 | traversing. |
| 61 | |
| 62 | toFront(), hasNext() and next() provide forward iteration. |
| 63 | |
| 64 | toBack(), hasPrevious() and previous() provide backward iteration. |
| 65 | |
| 66 | name(), id() and flags() return information about the last property |
| 67 | that was jumped over using next() or previous(). |
| 68 | |
| 69 | \sa QScriptClass::newIterator(), QScriptValueIterator |
| 70 | */ |
| 71 | |
| 72 | class QScriptClassPropertyIteratorPrivate |
| 73 | { |
| 74 | Q_DECLARE_PUBLIC(QScriptClassPropertyIterator) |
| 75 | public: |
| 76 | QScriptClassPropertyIteratorPrivate() {} |
| 77 | virtual ~QScriptClassPropertyIteratorPrivate() {} |
| 78 | |
| 79 | QScriptValue object; |
| 80 | |
| 81 | QScriptClassPropertyIterator *q_ptr; |
| 82 | }; |
| 83 | |
| 84 | /*! |
| 85 | Constructs an iterator for traversing \a object. |
| 86 | |
| 87 | Subclasses should ensure that the iterator is set to the front of the |
| 88 | sequence of properties (before the first property). |
| 89 | */ |
| 90 | QScriptClassPropertyIterator::QScriptClassPropertyIterator(const QScriptValue &object) |
| 91 | : d_ptr(new QScriptClassPropertyIteratorPrivate) |
| 92 | { |
| 93 | d_ptr->q_ptr = this; |
| 94 | d_ptr->object = object; |
| 95 | } |
| 96 | |
| 97 | /*! |
| 98 | \internal |
| 99 | */ |
| 100 | QScriptClassPropertyIterator::QScriptClassPropertyIterator(const QScriptValue &object, |
| 101 | QScriptClassPropertyIteratorPrivate &dd) |
| 102 | : d_ptr(&dd) |
| 103 | { |
| 104 | d_ptr->q_ptr = this; |
| 105 | d_ptr->object = object; |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | Destroys the iterator. |
| 110 | */ |
| 111 | QScriptClassPropertyIterator::~QScriptClassPropertyIterator() |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | Returns the Qt Script object this iterator is traversing. |
| 117 | */ |
| 118 | QScriptValue QScriptClassPropertyIterator::object() const |
| 119 | { |
| 120 | Q_D(const QScriptClassPropertyIterator); |
| 121 | return d->object; |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | \fn bool QScriptClassPropertyIterator::hasNext() const |
| 126 | |
| 127 | Returns true if there is at least one item ahead of the iterator |
| 128 | (i.e. the iterator is \e not at the back of the property sequence); |
| 129 | otherwise returns false. |
| 130 | |
| 131 | \sa next(), hasPrevious() |
| 132 | */ |
| 133 | |
| 134 | /*! |
| 135 | \fn void QScriptClassPropertyIterator::next() |
| 136 | |
| 137 | Advances the iterator by one position. |
| 138 | |
| 139 | Calling this function on an iterator located at the back of the |
| 140 | container leads to undefined results. |
| 141 | |
| 142 | \sa hasNext(), previous(), name() |
| 143 | */ |
| 144 | |
| 145 | /*! |
| 146 | \fn bool QScriptClassPropertyIterator::hasPrevious() const |
| 147 | |
| 148 | Returns true if there is at least one item behind the iterator |
| 149 | (i.e. the iterator is \e not at the front of the property sequence); |
| 150 | otherwise returns false. |
| 151 | |
| 152 | \sa previous(), hasNext() |
| 153 | */ |
| 154 | |
| 155 | /*! |
| 156 | \fn void QScriptClassPropertyIterator::previous() |
| 157 | |
| 158 | Moves the iterator back by one position. |
| 159 | |
| 160 | Calling this function on an iterator located at the front of the |
| 161 | container leads to undefined results. |
| 162 | |
| 163 | \sa hasPrevious(), next(), name() |
| 164 | */ |
| 165 | |
| 166 | /*! |
| 167 | \fn void QScriptClassPropertyIterator::toFront() |
| 168 | |
| 169 | Moves the iterator to the front of the QScriptValue (before the |
| 170 | first property). |
| 171 | |
| 172 | \sa toBack(), next() |
| 173 | */ |
| 174 | |
| 175 | /*! |
| 176 | \fn void QScriptClassPropertyIterator::toBack() |
| 177 | |
| 178 | Moves the iterator to the back of the QScriptValue (after the |
| 179 | last property). |
| 180 | |
| 181 | \sa toFront(), previous() |
| 182 | */ |
| 183 | |
| 184 | /*! |
| 185 | \fn QScriptString QScriptClassPropertyIterator::name() const |
| 186 | |
| 187 | Returns the name of the last property that was jumped over using |
| 188 | next() or previous(). |
| 189 | |
| 190 | \sa id() |
| 191 | */ |
| 192 | |
| 193 | /*! |
| 194 | \fn uint QScriptClassPropertyIterator::id() const |
| 195 | |
| 196 | Returns the id of the last property that was jumped over using |
| 197 | next() or previous(). |
| 198 | |
| 199 | The default implementation returns 0. |
| 200 | |
| 201 | \sa name() |
| 202 | */ |
| 203 | uint QScriptClassPropertyIterator::id() const |
| 204 | { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /*! |
| 209 | Returns the flags of the last property that was jumped over using |
| 210 | next() or previous(). |
| 211 | |
| 212 | The default implementation calls the propertyFlags() function of |
| 213 | object() with argument name(). |
| 214 | */ |
| 215 | QScriptValue::PropertyFlags QScriptClassPropertyIterator::flags() const |
| 216 | { |
| 217 | return object().propertyFlags(name: name()); |
| 218 | } |
| 219 | |
| 220 | QT_END_NAMESPACE |
| 221 | |