• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
list.h
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;
34  };
35 
36  class ListIterator;
37 
48  class KJS_EXPORT List {
49  public:
50  List();
51  List(bool needsMarking);
52  ~List() { deref(); }
53 
54  List(const List &b) : _impBase(b._impBase), _needsMarking(false) {
55  ++_impBase->refCount;
56  if (!_impBase->valueRefCount) refValues();
57  ++_impBase->valueRefCount;
58  }
59  List &operator=(const List &);
60 
66  void append(const Value& val) { append(val.imp()); }
67  void append(ValueImp *val);
71  void clear();
72 
76  List copy() const;
77 
81  List copyTail() const;
82 
86  bool isEmpty() const { return _impBase->size == 0; }
90  int size() const { return _impBase->size; }
94  ListIterator begin() const;
98  ListIterator end() const;
99 
108  Value at(int i) const { return Value(impAt(i)); }
112  Value operator[](int i) const { return Value(impAt(i)); }
113 
114  ValueImp *impAt(int i) const;
115 
120  static const List &empty();
121 
122  void mark() { if (_impBase->valueRefCount == 0) markValues(); }
123  private:
124  ListImpBase *_impBase;
125  bool _needsMarking;
126 
127  void deref() { if (!_needsMarking && --_impBase->valueRefCount == 0) derefValues(); if (--_impBase->refCount == 0) release(); }
128 
129  void release();
130  void refValues();
131  void derefValues();
132  void markValues();
133  };
134 
138  class ListIterator {
139  public:
144  ListIterator(const List &l) : _list(&l), _i(0) { }
145  ListIterator(const List &l, int index) : _list(&l), _i(index) { }
150  ValueImp *operator->() const { return _list->impAt(_i); }
151  Value operator*() const { return Value(_list->impAt(_i)); }
156  Value operator++() { return Value(_list->impAt(++_i)); }
160  Value operator++(int) { return Value(_list->impAt(_i++)); }
164  Value operator--() { return Value(_list->impAt(--_i)); }
168  Value operator--(int) { return Value(_list->impAt(_i--)); }
174  bool operator==(const ListIterator &it) const { return _i == it._i; }
179  bool operator!=(const ListIterator &it) const { return _i != it._i; }
180 
181  private:
182  const List *_list;
183  int _i;
184  };
185 
186  inline ListIterator List::begin() const { return ListIterator(*this); }
187  inline ListIterator List::end() const { return ListIterator(*this, size()); }
188 
189  inline List &List::operator=(const List &b)
190  {
191  ListImpBase *bImpBase = b._impBase;
192  ++bImpBase->refCount;
193  deref();
194  _impBase = bImpBase;
195  if (!_needsMarking) {
196  if (!_impBase->valueRefCount) {
197  refValues();
198  }
199  _impBase->valueRefCount++;
200  }
201 
202  return *this;
203  }
204 
205  } // namespace KJS
206 
207 #endif // KJS_LIST_H
KJS::ListIterator
Iterator for KJS::List objects.
Definition: list.h:138
KJS::ListIterator::ListIterator
ListIterator(const List &l)
Construct an iterator that points to the first element of the list.
Definition: list.h:144
KJS::ListIterator::operator--
Value operator--(int)
Postfix decrement operator.
Definition: list.h:168
KJS::ListIterator::operator==
bool operator==(const ListIterator &it) const
Compare the iterator with another one.
Definition: list.h:174
KJS::ListIterator::operator--
Value operator--()
Prefix decrement operator.
Definition: list.h:164
KJS::ListIterator::operator++
Value operator++()
Prefix increment operator.
Definition: list.h:156
KJS::ListIterator::operator++
Value operator++(int)
Postfix increment operator.
Definition: list.h:160
KJS::ListIterator::operator!=
bool operator!=(const ListIterator &it) const
Check for inequality with another iterator.
Definition: list.h:179
KJS::ListIterator::operator->
ValueImp * operator->() const
Dereference the iterator.
Definition: list.h:150
KJS::List
Native list type.
Definition: list.h:48
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86
KJS::List::operator[]
Value operator[](int i) const
Equivalent to at.
Definition: list.h:112
KJS::List::begin
ListIterator begin() const
Definition: list.h:186
KJS::List::size
int size() const
Definition: list.h:90
KJS::List::at
Value at(int i) const
Retrieve an element at an indexed position.
Definition: list.h:108
KJS::List::end
ListIterator end() const
Definition: list.h:187
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:78
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.