1
#ifndef __KSCRIPT_STRUCT_H__
2
#define __KSCRIPT_STRUCT_H__
6
#include <qstringlist.h>
10
#include "kscript_value.h"
11
#include "kscript_context.h"
16
class KSStructClass : public QShared
19
typedef KSSharedPtr<KSStructClass> Ptr;
21
KSStructClass( KSModule* module, const QString& name /*, const KSParseNode* n*/ );
22
virtual ~KSStructClass() { }
24
virtual bool constructor( KSContext& c );
26
* Creates a new KSStruct of this class. The returned object
27
* has a reference count of 1.
29
KSStruct* constructor();
31
KSModule* module() { return m_module; }
33
KSNamespace* nameSpace() { return &m_space; }
34
const KSNamespace* nameSpace() const { return &m_space; }
35
virtual KSValue::Ptr member( KSContext& context, const QString& name );
37
const QStringList& vars() const { return m_vars; }
38
void addVariable( const QString& v ) { m_vars.append( v ); }
39
void setVariables( const QStringList& l ) { m_vars = l; }
40
bool hasVariable( const QString& v ) { return m_vars.contains( v ); }
43
* @return the name of the class, for example "QRect" or "QPixmap".
47
QString name() const { return m_name; }
49
* @return the name of the class with prepended name of the module like this:
50
* "qt:QRect" or "kde:KColorDialog"
54
QString fullName() const;
57
* When getting a pointer to a KSObject via @ref KSValue::objectValue this function
58
* helps to do some dynamic casting.
60
virtual bool inherits( const char* name ) { return ( strcmp( name, "KSStructClass" ) == 0 ); }
65
// const KSParseNode* m_node;
70
class KSStruct : public QShared
73
typedef KSSharedPtr<KSStruct> Ptr;
75
KSStruct( KSStructClass* c ) { m_class = c; }
76
virtual ~KSStruct() { }
79
* Implements a KScript function of the same name.
81
bool isA( KSContext& context );
83
virtual KSValue::Ptr member( KSContext&, const QString& name );
84
virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
86
const KSStructClass* getClass() const { return m_class; }
87
KSStructClass* getClass() { return m_class; }
90
* A convenience function
92
QString className() const { return m_class->name(); }
94
// ########## Torben: Make real copies of the menus.
95
virtual KSStruct* clone() { KSStruct *s = new KSStruct( m_class ); s->m_space = m_space; return s; }
97
KSModule* module() { return m_class->module(); }
98
KSNamespace* instanceNameSpace() { return &m_space; }
99
const KSNamespace* instanceNameSpace() const { return &m_space; }
102
* This function is used in @ref KSBuiltinStruct. We put that in here
103
* to avoid casting to KSBuiltinStruct all the time.
105
virtual void* object() { return 0; }
106
virtual const void* object() const { return 0; }
109
KSStructClass* m_class;
113
class KSBuiltinStruct;
115
class KSBuiltinStructClass : public KSStructClass
117
friend KSBuiltinStruct;
119
KSBuiltinStructClass( KSModule* module, const QString& name );
120
virtual ~KSBuiltinStructClass() { }
122
virtual bool constructor( KSContext& c ) = 0;
123
virtual bool destructor( void* object ) = 0;
124
virtual KSStruct* clone( KSBuiltinStruct* ) = 0;
126
typedef bool (*MethodPtr)( void* object, KSContext&, const QValueList<KSValue::Ptr>& args );
129
* @param signature is the signature of the method. Passing an empty string here means
130
* that the method does not expect any parameter while a null string means
131
* that the function will check the arguments itself.
133
void addMethod( const QString& name, MethodPtr func, const QCString& signature );
134
bool hasMethod( const QString& ) const;
136
bool call( void* instance, KSContext& context, const QString& name );
140
* It can not happen that @p name is not the name of a variable, since @ref KSBuiltinStruct
141
* checks wether @p name is really a variable of this struct before calling.
143
virtual KSValue::Ptr property( KSContext& context, void* object, const QString& name ) = 0;
145
* If the type does not match the property, you must give an exception.
146
* If the property is readonly just return 0 and dont give an exception.
148
* It can not happen that @p name is not the name of a variable, since @ref KSBuiltinStruct
149
* checks wether @p name is really a variable of this struct before calling.
151
virtual bool setProperty( KSContext& context, void* object, const QString& name, const KSValue::Ptr value ) = 0;
157
QCString m_signature;
160
QMap<QString,Method> m_methods;
164
class KSBuiltinStruct : public KSStruct
167
KSBuiltinStruct( KSStructClass* c, void* object );
169
* Destroys the struct and the associated C++ object.
171
* @see KSBuiltinStructClass::destructor
173
virtual ~KSBuiltinStruct();
175
virtual KSValue::Ptr member( KSContext&, const QString& name );
176
virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
179
* This is the universal method dispatcher.
181
* @see KSBuiltinStructClass::call
183
bool call( KSContext& context, const QString& name );
186
* Make a real copy of the struct. That means that the C++ object
189
* @see KSBuiltinStructClass::clone
194
* @return a pointer to the C++ object that holds the real data of this struct.
197
const void* object() const;