~ubuntu-branches/ubuntu/edgy/koffice/edgy-updates

« back to all changes in this revision

Viewing changes to lib/kscript/kscript_struct.h

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040509113300-xi5t1z4yxe7n03x7
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef __KSCRIPT_STRUCT_H__
2
 
#define __KSCRIPT_STRUCT_H__
3
 
 
4
 
#include <qshared.h>
5
 
#include <qstring.h>
6
 
#include <qstringlist.h>
7
 
#include <qcstring.h>
8
 
#include <qmap.h>
9
 
 
10
 
#include "kscript_value.h"
11
 
#include "kscript_context.h"
12
 
 
13
 
class KSParseNode;
14
 
class KSStruct;
15
 
 
16
 
class KSStructClass : public QShared
17
 
{
18
 
public:
19
 
  typedef KSSharedPtr<KSStructClass> Ptr;
20
 
 
21
 
  KSStructClass( KSModule* module, const QString& name /*, const KSParseNode* n*/ );
22
 
  virtual ~KSStructClass() { }
23
 
 
24
 
  virtual bool constructor( KSContext& c );
25
 
  /**
26
 
   * Creates a new KSStruct of this class. The returned object
27
 
   * has a reference count of 1.
28
 
   */
29
 
  KSStruct* constructor();
30
 
 
31
 
  KSModule* module() { return m_module; }
32
 
 
33
 
  KSNamespace* nameSpace() { return &m_space; }
34
 
  const KSNamespace* nameSpace() const { return &m_space; }
35
 
  virtual KSValue::Ptr member( KSContext& context, const QString& name );
36
 
 
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 ); }
41
 
 
42
 
  /**
43
 
   * @return the name of the class, for example "QRect" or "QPixmap".
44
 
   *
45
 
   * @see #fullName
46
 
   */
47
 
  QString name() const { return m_name; }
48
 
  /**
49
 
   * @return the name of the class with prepended name of the module like this:
50
 
   * "qt:QRect" or "kde:KColorDialog"
51
 
   *
52
 
   * @see #name
53
 
   */
54
 
  QString fullName() const;
55
 
 
56
 
  /**
57
 
   * When getting a pointer to a KSObject via @ref KSValue::objectValue this function
58
 
   * helps to do some dynamic casting.
59
 
   */
60
 
  virtual bool inherits( const char* name ) { return ( strcmp( name, "KSStructClass" ) == 0 ); }
61
 
 
62
 
private:
63
 
  QString m_name;
64
 
  KSNamespace m_space;
65
 
    // const KSParseNode* m_node;
66
 
  QStringList m_vars;
67
 
  KSModule* m_module;
68
 
};
69
 
 
70
 
class KSStruct : public QShared
71
 
{
72
 
public:
73
 
  typedef KSSharedPtr<KSStruct> Ptr;
74
 
 
75
 
  KSStruct( KSStructClass* c ) { m_class = c; }
76
 
  virtual ~KSStruct() { }
77
 
 
78
 
  /**
79
 
   * Implements a KScript function of the same name.
80
 
   */
81
 
  bool isA( KSContext& context );
82
 
 
83
 
  virtual KSValue::Ptr member( KSContext&, const QString& name );
84
 
  virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
85
 
 
86
 
  const KSStructClass* getClass() const { return m_class; }
87
 
  KSStructClass* getClass() { return m_class; }
88
 
 
89
 
  /**
90
 
   * A convenience function
91
 
   */
92
 
  QString className() const { return m_class->name(); }
93
 
    
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; }
96
 
 
97
 
  KSModule* module() { return m_class->module(); }
98
 
  KSNamespace* instanceNameSpace() { return &m_space; }
99
 
  const KSNamespace* instanceNameSpace() const { return &m_space; }
100
 
 
101
 
  /**
102
 
   * This function is used in @ref KSBuiltinStruct. We put that in here
103
 
   * to avoid casting to KSBuiltinStruct all the time.
104
 
   */
105
 
  virtual void* object() { return 0; }
106
 
  virtual const void* object() const { return 0; }
107
 
 
108
 
private:
109
 
  KSStructClass* m_class;
110
 
  KSNamespace m_space;
111
 
};
112
 
 
113
 
class KSBuiltinStruct;
114
 
 
115
 
class KSBuiltinStructClass : public KSStructClass
116
 
{
117
 
    friend KSBuiltinStruct;
118
 
public:
119
 
    KSBuiltinStructClass( KSModule* module, const QString& name );
120
 
    virtual ~KSBuiltinStructClass() { }
121
 
 
122
 
    virtual bool constructor( KSContext& c ) = 0;
123
 
    virtual bool destructor( void* object ) = 0;
124
 
    virtual KSStruct* clone( KSBuiltinStruct* ) = 0;
125
 
 
126
 
    typedef bool (*MethodPtr)( void* object, KSContext&, const QValueList<KSValue::Ptr>& args );
127
 
 
128
 
    /**
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.
132
 
     */
133
 
    void addMethod( const QString& name, MethodPtr func, const QCString& signature );
134
 
    bool hasMethod( const QString& ) const;
135
 
 
136
 
    bool call( void* instance, KSContext& context, const QString& name );
137
 
 
138
 
protected:
139
 
    /*
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.
142
 
     */
143
 
    virtual KSValue::Ptr property( KSContext& context, void* object, const QString& name ) = 0;
144
 
    /**
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.
147
 
     *
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.
150
 
     */
151
 
    virtual bool setProperty( KSContext& context, void* object, const QString& name, const KSValue::Ptr value ) = 0;
152
 
 
153
 
private:
154
 
    struct Method
155
 
    {
156
 
        MethodPtr m_method;
157
 
        QCString m_signature;
158
 
    };
159
 
 
160
 
    QMap<QString,Method> m_methods;
161
 
};
162
 
 
163
 
 
164
 
class KSBuiltinStruct : public KSStruct
165
 
{
166
 
public:
167
 
    KSBuiltinStruct( KSStructClass* c, void* object );
168
 
    /**
169
 
     * Destroys the struct and the associated C++ object.
170
 
     *
171
 
     * @see KSBuiltinStructClass::destructor
172
 
     */
173
 
    virtual ~KSBuiltinStruct();
174
 
 
175
 
    virtual KSValue::Ptr member( KSContext&, const QString& name );
176
 
    virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
177
 
 
178
 
    /**
179
 
     * This is the universal method dispatcher.
180
 
     *
181
 
     * @see KSBuiltinStructClass::call
182
 
     */
183
 
    bool call( KSContext& context, const QString& name );
184
 
 
185
 
    /**
186
 
     * Make a real copy of the struct. That means that the C++ object
187
 
     * is cloned, too.
188
 
     *
189
 
     * @see KSBuiltinStructClass::clone
190
 
     */
191
 
    KSStruct* clone();
192
 
 
193
 
    /**
194
 
     * @return a pointer to the C++ object that holds the real data of this struct.
195
 
     */
196
 
    void* object();
197
 
    const void* object() const;
198
 
 
199
 
private:
200
 
    void* m_object;
201
 
};
202
 
 
203
 
#endif