Kross
interpreter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "interpreter.h"
00021 #include "script.h"
00022 #include "action.h"
00023 #include "manager.h"
00024
00025 #include <klibloader.h>
00026
00027 extern "C"
00028 {
00029 typedef void* (*def_interpreter_func)(int version, Kross::InterpreterInfo*);
00030 }
00031
00032 using namespace Kross;
00033
00034
00035
00036
00037
00038 namespace Kross {
00039
00041 class InterpreterInfo::Private
00042 {
00043 public:
00045 QString interpretername;
00047 QString library;
00049 QString wildcard;
00051 QStringList mimetypes;
00053 Option::Map options;
00055 Interpreter* interpreter;
00056 };
00057
00058 }
00059
00060 InterpreterInfo::InterpreterInfo(const QString& interpretername, const QString& library, const QString& wildcard, QStringList mimetypes, Option::Map options)
00061 : ErrorInterface()
00062 , d( new Private() )
00063 {
00064 d->interpretername = interpretername;
00065 d->library = library;
00066 d->wildcard = wildcard;
00067 d->mimetypes = mimetypes;
00068 d->options = options;
00069 d->interpreter = 0;
00070 }
00071
00072 InterpreterInfo::~InterpreterInfo()
00073 {
00074 delete d->interpreter;
00075
00076 delete d;
00077 }
00078
00079 const QString InterpreterInfo::interpreterName() const
00080 {
00081 return d->interpretername;
00082 }
00083
00084 const QString InterpreterInfo::wildcard() const
00085 {
00086 return d->wildcard;
00087 }
00088
00089 const QStringList InterpreterInfo::mimeTypes() const
00090 {
00091 return d->mimetypes;
00092 }
00093
00094 bool InterpreterInfo::hasOption(const QString& key) const
00095 {
00096 return d->options.contains(key);
00097 }
00098
00099 InterpreterInfo::Option* InterpreterInfo::option(const QString& name) const
00100 {
00101 return d->options[name];
00102 }
00103
00104 const QVariant InterpreterInfo::optionValue(const QString& name, const QVariant& defaultvalue) const
00105 {
00106 Option* o = option(name);
00107 return o ? o->value : defaultvalue;
00108 }
00109
00110 InterpreterInfo::Option::Map InterpreterInfo::options()
00111 {
00112 return d->options;
00113 }
00114
00115 Interpreter* InterpreterInfo::interpreter()
00116 {
00117 if(d->interpreter)
00118 return d->interpreter;
00119
00120 if(hadError())
00121 return 0;
00122
00123 #ifdef KROSS_INTERPRETER_DEBUG
00124 krossdebug( QString("Loading the interpreter library for %1").arg(d->interpretername) );
00125 #endif
00126
00127
00128 KLibLoader *loader = KLibLoader::self();
00129
00130 KLibrary* library = loader->library( d->library, QLibrary::ExportExternalSymbolsHint );
00131 if(! library) {
00132 setError(i18n("Could not load interpreter library \"%1\"",d->library), loader->lastErrorMessage());
00133 return 0;
00134 }
00135
00136
00137 def_interpreter_func interpreter_func;
00138 interpreter_func = (def_interpreter_func) library->resolveFunction("krossinterpreter");
00139
00140 d->interpreter = interpreter_func
00141 ? (Interpreter*) (interpreter_func)(KROSS_VERSION, this)
00142 : 0;
00143 if(! d->interpreter) {
00144 setError(i18n("Incompatible interpreter library."));
00145 }
00146 else {
00147
00148
00149 #ifdef KROSS_INTERPRETER_DEBUG
00150 krossdebug("Successfully loaded Interpreter instance from library.");
00151 #endif
00152 }
00153
00154
00155 library->unload();
00156
00157 return d->interpreter;
00158 }
00159
00160
00161
00162
00163
00164 namespace Kross {
00165
00167 class Interpreter::Private
00168 {
00169 public:
00171 InterpreterInfo* interpreterinfo;
00172
00173 Private(InterpreterInfo* info) : interpreterinfo(info) {}
00174 };
00175
00176 }
00177
00178 Interpreter::Interpreter(InterpreterInfo* info)
00179 : ErrorInterface()
00180 , d( new Private(info) )
00181 {
00182 }
00183
00184 Interpreter::~Interpreter()
00185 {
00186 delete d;
00187 }
00188
00189 InterpreterInfo* Interpreter::interpreterInfo() const
00190 {
00191 return d->interpreterinfo;
00192 }
00193
00194 void Interpreter::virtual_hook(int id, void* data)
00195 {
00196 Q_UNUSED(id);
00197 Q_UNUSED(data);
00198 }