lib Library API Documentation

koscript_value.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef __KSCRIPT_VALUE_H
00021 #define __KSCRIPT_VALUE_H
00022 
00023 #include <qstring.h>
00024 #include <qvaluelist.h>
00025 #include <qmap.h>
00026 #include <qshared.h>
00027 #include <qdatetime.h>
00028 #include <koffice_export.h>
00029 
00030 #include "koscript_ptr.h"
00031 #include "koscript_types.h"
00032 
00033 class KSFunction;
00034 class KSMethod;
00035 class KSContext;
00036 class KSProperty;
00037 class KSModule;
00038 class KSStruct;
00039 class KSStructClass;
00040 
00041 typedef bool (KSStruct::*KSStructBuiltinMethod)( KSContext&, const QString& );
00042 
00048 class KOSCRIPT_EXPORT KSValue : public QShared
00049 {
00050 public:
00051     typedef KSSharedPtr<KSValue> Ptr;
00052 
00053     enum Type {
00054       Empty,
00055       StringType,
00056       IntType,
00057       BoolType,
00058       DoubleType,
00059       ListType,
00060       MapType,
00061       CharType,
00062       CharRefType,
00063       FunctionType,
00064       MethodType,
00065       PropertyType,
00066       ModuleType,
00067       StructType,
00068       StructClassType,
00069       StructBuiltinMethodType,
00070       DateType,
00071       TimeType,
00072       NTypes
00073     };
00074 
00075     enum Mode {
00076       LeftExpr,
00077       Constant,
00078       Temp
00079     };
00080 
00081     KSValue();
00082     KSValue( Type );
00083     KSValue( const KSValue& );
00084     virtual ~KSValue();
00085 
00086     KSValue( const QString& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00087     KSValue( const QValueList<Ptr>& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00088     KSValue( const QMap<QString,Ptr>& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00089     KSValue( KScript::Long _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00090     KSValue( int _v ) { m_mode = Temp; typ = Empty; setValue( (KScript::Long)_v ); }
00091     KSValue( KScript::Boolean _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00092     KSValue( KScript::Double _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00093     KSValue( const KScript::Char& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00094     KSValue( const KScript::CharRef& _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00095     KSValue( KSFunction* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00096     KSValue( KSMethod* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00097     KSValue( KSProperty* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00098     KSValue( KSModule* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00099     KSValue( KSStruct* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00100     KSValue( KSStructClass* _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00101     KSValue( KSStructBuiltinMethod _v ) { m_mode = Temp; typ = Empty; setValue( _v ); }
00102     KSValue( const QTime& t ) { m_mode = Temp; typ = Empty; setValue( t ); }
00103     KSValue( const QDate& d ) { m_mode = Temp; typ = Empty; setValue( d ); }
00104 
00105     KSValue& operator= ( const KSValue& );
00106 
00107     void setValue( const QString& );
00108     void setValue( const QValueList<Ptr>& );
00109     void setValue( const QMap<QString,Ptr>& );
00110     void setValue( int _v ) { setValue( (KScript::Long)_v ); }
00111     void setValue( KScript::Long );
00112     void setValue( KScript::Boolean );
00113     void setValue( KScript::Double );
00114     void setValue( const KScript::Char& );
00115     void setValue( const KScript::CharRef& );
00116     void setValue( KSFunction* );
00117     void setValue( KSMethod* );
00118     void setValue( KSProperty* );
00119     void setValue( KSModule* );
00120     void setValue( KSStruct* );
00121     void setValue( KSStructClass* );
00122     void setValue( KSStructBuiltinMethod );
00123     void setValue( const QDate& );
00124     void setValue( const QTime& );
00125 
00126     void suck( KSValue* );
00127 
00128     Mode mode() const { return m_mode; }
00129     void setMode( Mode m ) { m_mode = m; }
00130 
00131     Type type() const { return typ; }
00132     virtual QString typeName() const;
00133 
00134     bool isEmpty() const { return ( typ == Empty ); }
00135 
00136     const QDate& dateValue() const { Q_ASSERT( typ == DateType ); return *((QDate*)val.ptr); }
00137     QDate& dateValue() { Q_ASSERT( typ == DateType ); return *((QDate*)val.ptr); }
00138 
00139     const QTime& timeValue() const { Q_ASSERT( typ == TimeType ); return *((QTime*)val.ptr); }
00140     QTime& timeValue() { Q_ASSERT( typ == TimeType ); return *((QTime*)val.ptr); }
00141 
00142     const QString& stringValue() const { Q_ASSERT( typ == StringType ); return *((QString*)val.ptr); }
00143     QString& stringValue() { Q_ASSERT( typ == StringType ); return *((QString*)val.ptr); }
00144     const QValueList<Ptr>& listValue() const { Q_ASSERT( typ == ListType );  return *((QValueList<Ptr>*)val.ptr); }
00145     QValueList<Ptr>& listValue() { Q_ASSERT( typ == ListType );  return *((QValueList<Ptr>*)val.ptr); }
00146     const QMap<QString,Ptr>& mapValue() const { Q_ASSERT( typ == MapType ); return *((QMap<QString,Ptr>*)val.ptr); }
00147     QMap<QString,Ptr>& mapValue() { Q_ASSERT( typ == MapType ); return *((QMap<QString,Ptr>*)val.ptr); }
00148     KScript::Long intValue() const { Q_ASSERT( typ == IntType || typ == DoubleType ); if ( typ == IntType ) return val.i; return (int)val.d; }
00149     KScript::Boolean boolValue() const { Q_ASSERT( typ == IntType || typ == DoubleType || typ == BoolType || typ == StringType );
00150                                          if ( typ == BoolType ) return val.b; if( typ == DoubleType ) return ( doubleValue() != 0 );
00151                                          if ( typ == IntType ) return intValue() != 0; return !stringValue().isEmpty(); }
00152     KScript::Double doubleValue() const { Q_ASSERT( typ == DoubleType || typ == IntType ); if ( typ == DoubleType ) return val.d;
00153                                           return (double)val.i; }
00154     KScript::Char charValue() const { if ( typ == CharRefType ) return *((KScript::CharRef*)val.ptr);
00155                                             Q_ASSERT( typ == CharType ); return QChar( val.c ); }
00156     KScript::CharRef& charRefValue() { Q_ASSERT( typ == CharRefType ); return *((KScript::CharRef*)val.ptr); }
00157     const KScript::CharRef& charRefValue() const { Q_ASSERT( typ == CharRefType ); return *((KScript::CharRef*)val.ptr); }
00158     KSFunction* functionValue() { Q_ASSERT( typ == FunctionType ); return ((KSFunction*)val.ptr); }
00159     const KSFunction* functionValue() const { Q_ASSERT( typ == FunctionType ); return ((KSFunction*)val.ptr); }
00160     KSMethod* methodValue() { Q_ASSERT( typ == MethodType ); return ((KSMethod*)val.ptr); }
00161     const KSMethod* methodValue() const { Q_ASSERT( typ == MethodType ); return ((KSMethod*)val.ptr); }
00162     KSProperty* propertyValue() { Q_ASSERT( typ == PropertyType ); return ((KSProperty*)val.ptr); }
00163     const KSProperty* propertyValue() const { Q_ASSERT( typ == PropertyType ); return ((KSProperty*)val.ptr); }
00164     KSModule* moduleValue() { Q_ASSERT( typ == ModuleType ); return ((KSModule*)val.ptr); }
00165     const KSModule* moduleValue() const { Q_ASSERT( typ == ModuleType ); return ((KSModule*)val.ptr); }
00166     KSStructClass* structClassValue() { Q_ASSERT( typ == StructClassType ); return ((KSStructClass*)val.ptr); }
00167     const KSStructClass* structClassValue() const { Q_ASSERT( typ == StructClassType ); return ((KSStructClass*)val.ptr); }
00168     KSStruct* structValue() { Q_ASSERT( typ == StructType ); return ((KSStruct*)val.ptr); }
00169     const KSStruct* structValue() const { Q_ASSERT( typ == StructType ); return ((KSStruct*)val.ptr); }
00170     KSStructBuiltinMethod structBuiltinMethodValue() { Q_ASSERT( typ == StructBuiltinMethodType ); return val.sm; }
00171 
00175     bool cast( Type );
00176 
00181     bool implicitCast( Type typ ) const;
00182 
00183     QString toString( KSContext& context );
00184 
00185     bool operator==( const KSValue& v ) const;
00186 
00187     bool cmp( const KSValue& v ) const;
00188 
00192     void clear();
00193 
00194     static QString typeToName( Type _typ );
00198     static Type nameToType( const QString& _name );
00199 
00204     static KSValue* null() { if ( !s_null ) s_null = new KSValue; s_null->ref(); return s_null; }
00205 
00206 protected:
00207 
00208     Mode m_mode;
00209     Type typ;
00210     union
00211     {
00212     KScript::Long i;
00213     KScript::Boolean b;
00214     KScript::Double d;
00215     ushort c;
00216     void *ptr;
00217     KSStructBuiltinMethod sm;
00218     QDate* date;
00219     QTime* time;
00220     } val;
00221 
00222 private:
00223     static void initTypeNameMap();
00224     static KSValue* s_null;
00225 };
00226 
00227 #endif
00228 
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:07 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003