2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003 Apple Computer, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
33 int valueRefCount; // FIXME: Get rid of this.
39 * @short Native list type.
41 * List is a native ECMAScript type. List values are only used for
42 * intermediate results of expression evaluation and cannot be stored
43 * as properties of objects.
45 class List : Noncopyable {
51 * Append an object to the end of the list.
53 * @param val Pointer to object.
55 void append(JSValue *val);
57 * Remove all elements from the list.
61 void reset() { deref(); ++(_impBase = empty()._impBase)->refCount; }
64 * Make a copy of the list, starting from startIndex.
66 void slice(int startIndex, List& result) const;
68 * @return true if the list is empty. false otherwise.
70 bool isEmpty() const { return _impBase->size == 0; }
72 * @return the current size of the list.
74 int size() const { return _impBase->size; }
76 * @return A KJS::ListIterator pointing to the first element.
78 ListIterator begin() const;
80 * @return A KJS::ListIterator pointing to the last element.
82 ListIterator end() const;
85 * Retrieve an element at an indexed position. If you want to iterate
86 * trough the whole list using KJS::ListIterator will be faster.
88 * @param i List index.
89 * @return Return the element at position i. KJS::Undefined if the
90 * index is out of range.
92 JSValue *at(int i) const;
96 JSValue *operator[](int i) const { return at(i); }
99 * Returns a pointer to a static instance of an empty list. Useful if a
100 * function has a KJS::List parameter.
102 static const List &empty();
104 static void markProtectedLists();
106 ListImpBase *_impBase;
108 void deref() { --_impBase->valueRefCount; if (--_impBase->refCount == 0) release(); }
115 * @short Iterator for KJS::List objects.
120 * Construct an iterator that points to the first element of the list.
121 * @param l The list the iterator will operate on.
123 ListIterator(const List &l) : _list(&l), _i(0) { }
124 ListIterator(const List &l, int index) : _list(&l), _i(index) { }
126 * Dereference the iterator.
127 * @return A pointer to the element the iterator operates on.
129 JSValue *operator->() const { return _list->at(_i); }
130 JSValue *operator*() const { return _list->at(_i); }
132 * Prefix increment operator.
133 * @return The element after the increment.
135 JSValue *operator++() { return _list->at(++_i); }
137 * Postfix increment operator.
139 JSValue *operator++(int) { return _list->at(_i++); }
141 * Prefix decrement operator.
143 JSValue *operator--() { return _list->at(--_i); }
145 * Postfix decrement operator.
147 JSValue *operator--(int) { return _list->at(_i--); }
149 * Compare the iterator with another one.
150 * @return True if the two iterators operate on the same list element.
153 bool operator==(const ListIterator &it) const { return _i == it._i; }
155 * Check for inequality with another iterator.
156 * @return True if the two iterators operate on different list elements.
158 bool operator!=(const ListIterator &it) const { return _i != it._i; }
165 inline ListIterator List::begin() const { return ListIterator(*this); }
166 inline ListIterator List::end() const { return ListIterator(*this, size()); }