kjs Library API Documentation

value.h

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Lesser General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Lesser General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Lesser General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  *  Boston, MA 02111-1307, USA.
00021  *
00022  */
00023 
00024 #ifndef _KJS_VALUE_H_
00025 #define _KJS_VALUE_H_
00026 
00027 #include <stdlib.h> // Needed for size_t
00028 
00029 #include "ustring.h"
00030 #include <kjs/global.h>
00031 
00032 // Primitive data types
00033 
00034 namespace KJS {
00035 
00036   class Value;
00037   class ValueImp;
00038   class ValueImpPrivate;
00039   class Undefined;
00040   class UndefinedImp;
00041   class Null;
00042   class NullImp;
00043   class Boolean;
00044   class BooleanImp;
00045   class String;
00046   class StringImp;
00047   class Number;
00048   class NumberImp;
00049   class Object;
00050   class ObjectImp;
00051   class Reference;
00052   class ReferenceImp;
00053   class List;
00054   class ListImp;
00055   class Completion;
00056   class CompletionImp;
00057   class ExecState;
00058 
00062   enum Type {
00063     UnspecifiedType = 0,
00064     UndefinedType   = 1,
00065     NullType        = 2,
00066     BooleanType     = 3,
00067     StringType      = 4,
00068     NumberType      = 5,
00069     ObjectType      = 6,
00070     ReferenceType   = 7,
00071     ListType        = 8,
00072     CompletionType  = 9
00073   };
00074 
00083   class ValueImp {
00084     friend class Collector;
00085   public:
00086     ValueImp();
00087     virtual ~ValueImp();
00088 
00089     inline ValueImp* ref() { refcount++; return this; }
00090     inline bool deref() { return (!--refcount); }
00091     unsigned int refcount;
00092 
00093     virtual void mark();
00094     bool marked() const;
00095     void* operator new(size_t);
00096     void operator delete(void*);
00097 
00103     void setGcAllowed(); // TODO: inline, remove function below
00104     void inlinedSetGcAllowed() { _flags |= VI_GCALLOWED; }
00105 
00106     virtual Type type() const = 0;
00107 
00108     // The conversion operations
00109 
00110     virtual Value toPrimitive(ExecState *exec,
00111                               Type preferredType = UnspecifiedType) const = 0;
00112     virtual bool toBoolean(ExecState *exec) const = 0;
00113     virtual double toNumber(ExecState *exec) const = 0;
00114     // TODO: no need for the following 4 int conversions to be virtual
00115     virtual int toInteger(ExecState *exec) const;
00116     virtual int toInt32(ExecState *exec) const;
00117     virtual unsigned int toUInt32(ExecState *exec) const;
00118     virtual unsigned short toUInt16(ExecState *exec) const;
00119     virtual UString toString(ExecState *exec) const = 0;
00120     virtual Object toObject(ExecState *exec) const = 0;
00121 
00122     // Reference operations
00123 
00124     virtual Value getBase(ExecState *exec) const;
00125     virtual UString getPropertyName(ExecState *exec) const;
00126     virtual Value getValue(ExecState *exec) const;
00127     virtual void putValue(ExecState *exec, const Value w);
00128 
00129   private:
00130     enum {
00131       VI_MARKED = 1,
00132       VI_GCALLOWED = 2,
00133       VI_CREATED = 4,
00134       VI_DESTRUCTED = 8
00135     }; // VI means VALUEIMPL
00136 
00137     ValueImpPrivate *_vd;
00138     unsigned int _flags;
00139   };
00140 
00156   class Value {
00157   public:
00158     Value();
00159     explicit Value(ValueImp *v);
00160     Value(const Value &v);
00161     virtual ~Value();       // TODO: virtual makes no sense. just slower.
00162 
00163     Value& operator=(const Value &v);
00170     bool isValid() const;
00175     bool isNull() const;
00176     ValueImp *imp() const;
00177 
00185     Type type() const;
00186 
00193     bool isA(Type t) const;
00194 
00199     Value toPrimitive(ExecState *exec,
00200                       Type preferredType = UnspecifiedType) const;
00201 
00205     bool toBoolean(ExecState *exec) const;
00206 
00210     double toNumber(ExecState *exec) const;
00211 
00215     int toInteger(ExecState *exec) const;
00216 
00220     int toInt32(ExecState *exec) const;
00221 
00225     unsigned int toUInt32(ExecState *exec) const;
00226 
00230     unsigned short toUInt16(ExecState *exec) const;
00231 
00235     UString toString(ExecState *exec) const;
00236 
00240     Object toObject(ExecState *exec) const;
00241 
00248     Value getBase(ExecState *exec) const;
00249 
00254     UString getPropertyName(ExecState *exec) const;
00255 
00260     Value getValue(ExecState *exec) const;
00261 
00266     void putValue(ExecState *exec, const Value w);
00267 
00268   protected:
00269     ValueImp *rep;
00270   };
00271 
00272   bool operator==(const Value &v1, const Value &v2);
00273   bool operator!=(const Value &v1, const Value &v2);
00274 
00275   // Primitive types
00276 
00282   class Undefined : public Value {
00283   public:
00284     Undefined();
00285     Undefined(const Undefined &v);
00286     virtual ~Undefined();
00287 
00288     Undefined& operator=(const Undefined &v);
00289 
00299     static Undefined dynamicCast(const Value &v);
00300   private:
00301     friend class UndefinedImp;
00302     explicit Undefined(UndefinedImp *v);
00303 
00304   };
00305 
00311   class Null : public Value {
00312   public:
00313     Null();
00314     Null(const Null &v);
00315     virtual ~Null();
00316 
00317     Null& operator=(const Null &v);
00318 
00328     static Null dynamicCast(const Value &v);
00329   private:
00330     friend class NullImp;
00331     explicit Null(NullImp *v);
00332   };
00333 
00337   class Boolean : public Value {
00338   public:
00339     Boolean(bool b = false);
00340     Boolean(const Boolean &v);
00341     virtual ~Boolean();
00342 
00343     Boolean& operator=(const Boolean &v);
00344 
00354     static Boolean dynamicCast(const Value &v);
00355 
00356     bool value() const;
00357   private:
00358     friend class BooleanImp;
00359     explicit Boolean(BooleanImp *v);
00360   };
00361 
00365   class String : public Value {
00366   public:
00367     String(const UString &s = "");
00368     String(const String &v);
00369     virtual ~String();
00370 
00371     String& operator=(const String &v);
00372 
00382     static String dynamicCast(const Value &v);
00383 
00384     UString value() const;
00385   private:
00386     friend class StringImp;
00387     explicit String(StringImp *v);
00388   };
00389 
00390   extern const double NaN;
00391   extern const double Inf;
00392 
00396   class Number : public Value {
00397   public:
00398     Number(int i);
00399     Number(unsigned int u);
00400     Number(double d = 0.0);
00401     Number(long int l);
00402     Number(long unsigned int l);
00403     Number(const Number &v);
00404     virtual ~Number();
00405 
00406     Number& operator=(const Number &v);
00407 
00408     double value() const;
00409     int intValue() const;
00410 
00411     bool isNaN() const;
00412     bool isInf() const;
00413 
00423     static Number dynamicCast(const Value &v);
00424   private:
00425     friend class NumberImp;
00426     explicit Number(NumberImp *v);
00427   };
00428 
00429 }; // namespace
00430 
00431 #endif // _KJS_VALUE_H_
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:16 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001