list.h
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef KJS_LIST_H
#define KJS_LIST_H
#include "value.h"
namespace KJS {
struct ListImpBase {
int size;
int refCount;
int valueRefCount;
};
class ListIterator;
class KJS_EXPORT List {
public:
List();
List(bool needsMarking);
~List() { deref(); }
List(const List &b) : _impBase(b._impBase), _needsMarking(false) {
++_impBase->refCount;
if (!_impBase->valueRefCount) refValues();
++_impBase->valueRefCount;
}
List &operator=(const List &);
void append(const Value& val) { append(val.imp()); }
void append(ValueImp *val);
void clear();
List copy() const;
List copyTail() const;
bool isEmpty() const { return _impBase->size == 0; }
int size() const { return _impBase->size; }
ListIterator begin() const;
ListIterator end() const;
Value at(int i) const { return Value(impAt(i)); }
Value operator[](int i) const { return Value(impAt(i)); }
ValueImp *impAt(int i) const;
static const List &empty();
void mark() { if (_impBase->valueRefCount == 0) markValues(); }
private:
ListImpBase *_impBase;
bool _needsMarking;
void deref() { if (!_needsMarking && --_impBase->valueRefCount == 0) derefValues(); if (--_impBase->refCount == 0) release(); }
void release();
void refValues();
void derefValues();
void markValues();
};
class ListIterator {
public:
ListIterator(const List &l) : _list(&l), _i(0) { }
ListIterator(const List &l, int index) : _list(&l), _i(index) { }
ValueImp *operator->() const { return _list->impAt(_i); }
Value operator*() const { return Value(_list->impAt(_i)); }
Value operator++() { return Value(_list->impAt(++_i)); }
Value operator++(int) { return Value(_list->impAt(_i++)); }
Value operator--() { return Value(_list->impAt(--_i)); }
Value operator--(int) { return Value(_list->impAt(_i--)); }
bool operator==(const ListIterator &it) const { return _i == it._i; }
bool operator!=(const ListIterator &it) const { return _i != it._i; }
private:
const List *_list;
int _i;
};
inline ListIterator List::begin() const { return ListIterator(*this); }
inline ListIterator List::end() const { return ListIterator(*this, size()); }
inline List &List::operator=(const List &b)
{
ListImpBase *bImpBase = b._impBase;
++bImpBase->refCount;
deref();
_impBase = bImpBase;
if (!_needsMarking) {
if (!_impBase->valueRefCount) {
refValues();
}
_impBase->valueRefCount++;
}
return *this;
}
} // namespace KJS
#endif // KJS_LIST_H
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
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::List::at
Value at(int i) const
Retrieve an element at an indexed position.
Definition: list.h:108
KJS::ListIterator::operator++
Value operator++(int)
Postfix increment operator.
Definition: list.h:160
KJS::ListIterator::ListIterator
ListIterator(const List &l)
Construct an iterator that points to the first element of the list.
Definition: list.h:144
KJS::List::begin
ListIterator begin() const
Definition: list.h:186
KJS::ListIterator::operator->
ValueImp * operator->() const
Dereference the iterator.
Definition: list.h:150
KJS::ListIterator::operator!=
bool operator!=(const ListIterator &it) const
Check for inequality with another iterator.
Definition: list.h:179
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:79
KJS::ListIterator::operator--
Value operator--()
Prefix decrement operator.
Definition: list.h:164
KJS::List::operator[]
Value operator[](int i) const
Equivalent to at.
Definition: list.h:112
KJS::List::size
int size() const
Definition: list.h:90
KJS::List::end
ListIterator end() const
Definition: list.h:187
KJS::ListIterator::operator++
Value operator++()
Prefix increment operator.
Definition: list.h:156
KJS::List
Native list type.
Definition: list.h:48
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86
KJS::ListIterator
Iterator for KJS::List objects.
Definition: list.h:138

KDE Home | KDE Accessibility Home | Description of Access Keys