koscript_struct.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "koscript_struct.h"
00021 #include "koscript_util.h"
00022 #include "koscript_property.h"
00023 #include "koscript_method.h"
00024 #include "koscript.h"
00025
00026 #include <klocale.h>
00027
00028
00029
00030
00031
00032
00033
00034 KSStructClass::KSStructClass( KSModule* m, const QString& name )
00035 : m_name( name ), m_module( m )
00036 {
00037 m_space.insert( "isA", new KSValue( &KSStruct::isA ) );
00038 }
00039
00040 bool KSStructClass::constructor( KSContext& context )
00041 {
00042 context.setValue( new KSValue( constructor() ) );
00043
00044 return true;
00045 }
00046
00047 KSStruct* KSStructClass::constructor()
00048 {
00049 return new KSStruct( this );
00050 }
00051
00052 KSValue::Ptr KSStructClass::member( KSContext& context, const QString& name )
00053 {
00054 KSNamespace::Iterator it = m_space.find( name );
00055 if ( it == m_space.end() )
00056 {
00057 QString tmp( i18n("Unknown symbol '%1' in struct of type %2 of module '%3'") );
00058 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( m_name ).arg( module()->name() ) ) );
00059 return 0;
00060 }
00061
00062 return it.data();
00063 }
00064
00065 QString KSStructClass::fullName() const
00066 {
00067 return ( m_module->name() + ":" + m_name );
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 bool KSStruct::isA( KSContext& context )
00077 {
00078 if ( !KSUtil::checkArgumentsCount( context, 0, "Struct::isA" ) )
00079 return false;
00080
00081 context.setValue( new KSValue( m_class->name() ) );
00082
00083 return true;
00084 }
00085
00086 KSValue::Ptr KSStruct::member( KSContext& context, const QString& name )
00087 {
00088 if ( context.leftExpr() )
00089 {
00090 this->ref();
00091 KSValue::Ptr ptr( new KSValue( new KSProperty( this, name ) ) );
00092 ptr->setMode( KSValue::LeftExpr );
00093 return ptr;
00094 }
00095
00096 KSNamespace::Iterator it = m_space.find( name );
00097 if ( it != m_space.end() )
00098 return it.data();
00099
00100 it = m_class->nameSpace()->find( name );
00101 if ( it != m_class->nameSpace()->end() )
00102 return it.data();
00103
00104 QString tmp( i18n("Unknown symbol '%1' in object of struct '%2'") );
00105 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( getClass()->name() ) ) );
00106 return 0;
00107 }
00108
00109 bool KSStruct::setMember( KSContext& context, const QString& name, const KSValue::Ptr& v )
00110 {
00111 if ( !m_class->vars().contains( name ) )
00112 {
00113 QString tmp( i18n("Unknown symbol '%1' in object of struct '%2'") );
00114 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( getClass()->name() ) ) );
00115 return false;
00116 }
00117
00118 m_space.insert( name, v );
00119
00120 return true;
00121 }
00122
00123 bool KSBuiltinStructClass::hasMethod( const QString& name ) const
00124 {
00125 return m_methods.contains( name );
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 KSBuiltinStructClass::KSBuiltinStructClass( KSModule* module, const QString& name )
00135 : KSStructClass( module, name )
00136 {
00137 }
00138
00139 void KSBuiltinStructClass::addMethod( const QString& name, KSBuiltinStructClass::MethodPtr m, const QCString& signature )
00140 {
00141 Method s;
00142 s.m_method = m;
00143 s.m_signature = signature;
00144 m_methods[ name ] = s;
00145 }
00146
00147 bool KSBuiltinStructClass::call( void* object, KSContext& context, const QString& name )
00148 {
00149 QMap<QString,Method>::Iterator it = m_methods.find( name );
00150 Q_ASSERT( it != m_methods.end() );
00151
00152 if ( !it.data().m_signature.isNull() )
00153 if ( !KSUtil::checkArgs( context, it.data().m_signature, name, TRUE ) )
00154 return FALSE;
00155
00156 return it.data().m_method( object, context, context.value()->listValue() );
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 KSBuiltinStruct::KSBuiltinStruct( KSStructClass* c, void* object )
00166 : KSStruct( c )
00167 {
00168 m_object = object;
00169 }
00170
00171 KSBuiltinStruct::~KSBuiltinStruct()
00172 {
00173 ((KSBuiltinStructClass*)getClass())->destructor( m_object );
00174 }
00175
00176 KSValue::Ptr KSBuiltinStruct::member( KSContext& context, const QString& name )
00177 {
00178 if ( context.leftExpr() )
00179 {
00180 this->ref();
00181 KSValue::Ptr ptr( new KSValue( new KSProperty( this, name ) ) );
00182 ptr->setMode( KSValue::LeftExpr );
00183 return ptr;
00184 }
00185
00186
00187 if ( ((KSBuiltinStructClass*)getClass())->hasMethod( name ) )
00188 return ( new KSValue( (KSStructBuiltinMethod)&KSBuiltinStruct::call ) );
00189
00190
00191 KSNamespace::Iterator it = getClass()->nameSpace()->find( name );
00192 if ( it != getClass()->nameSpace()->end() )
00193 return it.data();
00194
00195
00196 if ( getClass()->hasVariable( name ) )
00197 return( ((KSBuiltinStructClass*)getClass())->property( context, m_object, name ) );
00198
00199 QString tmp( i18n("Unknown symbol '%1' in object of struct '%2'") );
00200 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( getClass()->name() ) ) );
00201 return 0;
00202 }
00203
00204 bool KSBuiltinStruct::setMember( KSContext& context, const QString& name, const KSValue::Ptr& v )
00205 {
00206 if ( !getClass()->vars().contains( name ) )
00207 {
00208 QString tmp( i18n("Unknown variable '%1' in object of struct '%2'") );
00209 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( getClass()->name() ) ) );
00210 return FALSE;
00211 }
00212
00213 bool b = ((KSBuiltinStructClass*)getClass())->setProperty( context, m_object, name, v );
00214
00215
00216 if ( !b && context.exception() )
00217 return FALSE;
00218
00219 if ( !b )
00220 {
00221 QString tmp( i18n("The variable '%1' in object of struct '%2' is readonly") );
00222 context.setException( new KSException( "ReadOnly", tmp.arg( name ).arg( getClass()->name() ) ) );
00223 return FALSE;
00224 }
00225
00226 return TRUE;
00227 }
00228
00229 bool KSBuiltinStruct::call( KSContext& context, const QString& name )
00230 {
00231 return ((KSBuiltinStructClass*)getClass())->call( m_object, context, name );
00232 }
00233
00234 KSStruct* KSBuiltinStruct::clone()
00235 {
00236 return ((KSBuiltinStructClass*)getClass())->clone( this );
00237 }
00238
00239 void* KSBuiltinStruct::object()
00240 {
00241 return m_object;
00242 }
00243
00244 const void* KSBuiltinStruct::object() const
00245 {
00246 return m_object;
00247 }
This file is part of the documentation for lib Library Version 1.3.5.