value.h
// -*- c-basic-offset: 2 -*-
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* 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_VALUE_H_
#define _KJS_VALUE_H_
#include <stdlib.h> // Needed for size_t
#include "ustring.h"
#include "simple_number.h"
// Primitive data types
namespace KJS {
class Value;
class ValueImp;
class ValueImpPrivate;
class Undefined;
class UndefinedImp;
class Null;
class NullImp;
class Boolean;
class BooleanImp;
class String;
class StringImp;
class Number;
class NumberImp;
class Object;
class ObjectImp;
class Reference;
class List;
class ListImp;
class Completion;
class ExecState;
enum Type {
UnspecifiedType = 0,
UndefinedType = 1,
NullType = 2,
BooleanType = 3,
StringType = 4,
NumberType = 5,
ObjectType = 6
};
class KJS_EXPORT ValueImp {
friend class Collector;
friend class Value;
friend class ContextImp;
public:
ValueImp();
virtual ~ValueImp();
ValueImp* ref() { if (!SimpleNumber::is(this)) refcount++; return this; }
bool deref() { if (SimpleNumber::is(this)) return false; else return (!--refcount); }
virtual void mark();
bool marked() const;
void* operator new(size_t);
void operator delete(void*);
void setGcAllowed();
// Will crash if called on a simple number.
void setGcAllowedFast() { _flags |= VI_GCALLOWED; }
int toInteger(ExecState *exec) const;
int toInt32(ExecState *exec) const;
unsigned int toUInt32(ExecState *exec) const;
unsigned short toUInt16(ExecState *exec) const;
// Dispatch wrappers that handle the special small number case
Type dispatchType() const;
Value dispatchToPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
bool dispatchToBoolean(ExecState *exec) const;
double dispatchToNumber(ExecState *exec) const;
UString dispatchToString(ExecState *exec) const;
bool dispatchToUInt32(unsigned&) const;
Object dispatchToObject(ExecState *exec) const;
unsigned short int refcount;
bool isDestroyed() const { return _flags & VI_DESTRUCTED; }
private:
unsigned short int _flags;
virtual Type type() const = 0;
// The conversion operations
virtual Value toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
virtual bool toBoolean(ExecState *exec) const = 0;
virtual double toNumber(ExecState *exec) const = 0;
// TODO: no need for the following 4 int conversions to be virtual
virtual UString toString(ExecState *exec) const = 0;
virtual Object toObject(ExecState *exec) const = 0;
virtual bool toUInt32(unsigned&) const;
enum {
VI_MARKED = 1,
VI_GCALLOWED = 2,
VI_CREATED = 4,
VI_DESTRUCTED = 8 // nice word we have here :)
}; // VI means VALUEIMPL
ValueImpPrivate *_vd;
// Give a compile time error if we try to copy one of these.
ValueImp(const ValueImp&);
ValueImp& operator=(const ValueImp&);
};
class KJS_EXPORT Value {
public:
Value() : rep(0) { }
explicit Value(ValueImp *v);
Value(const Value &v);
~Value();
Value& operator=(const Value &v);
bool isValid() const { return rep != 0; }
bool isNull() const { return rep == 0; }
ValueImp *imp() const { return rep; }
Type type() const { return rep->dispatchType(); }
bool isA(Type t) const { return rep->dispatchType() == t; }
Value toPrimitive(ExecState *exec,
Type preferredType = UnspecifiedType) const
{ return rep->dispatchToPrimitive(exec, preferredType); }
bool toBoolean(ExecState *exec) const { return rep->dispatchToBoolean(exec); }
double toNumber(ExecState *exec) const { return rep->dispatchToNumber(exec); }
int toInteger(ExecState *exec) const { return rep->toInteger(exec); }
int toInt32(ExecState *exec) const { return rep->toInt32(exec); }
unsigned int toUInt32(ExecState *exec) const { return rep->toUInt32(exec); }
unsigned short toUInt16(ExecState *exec) const { return rep->toUInt16(exec); }
UString toString(ExecState *exec) const { return rep->dispatchToString(exec); }
Object toObject(ExecState *exec) const;
bool toUInt32(unsigned& i) const { return rep->dispatchToUInt32(i); }
protected:
ValueImp *rep;
};
// Primitive types
class KJS_EXPORT Undefined : public Value {
public:
Undefined();
static Undefined dynamicCast(const Value &v);
private:
friend class UndefinedImp;
explicit Undefined(UndefinedImp *v);
};
class KJS_EXPORT Null : public Value {
public:
Null();
static Null dynamicCast(const Value &v);
private:
friend class NullImp;
explicit Null(NullImp *v);
};
class KJS_EXPORT Boolean : public Value {
public:
Boolean(bool b = false);
static Boolean dynamicCast(const Value &v);
bool value() const;
private:
friend class BooleanImp;
explicit Boolean(BooleanImp *v);
};
class KJS_EXPORT String : public Value {
public:
String(const UString &s = "");
static String dynamicCast(const Value &v);
UString value() const;
private:
friend class StringImp;
explicit String(StringImp *v);
};
extern const double NaN;
extern const double Inf;
class KJS_EXPORT Number : public Value {
friend class ValueImp;
public:
Number(int i);
Number(unsigned int u);
Number(double d = 0.0);
Number(long int l);
Number(long unsigned int l);
double value() const;
int intValue() const;
bool isNaN() const;
bool isInf() const;
static Number dynamicCast(const Value &v);
private:
friend class NumberImp;
explicit Number(NumberImp *v);
};
} // namespace
#endif // _KJS_VALUE_H_
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:196
KJS::Number
Represents an primitive Number value.
Definition: value.h:368
KJS::Value::toBoolean
bool toBoolean(ExecState *exec) const
Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
Definition: value.h:217
KJS::Value::toUInt16
unsigned short toUInt16(ExecState *exec) const
Performs the ToUInt16 type conversion operation on this value (ECMA 9.7)
Definition: value.h:242
KJS::Null
Represents an primitive Null value.
Definition: value.h:295
KJS::ContextImp
Execution context.
Definition: context.h:35
KJS::Value::toInt32
int toInt32(ExecState *exec) const
Performs the ToInt32 type conversion operation on this value (ECMA 9.5)
Definition: value.h:232
KJS::Value::toNumber
double toNumber(ExecState *exec) const
Performs the ToNumber type conversion operation on this value (ECMA 9.3)
Definition: value.h:222
KJS::Object
Represents an Object.
Definition: object.h:82
KJS::Value::toUInt32
unsigned int toUInt32(ExecState *exec) const
Performs the ToUInt32 type conversion operation on this value (ECMA 9.6)
Definition: value.h:237
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:270
KJS::Value::isA
bool isA(Type t) const
Checks whether or not the value is of a particular tpye.
Definition: value.h:204
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::Value::toUInt32
bool toUInt32(unsigned &i) const
Checks if we can do a lossless conversion to UInt32.
Definition: value.h:257
KJS::String
Represents an primitive String value.
Definition: value.h:341
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:79
KJS::Value::toPrimitive
Value toPrimitive(ExecState *exec, Type preferredType=UnspecifiedType) const
Performs the ToPrimitive type conversion operation on this value (ECMA 9.1)
Definition: value.h:210
KJS::Boolean
Represents an primitive Boolean value.
Definition: value.h:317
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:247
KJS::Value::isNull
bool isNull() const
Definition: value.h:187
KJS::Value::toInteger
int toInteger(ExecState *exec) const
Performs the ToInteger type conversion operation on this value (ECMA 9.4)
Definition: value.h:227
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:182
KJS::Collector
Garbage collector.
Definition: collector.h:38
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439

KDE Home | KDE Accessibility Home | Description of Access Keys