value.h
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 #ifndef _KJS_VALUE_H_
00026 #define _KJS_VALUE_H_
00027 
00028 #include <stdlib.h> 
00029 
00030 #include "ustring.h"
00031 #include "simple_number.h"
00032 #include <kjs/global.h>
00033 
00034 
00035 
00036 namespace KJS {
00037 
00038   class Value;
00039   class ValueImp;
00040   class ValueImpPrivate;
00041   class Undefined;
00042   class UndefinedImp;
00043   class Null;
00044   class NullImp;
00045   class Boolean;
00046   class BooleanImp;
00047   class String;
00048   class StringImp;
00049   class Number;
00050   class NumberImp;
00051   class Object;
00052   class ObjectImp;
00053   class Reference;
00054   class List;
00055   class ListImp;
00056   class Completion;
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   };
00071 
00080   class ValueImp {
00081     friend class Collector;
00082     friend class Value;
00083     friend class ContextImp;
00084   public:
00085     ValueImp();
00086     virtual ~ValueImp();
00087 
00088     ValueImp* ref() { if (!SimpleNumber::is(this)) refcount++; return this; }
00089     bool deref() { if (SimpleNumber::is(this)) return false; else return (!--refcount); }
00090 
00091     virtual void mark();
00092     bool marked() const;
00093     void* operator new(size_t);
00094     void operator delete(void*);
00095 
00101     void setGcAllowed();
00102 
00103     
00104     void setGcAllowedFast() { _flags |= VI_GCALLOWED; }
00105 
00106     int toInteger(ExecState *exec) const;
00107     int toInt32(ExecState *exec) const;
00108     unsigned int toUInt32(ExecState *exec) const;
00109     unsigned short toUInt16(ExecState *exec) const;
00110 
00111     
00112 
00113     Type dispatchType() const;
00114     Value dispatchToPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
00115     bool dispatchToBoolean(ExecState *exec) const;
00116     double dispatchToNumber(ExecState *exec) const;
00117     UString dispatchToString(ExecState *exec) const;
00118     bool dispatchToUInt32(unsigned&) const;
00119     Object dispatchToObject(ExecState *exec) const;
00120 
00121     unsigned short int refcount;
00122 
00123     bool isDestroyed() const { return _flags & VI_DESTRUCTED; }
00124 
00125   private:
00126     unsigned short int _flags;
00127 
00128     virtual Type type() const = 0;
00129 
00130     
00131 
00132     virtual Value toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
00133     virtual bool toBoolean(ExecState *exec) const = 0;
00134     virtual double toNumber(ExecState *exec) const = 0;
00135     
00136     virtual UString toString(ExecState *exec) const = 0;
00137     virtual Object toObject(ExecState *exec) const = 0;
00138     virtual bool toUInt32(unsigned&) const;
00139 
00140     enum {
00141       VI_MARKED = 1,
00142       VI_GCALLOWED = 2,
00143       VI_CREATED = 4,
00144       VI_DESTRUCTED = 8   
00145     }; 
00146 
00147     ValueImpPrivate *_vd;
00148 
00149     
00150     ValueImp(const ValueImp&);
00151     ValueImp& operator=(const ValueImp&);
00152   };
00153 
00169   class Value {
00170   public:
00171     Value() : rep(0) { }
00172     explicit Value(ValueImp *v);
00173     Value(const Value &v);
00174     ~Value();
00175 
00176     Value& operator=(const Value &v);
00183     bool isValid() const { return rep != 0; }
00188     bool isNull() const { return rep == 0; }
00189     ValueImp *imp() const { return rep; }
00190 
00197     Type type() const { return rep->dispatchType(); }
00198 
00205     bool isA(Type t) const { return rep->dispatchType() == t; }
00206 
00211     Value toPrimitive(ExecState *exec,
00212                       Type preferredType = UnspecifiedType) const
00213       { return rep->dispatchToPrimitive(exec, preferredType); }
00214 
00218     bool toBoolean(ExecState *exec) const { return rep->dispatchToBoolean(exec); }
00219 
00223     double toNumber(ExecState *exec) const { return rep->dispatchToNumber(exec); }
00224 
00228     int toInteger(ExecState *exec) const { return rep->toInteger(exec); }
00229 
00233     int toInt32(ExecState *exec) const { return rep->toInt32(exec); }
00234 
00238     unsigned int toUInt32(ExecState *exec) const { return rep->toUInt32(exec); }
00239 
00243     unsigned short toUInt16(ExecState *exec) const { return rep->toUInt16(exec); }
00244 
00248     UString toString(ExecState *exec) const { return rep->dispatchToString(exec); }
00249 
00253     Object toObject(ExecState *exec) const;
00254 
00258     bool toUInt32(unsigned& i) const { return rep->dispatchToUInt32(i); }
00259 
00260   protected:
00261     ValueImp *rep;
00262   };
00263 
00264   
00265 
00271   class Undefined : public Value {
00272   public:
00273     Undefined();
00274 
00284     static Undefined dynamicCast(const Value &v);
00285   private:
00286     friend class UndefinedImp;
00287     explicit Undefined(UndefinedImp *v);
00288 
00289   };
00290 
00296   class Null : public Value {
00297   public:
00298     Null();
00299 
00309     static Null dynamicCast(const Value &v);
00310   private:
00311     friend class NullImp;
00312     explicit Null(NullImp *v);
00313   };
00314 
00318   class Boolean : public Value {
00319   public:
00320     Boolean(bool b = false);
00321 
00331     static Boolean dynamicCast(const Value &v);
00332 
00333     bool value() const;
00334   private:
00335     friend class BooleanImp;
00336     explicit Boolean(BooleanImp *v);
00337   };
00338 
00342   class String : public Value {
00343   public:
00344     String(const UString &s = "");
00345 
00355     static String dynamicCast(const Value &v);
00356 
00357     UString value() const;
00358   private:
00359     friend class StringImp;
00360     explicit String(StringImp *v);
00361   };
00362 
00363   extern const double NaN;
00364   extern const double Inf;
00365 
00369   class Number : public Value {
00370     friend class ValueImp;
00371   public:
00372     Number(int i);
00373     Number(unsigned int u);
00374     Number(double d = 0.0);
00375     Number(long int l);
00376     Number(long unsigned int l);
00377 
00378     double value() const;
00379     int intValue() const;
00380 
00381     bool isNaN() const;
00382     bool isInf() const;
00383 
00393     static Number dynamicCast(const Value &v);
00394   private:
00395     friend class NumberImp;
00396     explicit Number(NumberImp *v);
00397   };
00398 
00399 } 
00400 
00401 #endif // _KJS_VALUE_H_
 
This file is part of the documentation for kjs Library Version 3.2.0.