• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
value.h
1 // -*- c-basic-offset: 2 -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5  * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6  * Copyright (C) 2003 Apple Computer, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24 
25 #ifndef _KJS_VALUE_H_
26 #define _KJS_VALUE_H_
27 
28 #include <stdlib.h> // Needed for size_t
29 
30 #include "ustring.h"
31 #include "simple_number.h"
32 
33 // Primitive data types
34 
35 namespace KJS {
36 
37  class Value;
38  class ValueImp;
39  class ValueImpPrivate;
40  class Undefined;
41  class UndefinedImp;
42  class Null;
43  class NullImp;
44  class Boolean;
45  class BooleanImp;
46  class String;
47  class StringImp;
48  class Number;
49  class NumberImp;
50  class Object;
51  class ObjectImp;
52  class Reference;
53  class List;
54  class ListImp;
55  class Completion;
56  class ExecState;
57 
61  enum Type {
62  UnspecifiedType = 0,
63  UndefinedType = 1,
64  NullType = 2,
65  BooleanType = 3,
66  StringType = 4,
67  NumberType = 5,
68  ObjectType = 6
69  };
70 
79  class KJS_EXPORT ValueImp {
80  friend class Collector;
81  friend class Value;
82  friend class ContextImp;
83  public:
84  ValueImp();
85  virtual ~ValueImp();
86 
87  ValueImp* ref() { if (!SimpleNumber::is(this)) refcount++; return this; }
88  bool deref() { if (SimpleNumber::is(this)) return false; else return (!--refcount); }
89 
90  virtual void mark();
91  bool marked() const;
92  void* operator new(size_t);
93  void operator delete(void*);
94 
100  void setGcAllowed();
101 
102  // Will crash if called on a simple number.
103  void setGcAllowedFast() { _flags |= VI_GCALLOWED; }
104 
105  int toInteger(ExecState *exec) const;
106  int toInt32(ExecState *exec) const;
107  unsigned int toUInt32(ExecState *exec) const;
108  unsigned short toUInt16(ExecState *exec) const;
109 
110  // Dispatch wrappers that handle the special small number case
111 
112  Type dispatchType() const;
113  Value dispatchToPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;
114  bool dispatchToBoolean(ExecState *exec) const;
115  double dispatchToNumber(ExecState *exec) const;
116  UString dispatchToString(ExecState *exec) const;
117  bool dispatchToUInt32(unsigned&) const;
118  Object dispatchToObject(ExecState *exec) const;
119 
120  unsigned short int refcount;
121 
122  bool isDestroyed() const { return _flags & VI_DESTRUCTED; }
123 
124  private:
125  unsigned short int _flags;
126 
127  virtual Type type() const = 0;
128 
129  // The conversion operations
130 
131  virtual Value toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;
132  virtual bool toBoolean(ExecState *exec) const = 0;
133  virtual double toNumber(ExecState *exec) const = 0;
134  // TODO: no need for the following 4 int conversions to be virtual
135  virtual UString toString(ExecState *exec) const = 0;
136  virtual Object toObject(ExecState *exec) const = 0;
137  virtual bool toUInt32(unsigned&) const;
138 
139  enum {
140  VI_MARKED = 1,
141  VI_GCALLOWED = 2,
142  VI_CREATED = 4,
143  VI_DESTRUCTED = 8 // nice word we have here :)
144  }; // VI means VALUEIMPL
145 
146  ValueImpPrivate *_vd;
147 
148  // Give a compile time error if we try to copy one of these.
149  ValueImp(const ValueImp&);
150  ValueImp& operator=(const ValueImp&);
151  };
152 
168  class KJS_EXPORT Value {
169  public:
170  Value() : rep(0) { }
171  explicit Value(ValueImp *v);
172  Value(const Value &v);
173  ~Value();
174 
175  Value& operator=(const Value &v);
182  bool isValid() const { return rep != 0; }
187  bool isNull() const { return rep == 0; }
188  ValueImp *imp() const { return rep; }
189 
196  Type type() const { return rep->dispatchType(); }
197 
204  bool isA(Type t) const { return rep->dispatchType() == t; }
205 
210  Value toPrimitive(ExecState *exec,
211  Type preferredType = UnspecifiedType) const
212  { return rep->dispatchToPrimitive(exec, preferredType); }
213 
217  bool toBoolean(ExecState *exec) const { return rep->dispatchToBoolean(exec); }
218 
222  double toNumber(ExecState *exec) const { return rep->dispatchToNumber(exec); }
223 
227  int toInteger(ExecState *exec) const { return rep->toInteger(exec); }
228 
232  int toInt32(ExecState *exec) const { return rep->toInt32(exec); }
233 
237  unsigned int toUInt32(ExecState *exec) const { return rep->toUInt32(exec); }
238 
242  unsigned short toUInt16(ExecState *exec) const { return rep->toUInt16(exec); }
243 
247  UString toString(ExecState *exec) const { return rep->dispatchToString(exec); }
248 
252  Object toObject(ExecState *exec) const;
253 
257  bool toUInt32(unsigned& i) const { return rep->dispatchToUInt32(i); }
258 
259  protected:
260  ValueImp *rep;
261  };
262 
263  // Primitive types
264 
270  class KJS_EXPORT Undefined : public Value {
271  public:
272  Undefined();
273 
283  static Undefined dynamicCast(const Value &v);
284  private:
285  friend class UndefinedImp;
286  explicit Undefined(UndefinedImp *v);
287 
288  };
289 
295  class KJS_EXPORT Null : public Value {
296  public:
297  Null();
298 
308  static Null dynamicCast(const Value &v);
309  private:
310  friend class NullImp;
311  explicit Null(NullImp *v);
312  };
313 
317  class KJS_EXPORT Boolean : public Value {
318  public:
319  Boolean(bool b = false);
320 
330  static Boolean dynamicCast(const Value &v);
331 
332  bool value() const;
333  private:
334  friend class BooleanImp;
335  explicit Boolean(BooleanImp *v);
336  };
337 
341  class KJS_EXPORT String : public Value {
342  public:
343  String(const UString &s = "");
344 
354  static String dynamicCast(const Value &v);
355 
356  UString value() const;
357  private:
358  friend class StringImp;
359  explicit String(StringImp *v);
360  };
361 
362  extern const double NaN;
363  extern const double Inf;
364 
368  class KJS_EXPORT Number : public Value {
369  friend class ValueImp;
370  public:
371  Number(int i);
372  Number(unsigned int u);
373  Number(double d = 0.0);
374  Number(long int l);
375  Number(long unsigned int l);
376 
377  double value() const;
378  int intValue() const;
379 
380  bool isNaN() const;
381  bool isInf() const;
382 
392  static Number dynamicCast(const Value &v);
393  private:
394  friend class NumberImp;
395  explicit Number(NumberImp *v);
396  };
397 
398 } // namespace
399 
400 #endif // _KJS_VALUE_H_

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |