KJS

list.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2001 Harri Porten ([email protected])
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 #include "LocalStorage.h"
28 
29 namespace KJS
30 {
31 
32 const int inlineListValuesSize = 5;
33 
34 struct ListImpBase {
35  int size;
36  int refCount;
37  LocalStorageEntry *data; // points either to inline or out-of-line buffer
38 };
39 
40 class ListIterator;
41 
42 /**
43  * @short Native list type.
44  *
45  * List is a native ECMAScript type. List values are only used for
46  * intermediate results of expression evaluation and cannot be stored
47  * as properties of objects.
48  *
49  * The list is explicitly shared. Note that while copyTail() returns a
50  * copy of the list the referenced objects are still shared.
51  */
52 class KJS_EXPORT List
53 {
54 public:
55  List();
56  ~List()
57  {
58  deref();
59  }
60 
61  List(const List &b) : _impBase(b._impBase)
62  {
63  ++_impBase->refCount;
64  }
65  List &operator=(const List &);
66 
67  /**
68  * Append an object to the end of the list.
69  *
70  * @param val Pointer to object.
71  */
72  void append(JSValue *val);
73 
74  /**
75  * Remove all elements from the list.
76  */
77  void clear()
78  {
79  _impBase->size = 0;
80  }
81 
82  /*
83  * Resets this List to point to the default empty list
84  */
85  void reset()
86  {
87  deref();
88  ++(_impBase = empty()._impBase)->refCount;
89  }
90 
91  /**
92  * Make a copy of the list
93  */
94  List copy() const;
95 
96  /**
97  * Make a copy of the list, omitting the first element.
98  */
99  List copyTail() const;
100 
101  /**
102  * @return true if the list is empty. false otherwise.
103  */
104  bool isEmpty() const
105  {
106  return _impBase->size == 0;
107  }
108 
109  /**
110  * @return the current size of the list.
111  */
112  int size() const
113  {
114  return _impBase->size;
115  }
116 
117  /**
118  * @return A KJS::ListIterator pointing to the first element.
119  */
120  ListIterator begin() const;
121 
122  /**
123  * @return A KJS::ListIterator pointing to the last element.
124  */
125  ListIterator end() const;
126 
127  /**
128  * Retrieve an element at an indexed position.
129  *
130  * @param i List index.
131  * @return Return the element at position i. KJS::Undefined if the
132  * index is out of range.
133  */
134  JSValue *at(int i) const;
135 
136  // As above but omits the range change
137  JSValue *atUnchecked(int i) const
138  {
139  return _impBase->data[i].val.valueVal;
140  }
141 
142  /**
143  * Equivalent to at.
144  */
145  JSValue *operator[](int i) const
146  {
147  return at(i);
148  }
149 
150  /**
151  * Returns a pointer to a static instance of an empty list. Useful if a
152  * function has a KJS::List parameter.
153  */
154  static const List &empty();
155 
156  static void markProtectedLists();
157 private:
158  /**
159  * Copy all elements from the second list here
160  */
161  void copyFrom(const List &other);
162 
163  void appendSlowCase(JSValue *val);
164 
165  ListImpBase *_impBase;
166 
167  void deref()
168  {
169  if (--_impBase->refCount == 0) {
170  release();
171  }
172  }
173 
174  void release();
175 };
176 
177 inline JSValue *List::at(int i) const
178 {
179  if (i < _impBase->size) {
180  return _impBase->data[i].val.valueVal;
181  } else {
182  return jsUndefined();
183  }
184 }
185 
186 inline void List::append(JSValue *val)
187 {
188  int size = _impBase->size;
189  int newSize = size + 1;
190  if (newSize < inlineListValuesSize) {
191  // Can just write to the inline buffer
192  _impBase->data[size].val.valueVal = val;
193  _impBase->size = newSize;
194  } else {
195  appendSlowCase(val);
196  }
197 }
198 
199 /**
200  * @short Iterator for KJS::List objects.
201  */
203 {
204 public:
205  /**
206  * Construct an iterator that points to the first element of the list.
207  * @param l The list the iterator will operate on.
208  */
209  ListIterator(const List &l) : _list(&l), _i(0) { }
210  ListIterator(const List &l, int index) : _list(&l), _i(index) { }
211  /**
212  * Dereference the iterator.
213  * @return A pointer to the element the iterator operates on.
214  */
216  {
217  return _list->at(_i);
218  }
219  JSValue *operator*() const
220  {
221  return _list->at(_i);
222  }
223  /**
224  * Prefix increment operator.
225  * @return The element after the increment.
226  */
228  {
229  return _list->at(++_i);
230  }
231  /**
232  * Postfix increment operator.
233  */
235  {
236  return _list->at(_i++);
237  }
238  /**
239  * Prefix decrement operator.
240  */
242  {
243  return _list->at(--_i);
244  }
245  /**
246  * Postfix decrement operator.
247  */
249  {
250  return _list->at(_i--);
251  }
252  /**
253  * Compare the iterator with another one.
254  * @return True if the two iterators operate on the same list element.
255  * False otherwise.
256  */
257  bool operator==(const ListIterator &it) const
258  {
259  return _i == it._i;
260  }
261  /**
262  * Check for inequality with another iterator.
263  * @return True if the two iterators operate on different list elements.
264  */
265  bool operator!=(const ListIterator &it) const
266  {
267  return _i != it._i;
268  }
269 
270 private:
271  const List *_list;
272  int _i;
273 };
274 
275 inline ListIterator List::begin() const
276 {
277  return ListIterator(*this);
278 }
279 inline ListIterator List::end() const
280 {
281  return ListIterator(*this, size());
282 }
283 
284 } // namespace KJS
285 
286 #endif // KJS_LIST_H
287 
void append(JSValue *val)
Append an object to the end of the list.
Definition: list.h:186
JSValue * operator--(int)
Postfix decrement operator.
Definition: list.h:248
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
virtual void release(quint64 objid)
bool isEmpty() const
Definition: list.h:104
JSValue * operator[](int i) const
Equivalent to at.
Definition: list.h:145
bool operator!=(const ListIterator &it) const
Check for inequality with another iterator.
Definition: list.h:265
int size() const
Definition: list.h:112
void deref()
ListIterator end() const
Definition: list.h:279
JSValue * operator->() const
Dereference the iterator.
Definition: list.h:215
bool operator==(const ListIterator &it) const
Compare the iterator with another one.
Definition: list.h:257
Iterator for KJS::List objects.
Definition: list.h:202
JSValue * operator++()
Prefix increment operator.
Definition: list.h:227
JSValue * operator++(int)
Postfix increment operator.
Definition: list.h:234
Native list type.
Definition: list.h:52
JSValue * at(int i) const
Retrieve an element at an indexed position.
Definition: list.h:177
void clear()
Remove all elements from the list.
Definition: list.h:77
ListIterator begin() const
Definition: list.h:275
ListIterator(const List &l)
Construct an iterator that points to the first element of the list.
Definition: list.h:209
JSValue * operator--()
Prefix decrement operator.
Definition: list.h:241
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Sep 27 2023 03:55:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.