scope_chain.h
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 2003 Apple Computer, Inc. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #ifndef KJS_SCOPE_CHAIN_H 00023 #define KJS_SCOPE_CHAIN_H 00024 00025 #include "global.h" 00026 00027 namespace KJS { 00028 00029 class ObjectImp; 00030 00034 class KJS_EXPORT ScopeChainNode { 00035 public: 00036 ScopeChainNode(ScopeChainNode *n, ObjectImp *o) 00037 : next(n), object(o), refCount(1) { } 00038 00039 ScopeChainNode *next; 00040 ObjectImp *object; 00041 int refCount; 00042 }; 00043 00047 class KJS_EXPORT ScopeChain { 00048 public: 00049 ScopeChain() : _node(0) { } 00050 ~ScopeChain() { deref(); } 00051 00052 ScopeChain(const ScopeChain &c) : _node(c._node) 00053 { if (_node) ++_node->refCount; } 00054 ScopeChain &operator=(const ScopeChain &); 00055 00056 bool isEmpty() const { return !_node; } 00057 ObjectImp *top() const { return _node->object; } 00058 ObjectImp *bottom() const { const ScopeChainNode *n = _node; 00059 while (n->next) n = n->next; 00060 return n->object; } 00061 00062 void clear() { deref(); _node = 0; } 00063 void push(ObjectImp *); 00064 void pop(); 00065 00066 void mark(); 00067 00068 private: 00069 ScopeChainNode *_node; 00070 00071 void deref() { if (_node && --_node->refCount == 0) release(); } 00072 void ref() const; 00073 00074 void release(); 00075 }; 00076 00077 } // namespace KJS 00078 00079 #endif // KJS_SCOPE_CHAIN_H