Kross

core/interpreter.cpp
1 /***************************************************************************
2  * interpreter.cpp
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 #include "interpreter.h"
21 #include "script.h"
22 #include "action.h"
23 #include "manager.h"
24 #include "kross_debug.h"
25 
26 extern "C"
27 {
28  typedef void *(*def_interpreter_func)(int version, Kross::InterpreterInfo *);
29 }
30 
31 using namespace Kross;
32 
33 /*************************************************************************
34  * InterpreterInfo
35  */
36 
37 namespace Kross
38 {
39 
40 /// \internal d-pointer class.
41 class InterpreterInfo::Private
42 {
43 public:
44  /// The name the interpreter has. Could be something like "python" or "ruby".
45  QString interpretername;
46  /// The function-pointer to the module factory function.
47  QFunctionPointer funcPtr;
48  /// The file wildcard used to determinate extensions.
50  /// List of mimetypes this interpreter supports.
51  QStringList mimetypes;
52  /// A map with options.
54  /// The \a Interpreter instance.
56 };
57 
58 }
59 
60 InterpreterInfo::InterpreterInfo(const QString &interpretername, QFunctionPointer funcPtr, const QString &wildcard, const QStringList &mimetypes, const Option::Map &options)
61  : d(new Private())
62 {
63  d->interpretername = interpretername;
64  d->funcPtr = funcPtr;
65  d->wildcard = wildcard;
66  d->mimetypes = mimetypes;
67  d->options = options;
68  d->interpreter = nullptr;
69 }
70 
72 {
73  delete d->interpreter;
74  d->interpreter = nullptr;
75  delete d;
76 }
77 
79 {
80  return d->interpretername;
81 }
82 
84 {
85  return d->wildcard;
86 }
87 
89 {
90  return d->mimetypes;
91 }
92 
93 bool InterpreterInfo::hasOption(const QString &name) const
94 {
95  return d->options.contains(name);
96 }
97 
99 {
100  return d->options.contains(name) ? d->options[name] : nullptr;
101 }
102 
104 {
105  return d->options;
106 }
107 
108 const QVariant InterpreterInfo::optionValue(const QString &name, const QVariant &defaultvalue) const
109 {
110  return d->options.contains(name) ? d->options[name]->value : defaultvalue;
111 }
112 
114 {
115  if (d->interpreter) { // buffered
116  return d->interpreter;
117  }
118 
119  //#ifdef KROSS_INTERPRETER_DEBUG
120  qCDebug(KROSS_LOG) << "Loading the interpreter library for " << d->interpretername;
121  //#endif
122 
123  // Get the extern "C" krosspython_instance function.
124  def_interpreter_func interpreter_func = (def_interpreter_func) d->funcPtr;
125 
126  // and execute the extern krosspython_instance function.
127  d->interpreter = interpreter_func
128  ? (Interpreter *)(interpreter_func)(KROSS_VERSION, this)
129  : nullptr;
130 
131  if (! d->interpreter) {
132  //#ifdef KROSS_INTERPRETER_DEBUG
133  qCWarning(KROSS_LOG) << "Incompatible interpreter library.";
134  //#endif
135  } else {
136  // Job done. The library is loaded and our Interpreter* points
137  // to the external Kross::Python::Interpreter* instance.
138  //#ifdef KROSS_INTERPRETER_DEBUG
139  qCDebug(KROSS_LOG) << "Successfully loaded Interpreter instance from library.";
140  //#endif
141  }
142 
143  return d->interpreter;
144 }
145 
146 /*************************************************************************
147  * Interpreter
148  */
149 
150 namespace Kross
151 {
152 
153 /// \internal d-pointer class.
154 class Interpreter::Private
155 {
156 public:
157  InterpreterInfo *interpreterinfo;
158  Private(InterpreterInfo *info) : interpreterinfo(info) {}
159 };
160 
161 }
162 
164  : QObject()
165  , ErrorInterface()
166  , d(new Private(info))
167 {
168 }
169 
171 {
172  delete d;
173 }
174 
176 {
177  return d->interpreterinfo;
178 }
179 
Interface for error-handling.
Definition: action.cpp:36
~Interpreter() override
Destructor.
Option * option(const QString &name) const
Each interpreter is able to define options we could use to manipulate the interpreter behaviour...
InterpreterInfo * interpreterInfo() const
QMap< QString, Option * > Map
Map of options.
The InterpreterInfo class provides abstract information about a Interpreter before the interpreter-ba...
bool hasOption(const QString &name) const
InterpreterInfo(const QString &interpretername, QFunctionPointer funcPtr, const QString &wildcard, const QStringList &mimetypes, const Option::Map &options=Option::Map())
Constructor.
const QVariant optionValue(const QString &name, const QVariant &defaultvalue=QVariant()) const
Base class for interpreter implementations.
Interpreter(InterpreterInfo *info)
Constructor.
~InterpreterInfo()
Destructor.
const QString wildcard() const
const QString interpreterName() const
const QStringList mimeTypes() const
List of mimetypes this interpreter supports.
KDB_EXPORT KDbVersionInfo version()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Oct 24 2021 22:59:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.