Kross

core/interpreter.h
1 /***************************************************************************
2  * interpreter.h
3  * This file is part of the KDE project
4  * copyright (C)2004-2006 by Sebastian Sauer ([email protected])
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 #ifndef KROSS_INTERPRETER_H
21 #define KROSS_INTERPRETER_H
22 
23 #include "errorinterface.h"
24 
25 #include <QStringList>
26 #include <QVariant>
27 #include <QMap>
28 #include <QObject>
29 
30 namespace Kross
31 {
32 
33 // Forward declaration.
34 class Manager;
35 class Action;
36 class Script;
37 class Interpreter;
38 
39 /**
40  * The InterpreterInfo class provides abstract information about
41  * a \a Interpreter before the interpreter-backend itself is
42  * loaded.
43  */
44 class KROSSCORE_EXPORT InterpreterInfo
45 {
46 public:
47 
48  /**
49  * Each interpreter is able to define options we could
50  * use to manipulate the interpreter behaviour.
51  */
52  class Option
53  {
54  public:
55 
56  /**
57  * Map of options.
58  */
59  typedef QMap< QString, Option * > Map;
60 
61  /**
62  * Constructor.
63  *
64  * \param comment A localized comment that describes
65  * the option.
66  * \param value The QVariant value this option has.
67  */
68  Option(const QString &comment, const QVariant &value)
69  : comment(comment), value(value) {}
70 
71  /// A description of the option.
72  QString comment;
73 
74  /// The value the option has.
75  QVariant value;
76  };
77 
78  /**
79  * Constructor.
80  *
81  * \param interpretername The name of the interpreter. The name is
82  * used internally as unique identifier for the interpreter and
83  * could be for example "python", "ruby" or "javascript".
84  * \param funcPtr A pointer to the entry function within the
85  * library. The entry function each interpreter-backend does
86  * provide looks like this;
87  * \code
88  * typedef void* (*def_interpreter_func)(int version, Kross::InterpreterInfo*);
89  * \endcode
90  * The def_interpreter_func function will be used by Kross to load
91  * the interpreter's library. The passed version is used to be able
92  * to versioning details and we use the KROSS_VERSION defined within
93  * the krossconfig.h file here.
94  * \param wildcard File wildcard that identifies a by the interpreter
95  * supported scripting files. As example Python does define here
96  * "*.py" while Java does define "*.java *.class".
97  * \param mimetypes The file mimetype that identifies a by the interpreter
98  * supported scripting files. As example Python does define "text/x-python"
99  * here while Ruby defines "application/x-ruby" and Java "application/java".
100  * \param options The optional list of options supported by the interpreter
101  * to configure the backend.
102  */
103  InterpreterInfo(const QString &interpretername, QFunctionPointer funcPtr, const QString &wildcard, const QStringList &mimetypes, const Option::Map &options = Option::Map());
104 
105  /**
106  * Destructor.
107  */
108  ~InterpreterInfo();
109 
110  /**
111  * \return the name of the interpreter. For example "python" or "ruby".
112  */
113  const QString interpreterName() const;
114 
115  /**
116  * \return the file-wildcard used to determinate by this interpreter
117  * used scriptingfiles. For example python just defines it as "*py".
118  */
119  const QString wildcard() const;
120 
121  /**
122  * List of mimetypes this interpreter supports.
123  * \return QStringList with mimetypes like "application/javascript".
124  */
125  const QStringList mimeTypes() const;
126 
127  /**
128  * \return true if an \a Option with that \p key exists else false.
129  */
130  bool hasOption(const QString &name) const;
131 
132  /**
133  * \return the option defined with \p name .
134  */
135  Option *option(const QString &name) const;
136 
137  /**
138  * \return the reference to the intenal used map with all options.
139  */
140  Option::Map &options();
141 
142  /**
143  * \return the value of the option defined with \p name . If there
144  * doesn't exists an option with such a name, the \p defaultvalue
145  * is returned.
146  */
147  const QVariant optionValue(const QString &name, const QVariant &defaultvalue = QVariant()) const;
148 
149  /**
150  * \return the \a Interpreter instance this \a InterpreterInfo
151  * is the describer for. If the interpreter that implements the
152  * scripting backend isn't loaded yet, this method will trigger
153  * the loading of the interpreter's library. Note that this
154  * method may return NULL if there is no library for that
155  * interpreter installed or if the library is incompatible.
156  */
157  Interpreter *interpreter();
158 
159 private:
160  /// \internal d-pointer class.
161  class Private;
162  /// \internal d-pointer instance.
163  Private *const d;
164 };
165 
166 /**
167  * Base class for interpreter implementations.
168  *
169  * Each scripting backend needs to inherit its own
170  * interpreter and implement it.
171  *
172  * The Interpreter will be managed by the \a Manager
173  * class and does provide a factory method to create
174  * \a Script implementations.
175  */
176 class KROSSCORE_EXPORT Interpreter : public QObject, public ErrorInterface
177 {
178  Q_OBJECT
179 public:
180 
181  /**
182  * Constructor.
183  *
184  * \param info is the \a InterpreterInfo instance
185  * that describes this interpreter.
186  */
187  explicit Interpreter(InterpreterInfo *info);
188 
189  /**
190  * Destructor.
191  */
192  ~Interpreter() override;
193 
194  /**
195  * \return the \a InterpreterInfo that represents
196  * this \a Interpreter .
197  */
198  InterpreterInfo *interpreterInfo() const;
199 
200  /**
201  * Create and return a new interpreter dependent
202  * \a Script instance.
203  *
204  * \param Action The \a Action
205  * to use for the \a Script instance.
206  * \return The from \a Script inherited instance.
207  */
208  virtual Script *createScript(Action *Action) = 0;
209 
210 private:
211  /// \internal d-pointer class.
212  class Private;
213  /// \internal d-pointer instance.
214  Private *const d;
215 };
216 
217 }
218 
219 #endif
220 
Base class for interpreter implementations.
The Action class is an abstract container to deal with scripts like a single standalone script file.
Definition: action.h:112
The InterpreterInfo class provides abstract information about a Interpreter before the interpreter-ba...
Each interpreter is able to define options we could use to manipulate the interpreter behaviour.
Interface for error-handling.
Base class for interpreter dependent functionality each script provides.
Definition: core/script.h:61
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Dec 7 2023 04:10:00 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.