|
1 /* |
|
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. |
|
5 * |
|
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. |
|
10 * |
|
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. |
|
15 * |
|
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. |
|
20 * |
|
21 */ |
|
22 |
|
23 #ifndef KJS_LIST_H |
|
24 #define KJS_LIST_H |
|
25 |
|
26 #include "value.h" |
|
27 |
|
28 namespace KJS { |
|
29 |
|
30 struct ListImpBase { |
|
31 int size; |
|
32 int refCount; |
|
33 int valueRefCount; // FIXME: Get rid of this. |
|
34 }; |
|
35 |
|
36 class ListIterator; |
|
37 |
|
38 /** |
|
39 * @short Native list type. |
|
40 * |
|
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. |
|
44 * |
|
45 * The list is explicitly shared. Note that while copyTail() returns a |
|
46 * copy of the list the referenced objects are still shared. |
|
47 */ |
|
48 class List { |
|
49 public: |
|
50 IMPORT List(); |
|
51 ~List() { deref(); } |
|
52 |
|
53 List(const List &b) : _impBase(b._impBase) { |
|
54 ++_impBase->refCount; |
|
55 ++_impBase->valueRefCount; |
|
56 } |
|
57 List &operator=(const List &); |
|
58 |
|
59 /** |
|
60 * Append an object to the end of the list. |
|
61 * |
|
62 * @param val Pointer to object. |
|
63 */ |
|
64 IMPORT void append(JSValue *val); |
|
65 /** |
|
66 * Remove all elements from the list. |
|
67 */ |
|
68 void clear(); |
|
69 |
|
70 void reset() { deref(); ++(_impBase = empty()._impBase)->refCount; } |
|
71 |
|
72 /** |
|
73 * Make a copy of the list |
|
74 */ |
|
75 List copy() const; |
|
76 |
|
77 /** |
|
78 * Copy all elements from the second list here |
|
79 */ |
|
80 void copyFrom(const List& other); |
|
81 |
|
82 /** |
|
83 * Make a copy of the list, omitting the first element. |
|
84 */ |
|
85 IMPORT List copyTail() const; |
|
86 |
|
87 /** |
|
88 * @return true if the list is empty. false otherwise. |
|
89 */ |
|
90 bool isEmpty() const { return _impBase->size == 0; } |
|
91 /** |
|
92 * @return the current size of the list. |
|
93 */ |
|
94 int size() const { return _impBase->size; } |
|
95 /** |
|
96 * @return A KJS::ListIterator pointing to the first element. |
|
97 */ |
|
98 ListIterator begin() const; |
|
99 /** |
|
100 * @return A KJS::ListIterator pointing to the last element. |
|
101 */ |
|
102 ListIterator end() const; |
|
103 |
|
104 /** |
|
105 * Retrieve an element at an indexed position. If you want to iterate |
|
106 * trough the whole list using KJS::ListIterator will be faster. |
|
107 * |
|
108 * @param i List index. |
|
109 * @return Return the element at position i. KJS::Undefined if the |
|
110 * index is out of range. |
|
111 */ |
|
112 IMPORT JSValue *at(int i) const; |
|
113 /** |
|
114 * Equivalent to at. |
|
115 */ |
|
116 JSValue *operator[](int i) const { return at(i); } |
|
117 |
|
118 /** |
|
119 * Returns a pointer to a static instance of an empty list. Useful if a |
|
120 * function has a KJS::List parameter. |
|
121 */ |
|
122 static const List &empty(); |
|
123 |
|
124 static void markProtectedLists(); |
|
125 private: |
|
126 ListImpBase *_impBase; |
|
127 |
|
128 void deref() { --_impBase->valueRefCount; if (--_impBase->refCount == 0) release(); } |
|
129 |
|
130 IMPORT void release(); |
|
131 void markValues(); |
|
132 }; |
|
133 |
|
134 /** |
|
135 * @short Iterator for KJS::List objects. |
|
136 */ |
|
137 class ListIterator { |
|
138 public: |
|
139 /** |
|
140 * Construct an iterator that points to the first element of the list. |
|
141 * @param l The list the iterator will operate on. |
|
142 */ |
|
143 ListIterator(const List &l) : _list(&l), _i(0) { } |
|
144 ListIterator(const List &l, int index) : _list(&l), _i(index) { } |
|
145 /** |
|
146 * Dereference the iterator. |
|
147 * @return A pointer to the element the iterator operates on. |
|
148 */ |
|
149 JSValue *operator->() const { return _list->at(_i); } |
|
150 JSValue *operator*() const { return _list->at(_i); } |
|
151 /** |
|
152 * Prefix increment operator. |
|
153 * @return The element after the increment. |
|
154 */ |
|
155 JSValue *operator++() { return _list->at(++_i); } |
|
156 /** |
|
157 * Postfix increment operator. |
|
158 */ |
|
159 JSValue *operator++(int) { return _list->at(_i++); } |
|
160 /** |
|
161 * Prefix decrement operator. |
|
162 */ |
|
163 JSValue *operator--() { return _list->at(--_i); } |
|
164 /** |
|
165 * Postfix decrement operator. |
|
166 */ |
|
167 JSValue *operator--(int) { return _list->at(_i--); } |
|
168 /** |
|
169 * Compare the iterator with another one. |
|
170 * @return True if the two iterators operate on the same list element. |
|
171 * False otherwise. |
|
172 */ |
|
173 bool operator==(const ListIterator &it) const { return _i == it._i; } |
|
174 /** |
|
175 * Check for inequality with another iterator. |
|
176 * @return True if the two iterators operate on different list elements. |
|
177 */ |
|
178 bool operator!=(const ListIterator &it) const { return _i != it._i; } |
|
179 |
|
180 private: |
|
181 const List *_list; |
|
182 int _i; |
|
183 }; |
|
184 |
|
185 inline ListIterator List::begin() const { return ListIterator(*this); } |
|
186 inline ListIterator List::end() const { return ListIterator(*this, size()); } |
|
187 |
|
188 } // namespace KJS |
|
189 |
|
190 #endif // KJS_LIST_H |