File indexing completed on 2024-04-14 14:27:09

0001 /***************************************************************************
0002  * interpreter.h
0003  * This file is part of the KDE project
0004  * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #ifndef KROSS_INTERPRETER_H
0021 #define KROSS_INTERPRETER_H
0022 
0023 #include "errorinterface.h"
0024 
0025 #include <QStringList>
0026 #include <QVariant>
0027 #include <QMap>
0028 #include <QObject>
0029 
0030 namespace Kross
0031 {
0032 
0033 // Forward declaration.
0034 class Manager;
0035 class Action;
0036 class Script;
0037 class Interpreter;
0038 
0039 /**
0040  * The InterpreterInfo class provides abstract information about
0041  * a \a Interpreter before the interpreter-backend itself is
0042  * loaded.
0043  */
0044 class KROSSCORE_EXPORT InterpreterInfo
0045 {
0046 public:
0047 
0048     /**
0049      * Each interpreter is able to define options we could
0050      * use to manipulate the interpreter behaviour.
0051      */
0052     class Option
0053     {
0054     public:
0055 
0056         /**
0057         * Map of options.
0058         */
0059         typedef QMap< QString, Option * > Map;
0060 
0061         /**
0062          * Constructor.
0063          *
0064          * \param comment A localized comment that describes
0065          * the option.
0066          * \param value The QVariant value this option has.
0067          */
0068         Option(const QString &comment, const QVariant &value)
0069             : comment(comment), value(value) {}
0070 
0071         /// A description of the option.
0072         QString comment;
0073 
0074         /// The value the option has.
0075         QVariant value;
0076     };
0077 
0078     /**
0079      * Constructor.
0080      *
0081      * \param interpretername The name of the interpreter. The name is
0082      * used internally as unique identifier for the interpreter and
0083      * could be for example "python", "ruby" or "javascript".
0084      * \param funcPtr A pointer to the entry function within the
0085      * library. The entry function each interpreter-backend does
0086      * provide looks like this;
0087      * \code
0088      * typedef void* (*def_interpreter_func)(int version, Kross::InterpreterInfo*);
0089      * \endcode
0090      * The def_interpreter_func function will be used by Kross to load
0091      * the interpreter's library. The passed version is used to be able
0092      * to versioning details and we use the KROSS_VERSION defined within
0093      * the krossconfig.h file here.
0094      * \param wildcard File wildcard that identifies a by the interpreter
0095      * supported scripting files. As example Python does define here
0096      * "*.py" while Java does define "*.java *.class".
0097      * \param mimetypes The file mimetype that identifies a by the interpreter
0098      * supported scripting files. As example Python does define "text/x-python"
0099      * here while Ruby defines "application/x-ruby" and Java "application/java".
0100      * \param options The optional list of options supported by the interpreter
0101      * to configure the backend.
0102      */
0103     InterpreterInfo(const QString &interpretername, QFunctionPointer funcPtr, const QString &wildcard, const QStringList &mimetypes, const Option::Map &options = Option::Map());
0104 
0105     /**
0106      * Destructor.
0107      */
0108     ~InterpreterInfo();
0109 
0110     /**
0111      * \return the name of the interpreter. For example "python" or "ruby".
0112      */
0113     const QString interpreterName() const;
0114 
0115     /**
0116      * \return the file-wildcard used to determinate by this interpreter
0117      * used scriptingfiles. For example python just defines it as "*py".
0118      */
0119     const QString wildcard() const;
0120 
0121     /**
0122      * List of mimetypes this interpreter supports.
0123      * \return QStringList with mimetypes like "application/javascript".
0124      */
0125     const QStringList mimeTypes() const;
0126 
0127     /**
0128      * \return true if an \a Option with that \p key exists else false.
0129      */
0130     bool hasOption(const QString &name) const;
0131 
0132     /**
0133      * \return the option defined with \p name .
0134      */
0135     Option *option(const QString &name) const;
0136 
0137     /**
0138      * \return the reference to the intenal used map with all options.
0139      */
0140     Option::Map &options();
0141 
0142     /**
0143      * \return the value of the option defined with \p name . If there
0144      * doesn't exists an option with such a name, the \p defaultvalue
0145      * is returned.
0146      */
0147     const QVariant optionValue(const QString &name, const QVariant &defaultvalue = QVariant()) const;
0148 
0149     /**
0150      * \return the \a Interpreter instance this \a InterpreterInfo
0151      * is the describer for. If the interpreter that implements the
0152      * scripting backend isn't loaded yet, this method will trigger
0153      * the loading of the interpreter's library. Note that this
0154      * method may return NULL if there is no library for that
0155      * interpreter installed or if the library is incompatible.
0156      */
0157     Interpreter *interpreter();
0158 
0159 private:
0160     /// \internal d-pointer class.
0161     class Private;
0162     /// \internal d-pointer instance.
0163     Private *const d;
0164 };
0165 
0166 /**
0167  * Base class for interpreter implementations.
0168  *
0169  * Each scripting backend needs to inherit its own
0170  * interpreter and implement it.
0171  *
0172  * The Interpreter will be managed by the \a Manager
0173  * class and does provide a factory method to create
0174  * \a Script implementations.
0175  */
0176 class KROSSCORE_EXPORT Interpreter : public QObject, public ErrorInterface
0177 {
0178     Q_OBJECT
0179 public:
0180 
0181     /**
0182      * Constructor.
0183      *
0184      * \param info is the \a InterpreterInfo instance
0185      *        that describes this interpreter.
0186      */
0187     explicit Interpreter(InterpreterInfo *info);
0188 
0189     /**
0190      * Destructor.
0191      */
0192     ~Interpreter() override;
0193 
0194     /**
0195      * \return the \a InterpreterInfo that represents
0196      * this \a Interpreter .
0197      */
0198     InterpreterInfo *interpreterInfo() const;
0199 
0200     /**
0201      * Create and return a new interpreter dependent
0202      * \a Script instance.
0203      *
0204      * \param Action The \a Action
0205      *        to use for the \a Script instance.
0206      * \return The from \a Script inherited instance.
0207      */
0208     virtual Script *createScript(Action *Action) = 0;
0209 
0210 private:
0211     /// \internal d-pointer class.
0212     class Private;
0213     /// \internal d-pointer instance.
0214     Private *const d;
0215 };
0216 
0217 }
0218 
0219 #endif
0220